我编译了一个列列表,我希望用它来构建一个表。是否可以使用过程基于此列表创建表?即
List
A
B
C
New_Table
Column A Column B Column C
答案 0 :(得分:0)
客户可以包含列列表,其中包含行: - A,B,C
我所做的是在运行时将RowNum列添加到客户表中,这是动态的。由于这个行号的另一列附加了List列。使用此: -
根据您的要求,这是程序。请验证: -
create procedure newtable
@tablename NVARCHAR(1000)
as
Declare @col1 varchar(50),@col2 varchar(50),@col3 varchar(50), @cmd NVARCHAR(1000)
select @col1=List from (select List,ROW_NUMBER() Over(Order by List) as RowNum from customer)x where x.RowNum=1
select @col2=List from (select List,ROW_NUMBER() Over(Order by List) as RowNum from customer)x where x.RowNum=2
select @col3=List from (select List,ROW_NUMBER() Over(Order by List) as RowNum from customer)x where x.RowNum=3
set @cmd=N'Create Table '+@tablename+ N' (' + @col1+N' Varchar(50),'+@col2 + N' varchar(50),'+@col3+N' varchar(50))' ;
print @cmd
Exec(@cmd)
在此之后运行您的命令: -
exec newtable 'New_table'