将复杂查询结果移动到表

时间:2019-05-04 12:57:38

标签: sql sql-server

我有一个复杂的查询,由于查询开始时有DECLARE语句而无法另存为视图。

我想将查询结果保存到表中。

我试图插入以下语句来包围整个查询,并且也只围绕SET @sqlEXEC(@sql)

两者都导致了我无法解决的错误。

这是产生我需要的结果的查询:

DECLARE @sql VARCHAR(MAX)
DECLARE @colList VARCHAR(MAX)

--create dynamic list of columns
SELECT @colList = STUFF((SELECT + ',' + QUOTENAME(colName +  CAST(CAST(Billing_Order AS INTEGER) AS VARCHAR))
                         FROM Credible_Client_Insurance_Raw_Data
                         CROSS APPLY
                             (SELECT 1 As Ord, 'Payer_ID' ColName 
                              UNION ALL
                              SELECT 2 As Ord, 'Billing_Order' 
                              UNION ALL
                              SELECT 3 As Ord, 'Insurance_ID' 
                              UNION ALL
                              SELECT 4 As Ord, 'Group_No' 
                              UNION ALL
                              SELECT 5 As Ord, 'Copay_Fee' 
                              UNION ALL
                              SELECT 6 As Ord, 'Start_Date') v 
                          GROUP BY colName, Ord, CAST(CAST(Billing_Order AS INTEGER) AS VARCHAR)
                          ORDER BY CAST(CAST(Billing_Order AS INTEGER) AS VARCHAR), Ord
                          FOR XML PAT(''), TYPE).value('/', 'VARCHAR(MAX)'), 1, 1, '')

--unpivot columns into rows and then apply pivot
SET @sql = 'SELECT Client_ID, ' + @colList + ' 
            FROM 
                (SELECT  
                     Client_ID, ColVal,
                     colName + CAST(CAST(Billing_Order AS INTEGER) AS VARCHAR) ColName
                 FROM Credible_Client_Insurance_Raw_Data
                 CROSS APPLY
   (
     SELECT Payer_ID As ColVal, ''Payer_ID'' ColName UNION ALL
     SELECT Billing_Order, ''Billing_Order'' UNION ALL
     SELECT Insurance_ID, ''Insurance_ID'' UNION ALL
     SELECT Group_No, ''Group_No'' UNION ALL
     SELECT CAST(Copay_Fee AS VARCHAR), ''Copay_Fee'' UNION ALL
     SELECT CAST(Start_Date AS VARCHAR), ''Start_Date'' 
   ) v 
) A
PIVOT
(
   MAX(ColVal) FOR ColName IN (' + @colList + ')
) P1 '


EXEC(@sql)

我只想将查询的结果集插入到永久表Credible_Client_Insurance_Data中。

有什么方法可以执行

INSERT INTO Credible_Client_Insurance_Data (Client_ID, etc.)
    SELECT Client_ID, etc.
    FROM query_results  

还是将某种类型的数据插入表中?

1 个答案:

答案 0 :(得分:0)

IF 我的理解正确,不是您只需要将@sql的值更改为:

SET @sql = '
INSERT INTO Credible_Client_Insurance_Data (' + @colList + ')
SELECT Client_ID, ' + @colList + ' 
FROM
(
   SELECT Client_ID, ColVal,
   colName +  CAST(CAST(Billing_Order AS INTEGER) AS VARCHAR) ColName
   FROM Credible_Client_Insurance_Raw_Data
   CROSS APPLY
   (
     SELECT Payer_ID As ColVal, ''Payer_ID'' ColName UNION ALL
     SELECT Billing_Order, ''Billing_Order'' UNION ALL
     SELECT Insurance_ID, ''Insurance_ID'' UNION ALL
     SELECT Group_No, ''Group_No'' UNION ALL
     SELECT CAST(Copay_Fee AS VARCHAR), ''Copay_Fee'' UNION ALL
     SELECT CAST(Start_Date AS VARCHAR), ''Start_Date'' 
   ) v 
) A
PIVOT
(
   MAX(ColVal) FOR ColName IN (' + @colList + ')
) P1 ;';