How to get a table creation statement in MS SQL Server

Source: Internet
Author: User
Tags insert key sql
server|sqlserver| Create | statement
MS SQL Server can only get the creation statements for stored procedures as follows:

Sp_helptext procedurename

But often we need to get a table creation statement, for example, when the database upgrade to determine whether a table has changed, or already have a table exists, but do not know what it is the creation of statements, the field has no constraints, there is no primary key, what indexes are created, and so on. Below I give a stored procedure for reader's reference.

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

Code under the 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 TinyInt
DECLARE @UserType smallint
declare @TypeName sysname
DECLARE @Length TinyInt
DECLARE @Prec TinyInt
DECLARE @Scale TinyInt
DECLARE @Status TinyInt
DECLARE @cDefault int
DECLARE @DefaultID TinyInt
DECLARE @Const_Key varchar (255)
DECLARE @IndID SmallInt
DECLARE @IndStatus Int
DECLARE @Index_Key varchar (255)
DECLARE @DBName varchar (30)
DECLARE @strPri_Key varchar (255)

/*
* * Check to 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 IDENTITY NOT NULL,
Script Varchar (255) Not NULL,
LastLine tinyint
)

DECLARE cursor_column insensitive 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 to 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 ON
Select @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_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 (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)
End
End
Close Cursor_column
Deallocate Cursor_column

/* Get index information * *
Declare Cursor_index insensitive Cursor
For the Select name,indid,status from sysindexes where object_name (id) = @ObjName
and indid > 0 and indid<>255 Order by indid/* Increased the judgment of indid 255.
Open Cursor_index
Fetch Next from Cursor_index into @ColName, @IndID, @IndStatus
while (@ @FETCH_STATUS <>-1)
Begin
If @ @FETCH_STATUS <>-2
Begin

DECLARE @i TinyInt
DECLARE @thiskey varchar (50)
DECLARE @IndDesc varchar/* 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 = '
/*
* * The If the index is Ignore_dupkey (0x01).
*/
If @IndStatus & 0x01 = 0x01
Select @IndDesc = @IndDesc + ' ignore_dup_key ' + ', '
/*
* * The If the index is Ignore_dup_row (0x04).
*/
/* If @IndStatus & 0x04 = 0x04 * *
/* Select @IndDesc = @IndDesc + ' ignore_dup_row ' + ', '/* 2000 not supported * *
/*
* * The 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, @IndStatus
End
Close Cursor_index
Deallocate Cursor_index

Select Script from #spscript

SET NOCOUNT OFF

Return (0)


Code under the SQLSERVER6.5

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 TinyInt
DECLARE @UserType smallint
declare @TypeName sysname
DECLARE @Length TinyInt
DECLARE @Prec TinyInt
DECLARE @Scale TinyInt
DECLARE @Status TinyInt
DECLARE @cDefault int
DECLARE @DefaultID TinyInt
DECLARE @Const_Key varchar (255)
DECLARE @IndID SmallInt
DECLARE @IndStatus SmallInt
DECLARE @Index_Key varchar (255)
DECLARE @Segment SmallInt
DECLARE @DBName varchar (30)
DECLARE @strPri_Key varchar (255)

/*
* * Check to 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 IDENTITY NOT NULL,
Script Varchar (255) Not NULL,
LastLine tinyint
)

DECLARE cursor_column insensitive Cursor
For Select A.name,a.colid,a.usertype,b.name,a.length,a.prec,a.scale,a.status, A.cdefault,
Case A.cdefault If 0 Then ' else (Select Case C.text when "(") "Then" (")" Else C.text end
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 ON
Select @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_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 (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)
End
End
Close Cursor_column
Deallocate Cursor_column

/* Get index information * *
Declare Cursor_index insensitive Cursor
For the Select name,indid,status,segment from sysindexes where object_name (id) = @ObjName
and indid > 0 and indid<>255 Order by indid
Open Cursor_index
Fetch Next from Cursor_index into @ColName, @IndID, @IndStatus, @Segment
while (@ @FETCH_STATUS <>-1)
Begin
If @ @FETCH_STATUS <>-2
Begin

DECLARE @i TinyInt
DECLARE @thiskey varchar (50)
DECLARE @IndDesc varchar/* 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 = '
/*
* * The If the index is Ignore_dupkey (0x01).
*/
If @IndStatus & 0x01 = 0x01
Select @IndDesc = @IndDesc + ' ignore_dup_key ' + ', '
/*
* * The If the index is Ignore_dup_row (0x04).
*/
If @IndStatus & 0x04 = 0x04
Select @IndDesc = @IndDesc + ' ignore_dup_row ' + ', '
/*
* * The 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 Cursor_index into @ColName, @IndID, @IndStatus, @Segment
End
Close Cursor_index
Deallocate Cursor_index

Select Script from #spscript ORDER by ID

SET NOCOUNT OFF

Return (0)





Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.