存储过程.up_getTableStruct

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了存储过程.up_getTableStruct相关的知识,希望对你有一定的参考价值。

--存储过程.up_getTableStruct

create procedure [dbo].[up_getTableStruct]
@v_tableName varchar(256)
as
declare @i_objectId int,
@i_indId smallint,
@v_pkInfo varchar(100),
@v_clusteredInfo varchar(20),
@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(@i_objectId,‘IsTable‘) <> 1
begin
print ‘The object is not table‘
return
end

create table #temp1
(
i_id int identity,
v_desc varchar(200)
)

insert into #temp1(v_desc)
values(‘create table ‘[email protected]_tableName+‘(‘)

insert into #temp1(v_desc)
select a.name+space(4)+b.name+
case when b.xtype in (167,175,231,239) then ‘(‘+cast(a.length as varchar)+‘)‘
when b.xtype in (106,108) then ‘(‘+cast(a.xprec as varchar)+‘,‘+cast(a.xscale as
varchar)+‘)‘
else ‘‘ end+space(4)+
case when (a.colstat & 1 = 1) then ‘identity(‘+cast(ident_seed(@v_tableName) as
varchar)+‘,‘ +
cast(ident_incr(@v_tableName) as varchar)+‘)‘ else ‘‘ end +space(4)+
case a.isnullable when 0 then ‘not null‘ else ‘null‘ end+‘|‘
from syscolumns a,systypes b
where a.id = @i_objectId and a.xtype = b.xusertype
order by a.colid

if exists(select 1 from sysobjects where parent_obj = @i_objectId and xtype = ‘PK‘)
begin
select @v_pkInfo = b.name,@i_indId = indid,
@v_clusteredInfo = (case when (a.status & 16)=16 then ‘clustered‘ else
‘nonclustered‘ end )
from sysindexes a,sysobjects b
where a.id = b.parent_obj and a.name = b.name and b.xtype = ‘PK‘ and b.parent_obj =
@i_objectId

select @v_pkCol = index_col(@v_tableName, @i_indId, 1), @i_i = 2
select @v_key = index_col(@v_tableName, @i_indId, @i_i)
while (@v_key is not null)
begin
select @v_pkCol = @v_pkCol + ‘,‘ + @v_key, @i_i = @i_i + 1
select @v_key = index_col(@v_tableName, @i_indId, @i_i)
end
set @v_pkInfo = ‘ primary key ‘[email protected]_clusteredInfo+‘(‘[email protected]_pkCol+‘)‘
insert into #temp1(v_desc) values(@v_pkInfo)
end
else
begin
select @i_i = count(1) from #temp1
update #temp1 set v_desc = replace(v_desc,‘|‘,‘‘) where i_id = @i_i
end

insert into #temp1(v_desc) values(‘)‘)
update #temp1 set v_desc = replace(v_desc,‘|‘,‘,‘)

select v_desc from #temp1 order by i_id

drop table #temp1


GO

 

以上是关于存储过程.up_getTableStruct的主要内容,如果未能解决你的问题,请参考以下文章

T-SQL 跳过存储过程

sqlserver存储过程会不会被阻塞

mysql存储过程中最后有commit和没有commit 是有所不同的。(为测试但是碰到过这个问题)

存储过程执行时间过长时发出 SQL 警报

SQLServer中通过脚本内容查找存储过程

向存储过程添加 IF ...ELSE 语句以跳过重复的主键