修改select语句以创建表和插入记录

时间:2014-12-16 16:08:57

标签: sql-server tsql

我有一个TSQL,想要修改select语句来创建表并插入所选命令。

set @query = N'SELECT ' + @cols + N'  **create table tablename (insert into**    
        from 
        (
            select substring, Field_Name,
              rn = row_number() over(partition by field_name order by fieldnumber)
            from bear_crossjoin
        ) x
        pivot 
        (
            max(substring)
            for Field_Name in (' + @cols + N')
        ) p ' 

如何用粗体字母定义一个?

1 个答案:

答案 0 :(得分:0)

怎么样:

set @query = N'SELECT ' + @cols + N' INTO databasename.schemaname.tablename    
        from 
        (
            select substring, Field_Name,
              rn = row_number() over(partition by field_name order by fieldnumber)
            from bear_crossjoin
        ) x
        pivot 
        (
            max(substring)
            for Field_Name in (' + @cols + N')
        ) p '