0

我有一个场景,我在带有 CSV 的表的列中有条目(这些是动态值)。我需要在 sqlserver(MSSQL) 中使用这些值生成表

输入表

Value   
FirstName,LastName,SSN  
Address1,City,Zip
HomePhone,CellPhone

输出表1

FirstName   LastName    SSN

输出表2

Address1    City    Zip

输出表3

HomePhone   CellPhone

有人可以帮帮我吗。

4

2 回答 2

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;
于 2013-04-15T19:40:33.770 回答
0

Try This,

  1. Select FirstName,LastName,SSN into Table1 from #InputTable
  2. Select Address1,City,Zip into Table2 from #InputTable
  3. Select HomePhone,CellPhone into Table1 from #InputTable
于 2013-04-15T19:30:50.633 回答