我有一个场景,我在CSV列表中有条目(这些是动态值)。我需要在sqlserver(MSSQL)
中生成带有这些值的表输入表
Value
FirstName,LastName,SSN
Address1,City,Zip
HomePhone,CellPhone
输出表1
FirstName LastName SSN
输出表2
Address1 City Zip
输出表3
HomePhone CellPhone
有人可以帮助我。
答案 0 :(得分:1)
您需要动态SQL,例如:
declare @sql nvarchar(max);
with t as (
select 'FirstName,LastName,SSN' as value union all
select 'Address1,City,Zip' union all
select 'HomePhone,CellPhone'
)
select @sql = (select 'create table'+CAST(seqnum as varchar(255))+' ('+REPLACE(value, ',', ' varchar(255),') + ' varchar(255)); '
from (select t.*, ROW_NUMBER() over (order by (select null)) as seqnum
from t
) t
for xml path ('')
)
exec sp_executesql @sql;
答案 1 :(得分:0)
试试这个,
Select FirstName,LastName,SSN into Table1 from #InputTable
Select Address1,City,Zip into Table2 from #InputTable
Select HomePhone,CellPhone into Table1 from #InputTable