如何将查询结果保存到表中?

时间:2019-04-04 17:01:37

标签: sql

我需要将此代码的结果保存到单独的表中,该代码只是一个具有两列和十二行的表。如果我在末尾使用INTO子句,则会收到错误消息“ INTO附近的语法不正确”。我不确定这是什么意思,因为我在代码中间使用了相同的命令而没有任何问题。 INTO子句(不起作用)是下面代码的一部分。

-- The code below generates a table that marks the last 12 months based on the value for @querydate1
        DECLARE @queryDate1 datetime SET @queryDate1 = '03/30/2018';
        DECLARE @queryDate2 datetime set @queryDate2 = EOMONTH(@queryDate1,-1);
        DECLARE @queryDate3 datetime SET @queryDate3 = EOMONTH(@queryDate1,-2);
        DECLARE @queryDate4 datetime set @queryDate4 = EOMONTH(@queryDate1,-3);
        DECLARE @queryDate5 datetime SET @queryDate5 = EOMONTH(@queryDate1,-4);
        DECLARE @queryDate6 datetime set @queryDate6 = EOMONTH(@queryDate1,-5);
        DECLARE @queryDate7 datetime SET @queryDate7 = EOMONTH(@queryDate1,-6);
        DECLARE @queryDate8 datetime set @queryDate8 = EOMONTH(@queryDate1,-7);
        DECLARE @queryDate9 datetime SET @queryDate9 = EOMONTH(@queryDate1,-8);
        DECLARE @queryDate10 datetime set @queryDate10 = EOMONTH(@queryDate1,-9);
        DECLARE @queryDate11 datetime SET @queryDate11 = EOMONTH(@queryDate1,-10);
        DECLARE @queryDate12 datetime set @queryDate12 = EOMONTH(@queryDate1,-11);

        DROP TABLE GA_Financial.dbo.tmpTblTTM
        SELECT
             convert(varchar(10),@queryDate1,101) as [M1]
            ,convert(varchar(10),@queryDate2,101) as [M2]
            ,convert(varchar(10),@queryDate3,101) as [M3]
            ,convert(varchar(10),@queryDate4,101) as [M4]
            ,convert(varchar(10),@queryDate5,101) as [M5]
            ,convert(varchar(10),@queryDate6,101) as [M6]
            ,convert(varchar(10),@queryDate7,101) as [M7]
            ,convert(varchar(10),@queryDate8,101) as [M8]
            ,convert(varchar(10),@queryDate9,101) as [M9]
            ,convert(varchar(10),@queryDate10,101) as [M10]
            ,convert(varchar(10),@queryDate11,101) as [M11]
            ,convert(varchar(10),@queryDate12,101) as [M12]
        INTO ga_financial.dbo.tmpTblTTM

SELECT
            'M1' AS [MX], M1 AS [Date] FROM ga_financial.dbo.tmpTblTTM
            UNION ALL SELECT 'M2', M2 AS [Date] FROM ga_financial.dbo.tmpTblTTM
            UNION ALL SELECT 'M3', M3 AS [Date] FROM ga_financial.dbo.tmpTblTTM
            UNION ALL SELECT 'M4', M4 AS [Date] FROM ga_financial.dbo.tmpTblTTM
            UNION ALL SELECT 'M5', M5 AS [Date] FROM ga_financial.dbo.tmpTblTTM
            UNION ALL SELECT 'M6', M6 AS [Date] FROM ga_financial.dbo.tmpTblTTM
            UNION ALL SELECT 'M7', M7 AS [Date] FROM ga_financial.dbo.tmpTblTTM
            UNION ALL SELECT 'M8', M8 AS [Date] FROM ga_financial.dbo.tmpTblTTM
            UNION ALL SELECT 'M9', M9 AS [Date] FROM ga_financial.dbo.tmpTblTTM
            UNION ALL SELECT 'M10', M10 AS [Date] FROM ga_financial.dbo.tmpTblTTM
            UNION ALL SELECT 'M11', M11 AS [Date] FROM ga_financial.dbo.tmpTblTTM
            UNION ALL SELECT 'M12', M12 AS [Date] FROM ga_financial.dbo.tmpTblTTM
        INTO ga_financial.dbo.tmpTblTTM2

1 个答案:

答案 0 :(得分:2)

union all / union查询中,into在第一个select之后:

SELECT 'M1' AS [MX], M1 AS [Date]
INTO ga_financial.dbo.tmpTblTTM2
FROM ga_financial.dbo.tmpTblTTM
        UNION ALL SELECT 'M2', M2 AS [Date] FROM ga_financial.dbo.tmpTblTTM
        UNION ALL SELECT 'M3', M3 AS [Date] FROM ga_financial.dbo.tmpTblTTM
        UNION ALL SELECT 'M4', M4 AS [Date] FROM ga_financial.dbo.tmpTblTTM
        UNION ALL SELECT 'M5', M5 AS [Date] FROM ga_financial.dbo.tmpTblTTM
        UNION ALL SELECT 'M6', M6 AS [Date] FROM ga_financial.dbo.tmpTblTTM
        UNION ALL SELECT 'M7', M7 AS [Date] FROM ga_financial.dbo.tmpTblTTM
        UNION ALL SELECT 'M8', M8 AS [Date] FROM ga_financial.dbo.tmpTblTTM
        UNION ALL SELECT 'M9', M9 AS [Date] FROM ga_financial.dbo.tmpTblTTM
        UNION ALL SELECT 'M10', M10 AS [Date] FROM ga_financial.dbo.tmpTblTTM
        UNION ALL SELECT 'M11', M11 AS [Date] FROM ga_financial.dbo.tmpTblTTM
        UNION ALL SELECT 'M12', M12 AS [Date] FROM ga_financial.dbo.tmpTblTTM