日期:2014-05-16 浏览次数:20498 次
Declare @TableName Varchar(50), @Columns Varchar(5000), @Sql Varchar(500), @Sql1 Varchar(500)
Declare Cur_Table Cursor for
Select Name From sysObjects Where Xtype = 'U' --And Name = @TableName
Order By Name
Open Cur_Table
Fetch Next From Cur_Table Into @TableName
While @@Fetch_Status = 0
Begin
Set @Sql = 'Create Table '+@TableName + ' (' + char(9)+char(10)
Set @Sql1 = ''
Declare Cur_Column Cursor For
Select Name
+ ' '+ Case xusertype When 167 Then type_name(xusertype)+'('+Cast(Length As Varchar(10))+')'
When 175 Then type_name(xusertype)+'('+Cast(Length As Varchar(10))+')' Else type_name(xusertype) End
+ ' '+Case when isnullable = 0 then 'Not Null' else '' end
+ ','
from SysColumns
where id = Object_ID(@TableName) and number = 0
order by colid
Open Cur_Column;
Fetch Next From Cur_Column Into @Columns;
While @@Fetch_Status = 0
Begin
Set @Sql1 = @Sql1 + @Columns +char(9)+char(10);
Fetch Next From Cur_Column Into @Columns;
End;
Close Cur_Column
Deallocate Cur_Column
Print @Sql + Left(Rtrim(@Sql1), Len(@Sql1)- 3)+')' +Char(9)+Char(10);
Fetch Next From Cur_Table Into @TableName
End
Close Cur_Table
Deallocate Cur_Table