将查询结果插入临时表

时间:2019-06-10 12:14:36

标签: sql-server

将动态查询结果插入到临时表中

尝试创建临时表并将其插入

DECLARE @Currentday AS int;

SET @Currentday = CAST(REPLACE(CAST(GETDATE() AS date), '-', '') AS int);

IF @Currentday BETWEEN CAST(REPLACE(CAST(DATEADD(MONTH, DATEDIFF(MONTH, 0, GETDATE()), 0) AS date), '-', '') AS int) AND CAST(REPLACE(CAST(DATEADD(MONTH, DATEDIFF(MONTH, 0, GETDATE()), 1) AS date), '-', '') AS int)
    (SELECT ASAT,
            CIF,
            SUM(ACCOUNT_BALANCE) AS ACCOUNT_BALANCE
     INTO #SKST1
     FROM [COBG-BIU].dbo.MD_CARD_ENR_FAB
     WHERE CIF IN (SELECT CIF
                   FROM [COBG-BIU].dbo.MD_CARD_ENR_FAB
                   WHERE SPENDERTYPE = 'Revolver'
                     AND ASAT IN (CAST(CONVERT(varchar(6), GETDATE(), 112) AS int) - 3, CAST(CONVERT(varchar(6), GETDATE(), 112) AS int) - 2, CAST(CONVERT(varchar(6), GETDATE(), 112) AS int) - 4)
                     AND ACCOUNT_BALANCE > 0
                   GROUP BY CIF
                   HAVING COUNT(CIF) = 3)
       AND ASAT IN (CAST(CONVERT(varchar(6), GETDATE(), 112) AS int) - 3, CAST(CONVERT(varchar(6), GETDATE(), 112) AS int) - 2, CAST(CONVERT(varchar(6), GETDATE(), 112) AS int) - 4)
     GROUP BY ASAT,
              CIF);
ELSE
    (SELECT ASAT,
            CIF,
            SUM(ACCOUNT_BALANCE) AS ACCOUNT_BALANCE
     INTO ##SKST1
     FROM [COBG-BIU].dbo.MD_CARD_ENR_FAB
     WHERE CIF IN (SELECT CIF
                   FROM [COBG-BIU].dbo.MD_CARD_ENR_FAB
                   WHERE SPENDERTYPE = 'Revolver'
                     AND ASAT IN (CAST(CONVERT(varchar(6), GETDATE(), 112) AS int) - 3, CAST(CONVERT(varchar(6), GETDATE(), 112) AS int) - 2, CAST(CONVERT(varchar(6), GETDATE(), 112) AS int) - 1)
                     AND ACCOUNT_BALANCE > 0
                   GROUP BY CIF
                   HAVING COUNT(CIF) = 3)
       AND ASAT IN (CAST(CONVERT(varchar(6), GETDATE(), 112) AS int) - 3, CAST(CONVERT(varchar(6), GETDATE(), 112) AS int) - 2, CAST(CONVERT(varchar(6), GETDATE(), 112) AS int) - 1)
     GROUP BY ASAT,
              CIF);

要插入到临时表中的查询结果

1 个答案:

答案 0 :(得分:2)

出现错误的原因是因为它是编译错误。即使仅运行这些DDL语句中的1个(或更少),也不能尝试在同一批处理中两次(或多次)创建同一对象。例如,即使INTO都无法运行(因为1!= 2和2!= 3),即使这样也会产生错误:

IF 1 = 2 BEGIN

    SELECT 1 AS one
    INTO #t;

END ELSE IF 2 = 3 BEGIN

    SELECT 2 AS one
    INTO #t;
END

您需要先CREATE个对象,然后INSERT INTO它:

DECLARE @Currentday AS int;

SET @Currentday = CAST(REPLACE(CAST(GETDATE() AS date), '-', '') AS int);

CREATE TABLE #SKST1 (ASAT int,        --Guessed data type                
                     CIF varchar(20), --Guessed data type                
                     ACCOUNT_BALANCE decimal(12, 2)); --Guessed data type                

IF @Currentday BETWEEN CAST(REPLACE(CAST(DATEADD(MONTH, DATEDIFF(MONTH, 0, GETDATE()), 0) AS date), '-', '') AS int) AND CAST(REPLACE(CAST(DATEADD(MONTH, DATEDIFF(MONTH, 0, GETDATE()), 1) AS date), '-', '') AS int)
BEGIN

    INSERT INTO #SKST1 (ASAT,
                        CIF,
                        ACCOUNT_BALANCE)
    SELECT ASAT,
           CIF,
           SUM(ACCOUNT_BALANCE) AS ACCOUNT_BALANCE
    FROM [COBG-BIU].dbo.MD_CARD_ENR_FAB
    WHERE CIF IN (SELECT CIF
                  FROM [COBG-BIU].dbo.MD_CARD_ENR_FAB
                  WHERE SPENDERTYPE = 'Revolver'
                    AND ASAT IN (CAST(CONVERT(varchar(6), GETDATE(), 112) AS int) - 3, CAST(CONVERT(varchar(6), GETDATE(), 112) AS int) - 2, CAST(CONVERT(varchar(6), GETDATE(), 112) AS int) - 4)
                    AND ACCOUNT_BALANCE > 0
                  GROUP BY CIF
                  HAVING COUNT(CIF) = 3)
      AND ASAT IN (CAST(CONVERT(varchar(6), GETDATE(), 112) AS int) - 3, CAST(CONVERT(varchar(6), GETDATE(), 112) AS int) - 2, CAST(CONVERT(varchar(6), GETDATE(), 112) AS int) - 4)
    GROUP BY ASAT,
             CIF;

END;
ELSE BEGIN

    INSERT INTO #SKST1 (ASAT,
                        CIF,
                        ACCOUNT_BALANCE)
    SELECT ASAT,
           CIF,
           SUM(ACCOUNT_BALANCE) AS ACCOUNT_BALANCE
    FROM [COBG-BIU].dbo.MD_CARD_ENR_FAB
    WHERE CIF IN (SELECT CIF
                  FROM [COBG-BIU].dbo.MD_CARD_ENR_FAB
                  WHERE SPENDERTYPE = 'Revolver'
                    AND ASAT IN (CAST(CONVERT(varchar(6), GETDATE(), 112) AS int) - 3, CAST(CONVERT(varchar(6), GETDATE(), 112) AS int) - 2, CAST(CONVERT(varchar(6), GETDATE(), 112) AS int) - 1)
                    AND ACCOUNT_BALANCE > 0
                  GROUP BY CIF
                  HAVING COUNT(CIF) = 3)
      AND ASAT IN (CAST(CONVERT(varchar(6), GETDATE(), 112) AS int) - 3, CAST(CONVERT(varchar(6), GETDATE(), 112) AS int) - 2, CAST(CONVERT(varchar(6), GETDATE(), 112) AS int) - 1)
    GROUP BY ASAT,
             CIF;

END;

注意,我不得不猜测您的数据类型;您将需要修复这些问题。