向高手求教——如何获得一个表的建立语句
请各位高手赐教,如何通过系统视图或者过程来找到表建立的语句,即matadata,就像oracle里的get_dll过程一下
呵呵,用鼠标点击生成脚本之类的回答就没必要了,谢谢
[解决办法]
1.通过在企业管理器中对表使用CTRL+C,然后在对应数据库的中打开查询分析器CTRL+V,可以把表结构生成出来~;
2.利用存储过程
Declare @s varchar(8000)
set @s=''
SELECT @s=@s+
Char(9)+
a.name +
Space(30-len(a.Name))+
Case When b.name in (
'image','text','uniqueidentifier','tinyint',
'smallint','int','smalldatetime','real','money',
'datetime','float','sql_variant','ntext','bit',
'smallmoney','bigint','varbinary'
)
Then b.Name
When b.Name in ('decimal','numeric')
Then b.Name+'('
+Convert(Varchar(10),COLUMNPROPERTY(a.id,a.name,'PRECISION'))+','
+Convert(Varchar(10),isnull(COLUMNPROPERTY(a.id,a.name,'Scale'),0))+')'
Else b.Name+'('+Convert(varchar(10),COLUMNPROPERTY(a.id,a.name,'PRECISION'))+')'
End +
Case When b.Name in ('Varchar','Nvarchar')
Then ' COLLATE SQL_Latin1_General_CP1_CI_AS '
Else ''
End
+Space(2)
+','+Char(10)
FROM syscolumns a left join systypes b
on a.xtype=b.xusertype
inner join sysobjects d
on a.id=d.id --and d.xtype='U'
and d.name<>'dtproperties'
and d.name =@ObjName --表名
left join syscomments e
on a.cdefault=e.id
order by a.id,a.colorder
if @s<>''
set @s=Substring(@s,1,len(@s)-2)
Select 'Create Table '+@ObjName +'('+Char(13)+Char(10)+@s+Char(13)+Char(10)+')'