INSERT语句的选择列表包含的项目少于插入列表

时间:2014-08-29 22:16:03

标签: sql sql-server tsql

我正在尝试从View和Table中插入表变量,这是我试图使用的代码:

DECLARE @ResultCodes TABLE(ID_Audit INT,
                           AcctCode VARCHAR(3),
                           BidYear VARCHAR(4),
                           Num_Htls INT,
                           AuditStatus VARCHAR(50),
                           Account VARCHAR(3),
                           X INT, E INT, H INT, H_NLRA INT, N INT, Extra INT)

INSERT INTO @ResultCodes(ID_Audit, AcctCode, BidYear, Num_Htls, AuditStatus, Account, 
                         X, E, H, H_NLRA, N, Extra)
    SELECT 
        ID_Audit,
        AcctCode,
        BidYear,
        COUNT(ID_RFP),
        Audit_Status,
        Account 
    FROM 
        dbo.c_Audit_Rec_Status 
    WHERE 
        ID_Status_Audit = 105
    GROUP BY ID_Audit,AcctCode,BidYear,Audit_Status,Account
        UNION
    SELECT 
        SUM(CASE WHEN ResultCode3 = 'X' THEN 1 ELSE 0 END),
        SUM(CASE WHEN ResultCode3 = 'E' THEN 1 ELSE 0 END),
        SUM(CASE WHEN ResultCode3 = 'H' AND Rate_Basis = 'LRA' THEN 1 ELSE 0 END),
        SUM(CASE WHEN ResultCode3 = 'H' AND Rate_Basis = 'NLRA' THEN 1 ELSE 0 END),
        SUM(CASE WHEN ResultCode3 = 'N' THEN 1 ELSE 0 END),
        0
    FROM 
        tbl_FA_Results WHERE AuditDate3 IS NOT NULL         

        SELECT * FROM @ResultCodes  

但我一直收到这个错误

  

INSERT语句的选择列表包含的项目少于插入列表。 SELECT值的数量必须与INSERT列的数量匹配。

如果我曾经看过一次,我已经看了一百次这看起来应该有效,有人能看到我错过的吗?

修改

这是我最终得到的代码,就像魅力一样。有时只需要第二组眼睛:

DECLARE @ResultCodes TABLE(ID_Audit INT,AcctCode VARCHAR(3),BidYear VARCHAR(4),Num_Htls INT,AuditStatus VARCHAR(50),Account VARCHAR(3),X INT,E INT,H INT,H_NLRA INT,N INT,Extra INT)
INSERT INTO @ResultCodes(ID_Audit,AcctCode,BidYear,Num_Htls,AuditStatus,Account,X,E,H,H_NLRA,N,Extra)
    SELECT 
        ID_Audit,
        AcctCode,
        BidYear,
        COUNT(c_Audit_Rec_Status.ID_RFP),
        Audit_Status,
        Account ,
        SUM(CASE WHEN ResultCode3 = 'X' THEN 1 ELSE 0 END),
        SUM(CASE WHEN ResultCode3 = 'E' THEN 1 ELSE 0 END),
        SUM(CASE WHEN ResultCode3 = 'H' AND Rate_Basis = 'LRA' THEN 1 ELSE 0 END),
        SUM(CASE WHEN ResultCode3 = 'H' AND Rate_Basis = 'NLRA' THEN 1 ELSE 0 END),
        SUM(CASE WHEN ResultCode3 = 'N' THEN 1 ELSE 0 END),
        0
    FROM 
        dbo.c_Audit_Rec_Status,
        tbl_FA_Results  
    WHERE 
        c_Audit_Rec_Status.ID_Status_Audit = 105 AND AuditDate3 IS NOT NULL
    GROUP BY ID_Audit,AcctCode,BidYear,Audit_Status,Account

1 个答案:

答案 0 :(得分:2)

ID_Audit,AcctCode,BidYear,Num_Htls,AuditStatus,Account,X,E,H,H_NLRA,N,Extra

就像15件事。

  ID_Audit,
    AcctCode,
    BidYear,
    COUNT(ID_RFP),
    Audit_Status,
    Account 

就像6件事。那不会起作用。

了解更多信息:https://www.google.com/search?q=The+select+list+for+the+INSERT+statement+contains+fewer+items+than+the+insert+list.&oq=The+select+list+for+the+INSERT+statement+contains+fewer+items+than+the+insert+list.&aqs=chrome..69i57j0j69i64l3.160j0j1&sourceid=chrome&es_sm=91&ie=UTF-8