I have a table with the following structure :
我有一个具有以下结构的表:
TableNo1
Field1
row1
row2
row3
row4
row5
...
...
...
row n
Now I need to create a new table with the following schema:
现在我需要使用以下模式创建一个新表:
TableNo2
Field1(row1 of Table1) Field2(row2 of Table1) Field3(row3 of table1) Fieldn(row n of table1)
Field1(Table1的第1行)Field2(Table1的第2行)Field3(table1的第3行)Fieldn(table1的第n行)
I read about this but only thing i was able to find is the into clause which doesnt work.
我读到了这一点,但我能找到的唯一的东西是不起作用的into子句。
Can anyone please help?
有人可以帮忙吗?
1 个解决方案
#1
3
You can use dynamic SQL
您可以使用动态SQL
DECLARE @TableNo1 TABLE(Field varchar(128),DataType varchar(128))
DECLARE @s nvarchar(max)='CREATE TABLE dbo.TableNo2('
INSERT INTO @TableNo1
VALUES
('Field1','nvarchar(max)'),
('Field2','int')
SELECT @s+=T.Field+' '+T.DataType+',' FROM @TableNo1 T
SET @s=LEFT(@s,LEN(@s)-1)+')'
EXECUTE(@s)
#1
3
You can use dynamic SQL
您可以使用动态SQL
DECLARE @TableNo1 TABLE(Field varchar(128),DataType varchar(128))
DECLARE @s nvarchar(max)='CREATE TABLE dbo.TableNo2('
INSERT INTO @TableNo1
VALUES
('Field1','nvarchar(max)'),
('Field2','int')
SELECT @s+=T.Field+' '+T.DataType+',' FROM @TableNo1 T
SET @s=LEFT(@s,LEN(@s)-1)+')'
EXECUTE(@s)