How to get a table creation statement in MS SQLServer

xiaoxiao2021-03-06  90

In MS SQLServer, you can use

SP_HELPTEXT ProcedureName Gets the creation statement of the stored procedure. But often we need to get the creation statement of the table. For example, it is said that a table has changed when the database is upgraded, or there is already a table, but do not know what it is, there is no constraint, there is a primary key , Which indexes have been created. I will give a stored procedure for the reader's reference.

This stored procedure can get all the creative statements of all the tables you want, including the creation statement of the index associated with the table.

Code under SQLServer2000

Create Procedure SP_GET_TABLE_INFO @ ObjName Varchar (128) / * The Table To Generate SQL Script * / as

declare @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 Smallint Declare @indstatus INTDECLARE @index_key varchar (255) Declare @dbname varchar (30) Declare @strpri_key varchar (255)

/ *** Check to see the the Table exists and initialize @objid. * / If not exists (select name from sysobjects where name = @objname) Begin select @dbname = db_name () Raiserror (15009, -1, -1, @ Objname, @ dbname) Return (1) End

Create Table #SPScript (ID Int IDENTIN NOT NULL, SCRIPT VARCHAR (255) Not Null, Lastline Tinyint

Declare Cursor_Column Innsitive Cursor for 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_key from syscolumns a, systypes b where object_name (a.id) = @ObjName and a.usertype = b.usertype order by a.ColID

SET NOCOUNT ONSELECT @Script = 'Create Table' @objname '(' INSERT INTO #SPScript Values ​​(@ script, 0) / * Get Column Information * / Open Cursor_COLUMN

Fetch next from cursor_column @ colname, @ colid, @ @ @ @ PREC, @ scale, @ status, @ cdefault, @ const_key

Select @script = 'while (@@ fetch_status <> -1) Begin if (@@ fetch_status <> -2) begin select @script = @colname ' ' @Typename if @USERTYPE IN (1, 2, 3, 4) SELECT @Script = @script '(' Convert (Char (3), @ Length) ')' Else if @USERTYPE IN (24) SELECT @Script = @Script '(' Convert (char (char (char) 3), @ PREC) ',' Convert (CHAR (3), @ Scale) ')' Else SELECT @Script = @Script '' IF (@Status & 0x80)> 0 Select @script = @Script 'Identity (1, 1)'

if (@Status & 0x08)> 0 Select @Script = @Script 'NULL' else Select @Script = @Script 'NOT NULL' if @cDefault> 0 Select @Script = @Script 'DEFAULT' @Const_Key end fetch next from Cursor_Column into @ ColName, @ ColID, @ UserType, @ TypeName, @ Length, @ Prec, @ Scale, @ Status, @ cDefault, @ Const_Key if @@ FETCH_STATUS = 0 begin Select @Script = @Script ', 'Insert into #spscript values ​​(@ Script, 0) end else begin Insert into #spscript values ​​(@ Script, 1) Insert into #spscript values ​​(') ', 0) endendClose Cursor_ColumnDeallocate Cursor_Column / * Get index information * / Declare Cursor_Index INSENSITIVE CURSOR for Select name, indID, status from sysindexes where object_name (id) = @ ObjName and indID> 0 and indID <> 255 order by indID / * Added inDid 255 determines * / Open Cursor_IndexFetch Next from Cursor_Index into @ Colname, @indid, @INDSTATUSWHILE (@@ fetch_status <> -1) begin if @@ fetch_status <> -2 begin

Declare @i tinyint declare @thiskey varchar (50) Declare @inddesc varchar (68) / * String to build up index desc in * /

Select @i = 1 while (@i <= 16) begin select @thiskey = index_col (@objname, @indid, @i) if @Thiskey Is Null Break

IF @i = 1 Select @index_key = index_col (@objname, @indid, @i) else select @index_key = @index_key ',' index_col (@objname, @indid, @i) select @i = @i 1 end if (@indstatus & 0x02)> 0 Select @script = 'create unique' else select @script = 'create' if @indid = 1 select @script = @script 'clustered'

if (@IndStatus & 0x800)> 0 select @strPri_Key = 'PRIMARY KEY (' @Index_Key ')' else select @strPri_Key = 'if @IndID> 1 select @Script = @Script ' nonclustered 'Select @Script = @Script 'Index' @coLname 'on' @objname '(' @index_key ')' SELECT @inddesc = '/ * ** See if the index is ignore_dupkey (0x01). * / If @ IndStatus & 0x01 = 0x01 Select @IndDesc = @IndDesc 'IGNORE_DUP_KEY' ',' / * ** See if the index is ignore_dup_row (0x04). * / / * if @IndStatus & 0x04 = 0x04 * / / * Select @ Inddesc = @inddesc 'ignore_dup_row' ',' * / / * 2000 is not supported * / / * ** see if the index is allow_dup_row (0x40). * / If @indstatus & 0x40 = 0x40 select @indDesc = @indDesc 'Allow_dup_row' ',' IF @inddesc <> 'Begin Select @inddesc = Substring (@inddesc, 1, datalength (@inddesc) - 1) SELECT @script = @Script ' with ' @indDesc end / * ** Add The location of the data. * / End if (@strPri_Key = ') Insert into #spscript values ​​(@ Script, 0) else update #spscript set Script = Script @strPri_Key where LastLine = 1 Fetch Next from Cursor_Index into @ColName, @IndID, @IndStatusendClose Cursor_IndexDeallocate Cursor_indexselect script from #SPScript

