USE [master]
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
/*|<PRE>
****************************************************************************
软件名称: May Flower Erp
版权所有: (C) 2005-2006 May Flower ERP 开发组
对象名称: sp_HelpTable
对象作者: lvqy
创建日期: 2005-12-04
修改记录:
功能描述: 获取指定表的创建脚本,包括表和字段的属性、外键(注释掉的)
----------------------------------------------------------------------------
参数列表:
1: @TableName 需要创建脚本的表的名称
****************************************************************************
|</PRE>*/
CREATE PROCEDURE [dbo].[sp_HelpTable](@TableName sysname)
AS
SET NOCOUNT ON
DECLARE @ObjectID int
DECLARE @TableScript table(Iden int IDENTITY(1, 1), ScriptLine nvarchar(4000))
SET @ObjectID = object_id(@TableName)
IF @ObjectID IS NULL OR OBJECTPROPERTY(@ObjectID, 'IsTable') = 0
BEGIN
RAISERROR('指定的对象不是表对象', 16, 1)
RETURN
END
--获取表的创建脚本
--插入表头
INSERT INTO @TableScript(ScriptLine)
SELECT N'CREATE TABLE [' + USER_NAME(OBJECTPROPERTY(@ObjectID, N'OwnerId')) + N'].[' + object_name(@ObjectID) + N']('
--插入字段
INSERT INTO @TableScript(ScriptLine)
SELECT N' [' + a.Name + N'] [' + b.name + N']' +
CASE WHEN c.Object_id IS NOT NULL THEN N' IDENTITY(' + CONVERT(nvarchar, c.seed_value) + N', ' + CONVERT(nvarchar, c.increment_value) + N')'
ELSE '' END +
CASE WHEN b.xusertype IN (167, 175, 231, 239) THEN N'('+CONVERT(nvarchar, a.prec) + N')'
WHEN b.xusertype in (106, 108) THEN N'('+CONVERT(nvarchar, a.xprec) + N', ' + CONVERT(nvarchar, a.xscale) + N')'
ELSE '' END +
CASE a.isnullable WHEN 1 THEN N'' ELSE N' NOT' END + N' NULL' +
CASE WHEN d.Name IS NOT NULL THEN N' DEFAULT ' + d.Definition ELSE N'' END +
N','
FROM sys.syscolumns a
LEFT JOIN sys.systypes b ON a.xusertype = b.xusertype
LEFT JOIN sys.identity_columns c ON c.Object_id = a.ID AND c.Column_ID = a.ColID
LEFT JOIN sys.default_constraints d ON d.Parent_Object_ID = a.ID AND d.Parent_column_ID = a.ColID
WHERE a.[ID] = @ObjectID
ORDER BY a.ColOrder
--插入主键和索引
DECLARE @IndexID int, @IndexScript nvarchar(4000)
DECLARE IndexCursor CURSOR FOR
SELECT b.Index_ID, N' CONSTRAINT [' + a.Name + N'] ' +
CASE a.Type WHEN 'PK' THEN N'PRIMARY KEY ' WHEN 'UQ' THEN N'UNIQUE ' END +
CASE b.Type WHEN 1 THEN N'CLUSTERED' WHEN 2 THEN N'NONCLUSTERED ' END + N'('
FROM sys.key_constraints a
LEFT JOIN sys.indexes b ON b.Object_ID = a.Parent_Object_ID AND b.index_id = a.unique_index_id
WHERE a.Parent_Object_ID = @ObjectID
OPEN IndexCursor
FETCH NEXT FROM IndexCursor INTO @IndexID, @IndexScript
WHILE @@FETCH_STATUS = 0
BEGIN
SELECT @IndexScript = @IndexScript + N'[' + INDEX_COL(object_name(@ObjectID), 2 , 1) + N'],'
FROM sys.index_columns
WHERE Object_ID = @ObjectID
AND Index_ID = 2
SET @IndexScript = LEFT(@IndexScript, LEN(@IndexScript) -1) + N'),'
INSERT INTO @TableScript(ScriptLine) VALUES(@IndexScript)
FETCH NEXT FROM IndexCursor INTO @IndexID, @IndexScript
END
CLOSE IndexCursor
DEALLOCATE IndexCursor
--除去最后一个,号
UPDATE @TableScript
SET ScriptLine = LEFT(ScriptLine, LEN(ScriptLine) - 1)
WHERE Iden = (SELECT MAX(Iden) FROM @TableScript)
INSERT INTO @TableScript(ScriptLine) VALUES(N')')
INSERT INTO @TableScript(ScriptLine) VALUES(N'GO')
INSERT INTO @TableScript(ScriptLine) VALUES(N'')
--获取表备注
DECLARE @PropScript nvarchar(4000)
INSERT INTO @TableScript(ScriptLine)
SELECT N'EXEC sys.sp_addextendedproperty @name=N''' + a.Name + N''', @value=N''' + CONVERT(nvarchar, a.Value) + N''' ,@level0type=N''SCHEMA'', @level0name=N''' +
USER_NAME(OBJECTPROPERTY(a.major_Id, N'OwnerId')) + N''', @level1type=N''TABLE'', @level1name=N''' + b.Name + N''''
FROM sys.extended_properties a
LEFT JOIN sys.objects b ON b.[Object_ID] = a.major_Id
WHERE a.major_Id = @ObjectID
AND Minor_ID = 0
INSERT INTO @TableScript(ScriptLine) VALUES('GO')
DECLARE PropCursor CURSOR FOR
SELECT N'EXEC sys.sp_addextendedproperty @name=N''' + a.Name + N''', @value=N''' + CONVERT(nvarchar, a.Value) + N''' ,@level0type=N''SCHEMA'', @level0name=N''' +
USER_NAME(OBJECTPROPERTY(a.major_Id, N'OwnerId')) + N''', @level1type=N''TABLE'', @level1name=N''' + b.Name + N'''' +
N', @level2type=N''COLUMN'', @level2name=N''' + c.[Name] + ''''
FROM sys.extended_properties a
LEFT JOIN sys.objects b ON b.[Object_ID] = a.major_Id
LEFT JOIN sys.syscolumns c ON c.[ID] = a.major_Id AND c.ColID = a.Minor_ID
WHERE a.major_Id = @ObjectID
AND Minor_ID <> 0
OPEN PropCursor
FETCH NEXT FROM PropCursor INTO @PropScript
WHILE @@FETCH_STATUS = 0
BEGIN
INSERT INTO @TableScript(ScriptLine) VALUES(@PropScript)
INSERT INTO @TableScript(ScriptLine) VALUES(N'GO')
FETCH NEXT FROM PropCursor INTO @PropScript
END
CLOSE PropCursor
DEALLOCATE PropCursor
INSERT INTO @TableScript(ScriptLine) VALUES('')
--获取表外键
DECLARE @ConstID int, @i tinyint, @keyCnt tinyint, @TempletSQL nvarchar(400), @SQLScript nvarchar(500),
@FColName sysname, @RColName sysname,
@ForeignLine nvarchar(4000), @ReferencesLine nvarchar(4000), @ReferencesAction nvarchar(4000)
DECLARE @ConstIDTable table(ConstID int)
SELECT
@FColName = '',
@RColName = '',
@TempletSQL =
N'SELECT @eFColName = ''['' + col_name(FkeyID, Fkey%d) + '']'', @eRColName = ''['' + col_name(RkeyID, Rkey%d) + '']'' FROM sys.sysreferences WHERE ConstID = @ConstID'
INSERT INTO @ConstIDTable
SELECT ConstID FROM sys.sysreferences WHERE FKeyID = @ObjectID OR RKeyID = @ObjectID ORDER BY FKeyID
WHILE EXISTS(SELECT * FROM @ConstIDTable)
BEGIN
SELECT TOP 1 @ConstID = ConstID FROM @ConstIDTable
DELETE FROM @ConstIDTable WHERE ConstID = @ConstID
INSERT INTO @TableScript
SELECT N'--ALTER TABLE [dbo].[' + object_name(FKeyID) + '] WITH CHECK'
FROM sys.sysreferences
WHERE ConstID = @ConstID
INSERT INTO @TableScript(ScriptLine) VALUES('-- ADD' + CHAR(13) + CHAR(10))
SELECT @ForeignLine = N'-- CONSTRAINT [' + object_name(ConstID) + '] FOREIGN KEY(',
@ReferencesLine = N'REFERENCES [dbo].[' + object_name(RKeyID) + '] (',
@ReferencesAction = CASE b.Delete_Referential_Action WHEN 0 THEN N''
WHEN 1 THEN N'ON DELETE Cascade'
WHEN 2 THEN N'ON DELETE SET NULL'
WHEN 3 THEN N'ON DELETE SET DEFAULT'
END + ' ' +
CASE b.Delete_Referential_Action WHEN 0 THEN N''
WHEN 1 THEN N'ON UPDATE Cascade'
WHEN 2 THEN N'ON UPDATE SET NULL'
WHEN 3 THEN N'ON UPDATE SET DEFAULT'
END,
@keyCnt = KeyCnt
FROM sys.sysreferences a
LEFT JOIN sys.foreign_keys b ON a.ConstID = b.Object_ID
WHERE a.ConstID = @ConstID
--取字段
SET @i = 1
WHILE @i <= @keyCnt
BEGIN
SET @SQLScript = REPLACE(@TempletSQL, '%d', CONVERT(nvarchar, @i))
EXEC sp_executesql @stmt = @SQLScript, @params= N'@eFColName sysname output, @eRColName sysname output, @ConstID int',
@eFColName = @FColName output, @eRColName = @RColName output, @ConstID = @ConstID
print @SQLScript
SET @ForeignLine = @ForeignLine + CASE WHEN @i > 1 THEN ', ' ELSE '' END + @FColName
SET @ReferencesLine = @ReferencesLine + CASE WHEN @i > 1 THEN ', ' ELSE '' END + @RColName
SET @i = @i + 1
END
INSERT INTO @TableScript(ScriptLine) VALUES(@ForeignLine + N') ' + @ReferencesLine + N')')
IF @ReferencesAction <> ''
INSERT INTO @TableScript(ScriptLine) VALUES(@ReferencesAction)
INSERT INTO @TableScript(ScriptLine) VALUES(N'GO')
END
--返回表的创建脚本
SELECT ScriptLine FROM @TableScript
SET NOCOUNT OFF
[sql server] 获取指定表的创建脚本,包括表和字段的属性、外键(注释掉的)
最新推荐文章于 2021-04-19 11:10:58 发布