日期:2014-05-18  浏览次数:20460 次

如何用代码批量生成表结构?
本人做了一个批量生成PROC和VIEW的数据库管理工具,现在有想完善一下,想加一个批量生成表结构的功能,有没有成熟的东西?当然是用我的工具不用客户端.

------解决方案--------------------
CREATE PROC SP_Objitems
@ObjName nvarchar(40) = 'Skyobjitems ',
@Type char(1) = ' '
AS
/*
@Type : ' ' 代表仅是查询;非空值代表生成在异机上可执行的更新脚本
*/

Declare @SQL nvarchar(2000)
Select @SQL = 'Select * from skyobjitems where objname = ' ' '+@ObjName+ ' ' ' order by xorder '

IF @Type = ' '
Begin
EXEC (@SQL)
End
Else
Begin
SET NOCOUNT ON
Select 'Declare @id int Select @id = id from sysobjects where name = ' ' '+@ObjName + ' ' '
'
Select 'Delete From skyobjItems Where Objname = ' ' '+@ObjName + ' ' '
'
Select 'INSERT INTO skyobjitems ([ObjID],[ObjName],[xColumn],[Title],[Location],[System],[Property],[Permission],
[RowSource],[xPKey],[xDataType],[DataType],[DataWidth],[Width],[Presentation],[Validate],[Translate],
[BackColor],[ForeColor],[NumberFormat],[EditMask],[xOrder],[MaxValue],[MinValue],[Locked],[Invisible],
[Bar],[InputSource],[Alignment],[FooterText],[xIdentity],[Link],[refkey],[RefObj],[comments],[Nullable],
[notnull],[xcolumnType],[webWidth],[webLineFeed])
VALUES ( '
+ '@ID '+ ', '
+case when [ObjName] is null then 'NULL ' else 'N ' ' '+replace([ObjName], ' ' ' ', ' ' ' ' ' ')+ ' ' ' ' end+ ', '
+case when [xColumn] is null then 'NULL ' else 'N ' ' '+replace([xColumn], ' ' ' ', ' ' ' ' ' ')+ ' ' ' ' end+ ', '
+case when [Title] is null then 'NULL ' else 'N ' ' '+replace([Title], ' ' ' ', ' ' ' ' ' ')+ ' ' ' ' end+ ', '
+case when [Location] is null then 'NULL ' else 'N ' ' '+replace([Location], ' ' ' ', ' ' ' ' ' ')+ ' ' ' ' end+ ', '
+case when [System] is null then 'NULL ' else convert(varchar(12),[System]) end+ ', '
+case when [Property] is null then 'NULL ' else convert(varchar(12),[Property]) end+ ', '
+case when [Permission] is null then 'NULL ' else convert(varchar(12),[Permission]) end+ ', '
+case when [RowSource] is null then 'NULL ' else 'N ' ' '+replace([RowSource], ' ' ' ', ' ' ' ' ' ')+ ' ' ' ' end+ ', '
+case when [xPKey] is null then 'NULL ' else convert(varchar(1),[xPKey]) end+ ', '
+case when [xDataType] is null then 'NULL ' else convert(varchar(12),[xDataType]) end+ ', '
+case when [DataType] is null then 'NULL ' else convert(varchar(6),[DataType]) end+ ', '
+case when [DataWidth] is null then 'NULL ' else convert(varchar(12),[DataWidth]) end+ ', '
+case when [Width] is null then 'NULL ' else convert(varchar(12),[Width]) end+ ', '
+case when [Presentation] is null then 'NULL ' else convert(varchar(6),[Presentation]) end+ ', '
+case when [Validate] is null then 'NULL ' else convert(varchar(1),[Validate]) end+ ', '
+case when [Translate] is null then 'NULL ' else convert(varchar(1),[Translate]) end+ ', '
+case when [BackColor] is null then 'NULL ' else convert(varchar(11),[BackColor]) en