Set nocount off

Return (0) SQLSERVER 6.5 code

Create Procedure SP_GET_TABLE_INFO @ ObjName Varchar (128) / * The Table To Generate SQL Script * / as

declare @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 smallintddeclare @index_key varchar (255) Declare @segment smallintdeclare @dbname varchar (30) Declare @strpri_key varchar (255)

/ *** Check to see the the Table exists and initialize @objid. * / If not exists (select name from sysobjects where name = @objname) Begin select @dbname = db_name () Raiserror (15009, -1, -1, @ Objname, @ dbname) Return (1) End

Create Table #SPScript (ID Int IDENTIN NOT NULL, SCRIPT VARCHAR (255) Not Null, Lastline Tinyint

Declare Cursor_Column Innsitive Cursor for 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 Case C.Text When "" "" "Else C.Text End from syscomments c where a.cdefault = c.id) End const_key from syscolumns a, systempes b where object_name (A .id) @objname and a.USERTYPE = B.USERTYPE ORDER BY A.COLID

Set NoCount onselect @script = 'create table' @objname '(' Insert Into #spscript Values ​​(@ script, 0)

/ * Get column information * / Open Cursor_Column

fetch next from Cursor_Column into @ ColName, @ ColID, @ UserType, @ TypeName, @ Length, @ Prec, @ Scale, @ Status, @ cDefault, @ Const_KeySelect @Script = 'while (@@ FETCH_STATUS <> -1) begin if (@@ fetch_status <> -2) begin select @Script = @colname '' @Typename if @USERTYPE IN (1, 2, 3, 4) SELECT @Script = @Script '(' Convert (char (char) 3), @ Length) ')' Else if @USERTYPE IN (24) SELECT @Script = @Script '(' Convert (Char (3), @ PREC) ',' Convert (3) , @ Scale) ')' else select @script = @Script '' = @Status & 0x80)> 0 SELECT @Script = @Script 'Identity (1, 1)'

if (@Status & 0x08)> 0 Select @Script = @Script 'NULL' else Select @Script = @Script 'NOT NULL' if @cDefault> 0 Select @Script = @Script 'DEFAULT' @Const_Key end fetch next from Cursor_Column into @ ColName, @ ColID, @ UserType, @ TypeName, @ Length, @ Prec, @ Scale, @ Status, @ cDefault, @ Const_Key if @@ FETCH_STATUS = 0 begin Select @Script = @Script ', 'Insert into #spscript values ​​(@ Script, 0) end else begin Insert into #spscript values ​​(@ Script, 1) Insert into #spscript values ​​(') ', 0) endendClose Cursor_ColumnDeallocate Cursor_Column

/ * Get index information * / Declare Cursor_Index INSENSITIVE CURSOR for Select name, IndID, status, Segment from sysindexes where object_name (id) = @ ObjName and IndID> 0 and IndID <> 255 order by IndIDOpen Cursor_IndexFetch Next from Cursor_Index into @ColName, @IndID, @IndStatus, @Segmentwhile (@@ FETCH_STATUS <> -1) begin if @@ FETCH_STATUS <> -2 begindeclare @i TinyInt declare @thiskey varchar (50) declare @IndDesc varchar (68) / * string to build up INDEX DESC in * /

Select @i = 1 while (@i <= 16) begin select @thiskey = index_col (@objname, @indid, @i) if @Thiskey Is Null Break

IF @i = 1 Select @index_key = index_col (@objname, @indid, @i) else select @index_key = @index_key ',' index_col (@objname, @indid, @i) select @i = @i 1 end if (@indstatus & 0x02)> 0 Select @script = 'create unique' else select @script = 'create' if @indid = 1 select @script = @script 'clustered'

if (@IndStatus & 0x800)> 0 select @strPri_Key = 'PRIMARY KEY (' @Index_Key ')' else select @strPri_Key = 'if @IndID> 1 select @Script = @Script ' nonclustered 'Select @Script = @Script 'Index' @coLname 'on' @objname '(' @index_key ')' SELECT @inddesc = '/ * ** See if the index is ignore_dupkey (0x01). * / If @ IndStatus & 0x01 = 0x01 Select @IndDesc = @IndDesc 'IGNORE_DUP_KEY' ',' / * ** See if the index is ignore_dup_row (0x04). * / if @IndStatus & 0x04 = 0x04 Select @IndDesc = @IndDesc ' IGNORE_DUP_ROW ' ', '/ * ** See if the index is allow_dup_row (0x40). * / if @IndStatus & 0x40 = 0x40 Select @IndDesc = @IndDesc ' aLLOW_DUP_ROW ' ', 'if @IndDesc <>' begin Select @inddesc = Substring (@inddesc, 1, datalength (@inddesc) - 1) SELECT @Script = @Script 'with' @indDesc END / * ** Add The location of the data. * / If @segment < > 1 select @Script = @Script 'ON' name from syssegments where segment = @Segment end if (@strPri_Key = ') Insert into #spscript values ​​(@ Script, 0) else update #spscript set Script = Script @strPri_Key where Lastline = 1 fetch next from cursoor_index @ @ @ @

ColName, @indid, @SIDSTATUS, @segmentendclose cursor_indexdeallocate cursor_indexselect script from #SPScript Order By ID

Set nocount off

Return (0)

转载请注明原文地址:https://www.9cbs.com/read-123863.html

New Post(0)