作者:1712477436 | 来源:互联网 | 2023-10-14 13:47
createprocedureup_getTableStructv_tableNamevarchar(256)asdeclarei_objectIdint,--對象idi_indI
create procedure up_getTableStruct
@v_tableName varchar(256)
as
declare @i_objectId int, -- 對象id
@i_indId smallint, -- 索引id
@v_pkInfo varchar(100), -- 主鍵信息
@v_clusteredInfo varchar(20), -- clustered信息
@v_pkCol varchar(100), -- 主鍵字段
@v_key varchar(50),
@i_i smallint
set @i_objectId = object_id(@v_tableName)
if @i_objectId is null -- 判斷對象是否存在
begin
print 'The object not exists'
return
end
if OBJECTPROPERTY(&#64;i_objectId,&#39;IsTable&#39;) <> 1 -- 判斷對象是否是table
begin
print &#39;The object is not table&#39;
return
end
create table #temp1
(
i_id int identity,
v_desc varchar(200)
)
insert into #temp1(v_desc)
values(&#39;create table &#39;&#43;&#64;v_tableName&#43;&#39;(&#39;) --
insert into #temp1(v_desc) -- 將表的字段信息存入臨時表
select a.name&#43;space(4)&#43;b.name&#43;
case when b.xtype in (167,175,231,239) then &#39;(&#39;&#43;cast(a.length as varchar)&#43;&#39;)&#39;
when b.xtype in (106,108) then &#39;(&#39;&#43;cast(a.xprec as varchar)&#43;&#39;,&#39;&#43;cast(a.xscale as varchar)&#43;&#39;)&#39;
else &#39;&#39; end&#43;space(4)&#43;
case when (a.colstat & 1 &#61; 1) then &#39;identity(&#39;&#43;cast(ident_seed(&#64;v_tableName) as varchar)&#43;&#39;,&#39; &#43;
cast(ident_incr(&#64;v_tableName) as varchar)&#43;&#39;)&#39; else &#39;&#39; end &#43;space(4)&#43;
case a.isnullable when 0 then &#39;not null&#39; else &#39;null&#39; end&#43;&#39;|&#39;
from syscolumns a,systypes b
where a.id &#61; &#64;i_objectId and a.xtype &#61; b.xusertype
order by a.colid
create procedure 名
&#64;aa varchar(10)
as
begin
select a.id 编号,a.name 名称,b.id,b.name from table1 a join table2 b on a.id&#61;b.id where
end
if exists(select 1 from sysobjects where parent_obj &#61; &#64;i_objectId and xtype &#61; &#39;PK&#39;) -- 如果存在主鍵
begin
select &#64;v_pkInfo &#61; b.name,&#64;i_indId &#61; indid, -- 得到主鍵名,id及是否clustered信息
&#64;v_clusteredInfo &#61; (case when (a.status & 16)&#61;16 then &#39;clustered&#39; else &#39;nonclustered&#39; end )
from sysindexes a,sysobjects b
where a.id &#61; b.parent_obj and a.name &#61; b.name and b.xtype &#61; &#39;PK&#39; and b.parent_obj &#61; &#64;i_objectId
select &#64;v_pkCol &#61; index_col(&#64;v_tableName, &#64;i_indId, 1), &#64;i_i &#61; 2 -- 得到主鍵的第1個字段名
select &#64;v_key &#61; index_col(&#64;v_tableName, &#64;i_indId, &#64;i_i) -- 得到主鍵的第2個字段名
while (&#64;v_key is not null)
begin
select &#64;v_pkCol &#61; &#64;v_pkCol &#43; &#39;,&#39; &#43; &#64;v_key, &#64;i_i &#61; &#64;i_i &#43; 1
select &#64;v_key &#61; index_col(&#64;v_tableName, &#64;i_indId, &#64;i_i) -- 得到主鍵的第&#64;i_i個字段名
end -- 組合成主鍵信息
set &#64;v_pkInfo &#61; &#39;constraint &#39;&#43;&#64;v_pkInfo&#43;&#39; primary key &#39;&#43;&#64;v_clusteredInfo&#43;&#39;(&#39;&#43;&#64;v_pkCol&#43;&#39;)&#39;
insert into #temp1(v_desc) values(&#64;v_pkInfo) -- 將主鍵信息插入臨時表
end
else
begin
select &#64;i_i &#61; count(1) from #temp1
-- 如果沒有主鍵,那麼將最後一筆紀錄的&#39;|&#39;去掉
update #temp1 set v_desc &#61; replace(v_desc,&#39;|&#39;,&#39;&#39;) where i_id &#61; &#64;i_i
end
insert into #temp1(v_desc) values(&#39;)&#39;) --
update #temp1 set v_desc &#61; replace(v_desc,&#39;|&#39;,&#39;,&#39;)
select v_desc from #temp1 order by i_id
drop table #temp1