查看表的创建语句方法 SQL Server
(2013-06-07 13:49:56)
标签:
sqlserver表创建语句表创建语句查询查看表的创建语句sqlseverit |
分类: 数据库学习 |
一、右键直接提取法
二、存储过程提取法
create procedure SP_GET_TABLE_INFO@ObjName varchar(128)asdeclare @Script varchar(255)declare @ColName varchar(30)declare @ColID TinyIntdeclare @UserType smallintdeclare @TypeName sysnamedeclare @Length TinyIntdeclare @Prec TinyIntdeclare @Scale TinyIntdeclare @Status TinyIntdeclare @cDefault intdeclare @DefaultID TinyIntdeclare @Const_Key varchar(255)declare @IndID SmallIntdeclare @IndStatus Intdeclare @Index_Key varchar(255)declare @DBName varchar(30)declare @strPri_Key varchar (255)if not Exists(Select name from sysobjects where name = @ObjName)beginselect @DBName = db_name()raiserror(15009,-1,-1,@ObjName,@DBName)return (1)endcreate table #spscript(id int IDENTITY not null,Script Varchar(255) NOT NULL,LastLine tinyint)declare Cursor_Column INSENSITIVE CURSORfor Select a.name,a.ColID,a.usertype,b.name,a.length,a.prec,a.scale,a.Status,a.cDefault,case a.cdefault when 0 then ' ' else (select c.Text from syscomments c where a.cdefault = c.id) end const_keyfrom syscolumns a, systypes b where object_name(a.id) = @ObjNameand a.usertype = b.usertype order by a.ColIDset nocount onSelect @Script = 'Create table ' + @ObjName + '('Insert into #spscript values(@Script,0)open Cursor_Columnfetch next from Cursor_Column into @ColName,@ColID,@UserType,@TypeName,@Length,@Prec,@Scale,@Status,@cDefault,@Const_KeySelect @Script = ''while (@@FETCH_STATUS <> -1)beginif (@@FETCH_STATUS <> -2)beginSelect @Script = @ColName + ' ' + @TypeNameif @UserType in (1,2,3,4)Select @Script = @Script + '(' + Convert(char(3),@Length) + ') 'else if @UserType in (24)Select @Script = @Script + '(' + Convert(char(3),@Prec) + ','+ Convert(char(3),@Scale) + ') 'elseSelect @Script = @Script + ' 'if ( @Status & 0x80 ) > 0Select @Script = @Script + ' IDENTITY(1,1) 'if ( @Status & 0x08 ) > 0Select @Script = @Script + ' NULL 'elseSelect @Script = @Script + ' NOT NULL 'if @cDefault > 0Select @Script = @Script + ' DEFAULT ' + @Const_Keyendfetch next from Cursor_Column into @ColName,@ColID,@UserType,@TypeName,@Length,@Prec,@Scale,@Status,@cDefault,@Const_Keyif @@FETCH_STATUS = 0beginSelect @Script = @Script + ','Insert into #spscript values(@Script,0)endelsebeginInsert into #spscript values(@Script,1)Insert into #spscript values(')',0)endendClose Cursor_ColumnDeallocate Cursor_ColumnDeclare Cursor_Index INSENSITIVE CURSORfor Select name,IndID,status from sysindexes where object_name(id)=@ObjNameand IndID > 0 and IndID<>255 order by IndIDOpen Cursor_IndexFetch Next from Cursor_Index into @ColName, @IndID, @IndStatuswhile (@@FETCH_STATUS <> -1)beginif @@FETCH_STATUS <> -2begindeclare @i TinyIntdeclare @thiskey varchar(50)declare @IndDesc varchar(68)Select @i = 1while (@i <= 16)beginselect @thiskey = index_col(@ObjName, @IndID, @i)if @thiskey is nullbreakif @i = 1select @Index_Key = index_col(@ObjName, @IndID, @i)elseselect @Index_Key = @Index_Key + ', ' + index_col(@ObjName, @IndID, @i)select @i = @i + 1endif (@IndStatus & 0x02) > 0Select @Script = 'Create unique 'elseSelect @Script = 'Create 'if @IndID = 1select @Script = @Script + ' clustered 'if (@IndStatus & 0x800) > 0select @strPri_Key = ' PRIMARY KEY (' + @Index_Key + ')'elseselect @strPri_Key = ''if @IndID > 1select @Script = @Script + ' nonclustered 'Select @Script = @Script + ' index ' + @ColName + ' ON '+ @ObjName+ '(' + @Index_Key + ')'Select @IndDesc = ''if @IndStatus & 0x01 = 0x01Select @IndDesc = @IndDesc + ' IGNORE_DUP_KEY' + ','if @IndStatus & 0x40 = 0x40Select @IndDesc = @IndDesc + ' ALLOW_DUP_ROW' + ','if @IndDesc <> ''beginSelect @IndDesc = SubString( @IndDesc, 1, DataLength(@IndDesc) - 1 )Select @Script = @Script + ' WITH ' + @IndDescendendif (@strPri_Key = '')Insert into #spscript values(@Script,0)elseupdate #spscript set Script = Script + @strPri_Key where LastLine = 1Fetch Next from Cursor_Index into @ColName, @IndID, @IndStatusendDeallocate Cursor_IndexSelect Script from #spscriptset nocount offreturn (0)