日期:2014-05-17 浏览次数:20546 次
--当用以下代码创建一个表后,如何根据表名查询出创建该表的代码(也就是以下代码)?
CREATE TABLE [dbo].[a1](
[c2] [decimal](10, 2) NULL,
[c3] [decimal](10, 3) NULL CONSTRAINT [DF_a1_c3] DEFAULT ((0)),
[re] [bigint] IDENTITY(1,1) NOT NULL,
CONSTRAINT [PK_a1] PRIMARY KEY CLUSTERED
(
[re] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY]
declare @sql varchar(8000),@tablename varchar(100)
set @tablename ='a1'--这里输入表名
set @sql = 'create table ['+@tablename+']
(
'
select @sql = @sql + b.name + ' '+
c.name+
case when c.collation_name is not null then '('+
case when b.max_length <>-1 then convert(varchar(100),b.max_length)
else 'MAX'
end +') '
else ''
end +
case when b.is_identity = 1 then ' identity('+convert(varchar(100),IDENT_SEED(@tablename))+','+convert(varchar(100),IDENT_INCR(@tablename))+')' else '' end +
case when d.definition is not null then ' default('+d.definition +')' else '' end +
case when b.is_nullable = 0 then ' not null' else ' null' end +
',
'
from sys.objects a join sys.columns b
on a.object_id = b.object_id
join sys.types c
on b.system_type_id = c.system_type_id and b.user_type_id = c.user_type_id
left join sys.default_constraints d
on b.default_object_id = d.object_id
where a.name=@tablename
order by b.column_id
if exists(select * from sys.indexes where object_id =object_id(@tablename) and is_primary_key =1 )
begin
sele