SQL合并 - 插入的列数必须与指定的列数相匹配

时间:2017-08-16 12:06:04

标签: sql sql-server sql-merge

我正在使用存储过程,我使用Merge语句根据条件执行插入或更新。

然而,在尝试编译程序时,我收到错误

  

INSERT语句中的列多于VALUES子句中指定的值。 VALUES子句中的值数必须与INSERT语句中指定的列数相匹配。

但是,我确实认为我有正确数量的列,因为它是从插入到值区域的复制和粘贴练习。

请参阅下面的合并声明,并告诉我这是怎么回事

WITH CTE AS 
(
    SELECT  
        [PRNCPL_ACC_CD], [BRANCH_ACC_CD],
        [COPY_MISC_DLR_FG], [COPY_LABR_HRS_FG],
        [COPY_CSUM_COST_FG], [COPY_PART_PRICE_FG],
        [COPY_TRVL_RATE_FG], [COPY_DISC_FG],        
        [COPY_TAX_RATE_FG], [COPY_REJ_RESN_FG],
        [COPY_CNTR_OBLG_FG], [COPY_LABR_OVRS_FG]
    FROM 
        CEQBP 
    WHERE  
        SHEDDULED_CHANGE_ID = CONVERT(CHAR(255), @SHEDDULED_CHANGE_ID)
)
MERGE INTO CEQBM AS T 
USING CTE S ON  T.PRNCPL_ACC_CD  = S.PRNCPL_ACC_CD
            AND T.BRANCH_ACC_CD = S.BRANCH_ACC_CD

WHEN MATCHED THEN
    UPDATE 
       SET 
          T.[PRNCPL_ACC_CD] = S.[PRNCPL_ACC_CD],
          T.[BRANCH_ACC_CD] = S.[BRANCH_ACC_CD],
          T.[COPY_MISC_DLR_FG] = S.[COPY_MISC_DLR_FG],
          T.[COPY_LABR_HRS_FG] = S.[COPY_LABR_HRS_FG],
          T.[COPY_CSUM_COST_FG] = S.[COPY_CSUM_COST_FG],
          T.[COPY_PART_PRICE_FG] = S.[COPY_PART_PRICE_FG],
          T.[COPY_TRVL_RATE_FG] = S.[COPY_TRVL_RATE_FG],
          T.[COPY_DISC_FG] = S.[COPY_DISC_FG],
          T.[COPY_TAX_RATE_FG] = S.[COPY_TAX_RATE_FG],
          T.[COPY_REJ_RESN_FG] = S.[COPY_REJ_RESN_FG], 
          T.[COPY_CNTR_OBLG_FG] = S.[COPY_CNTR_OBLG_FG],
          T.[COPY_LABR_OVRS_FG] = S.[COPY_LABR_OVRS_FG]

WHEN NOT MATCHED THEN 
    INSERT ([PRNCPL_ACC_CD], [BRANCH_ACC_CD],
            [COPY_MISC_DLR_FG], [COPY_LABR_HRS_FG],
            [COPY_CSUM_COST_FG], [COPY_PART_PRICE_FG],
            [COPY_TRVL_RATE_FG], [COPY_DISC_FG],
            [COPY_TAX_RATE_FG], [COPY_REJ_RESN_FG],
            [COPY_CNTR_OBLG_FG], [COPY_LABR_OVRS_FG])
    VALUES (S.[PRNCPL_ACC_CD], S.[BRANCH_ACC_CD],
            S.[COPY_MISC_DLR_FG], S.[COPY_LABR_HRS_FG],
            S.[COPY_CSUM_COST_FG], S.[COPY_PART_PRICE_FG,
            S.[COPY_TRVL_RATE_FG], S.[COPY_DISC_FG],
            S.[COPY_TAX_RATE_FG], S.[COPY_REJ_RESN_FG],
            S.[COPY_CNTR_OBLG_FG], S.[COPY_LABR_OVRS_FG]);

使用完整脚本进行编辑

用户定义的表类型

CREATE TYPE CEQBP_TYP AS TABLE(
    [PRNCPL_ACC_CD]         [char](10)                  ,
    [BRANCH_ACC_CD]         [char](10)                  ,
    [COPY_MISC_DLR_FG]      BIT DEFAULT(0)              ,
    [COPY_LABR_HRS_FG]      BIT DEFAULT(0)              ,
    [COPY_CSUM_COST_FG]     BIT DEFAULT(0)              ,
    [COPY_PART_PRICE_FG]    BIT DEFAULT(0)              ,
    [COPY_TRVL_RATE_FG]     BIT DEFAULT(0)              ,
    [COPY_DISC_FG]          BIT DEFAULT(0)              ,
    [COPY_TAX_RATE_FG]      BIT DEFAULT(0)              ,
    [COPY_REJ_RESN_FG]      BIT DEFAULT(0)              ,
    [COPY_CNTR_OBLG_FG]     BIT DEFAULT(0)              ,
    [COPY_LABR_OVRS_FG]     BIT DEFAULT(0)              ,
    [EDIT_MISC_DLR_FG]      BIT DEFAULT(0)              ,
    [EDIT_LABR_HRS_FG]      BIT DEFAULT(0)              ,
    [EDIT_CSUM_COST_FG]     BIT DEFAULT(0)              ,
    [EDIT_PART_PRICE_FG]    BIT DEFAULT(0)              ,
    [EDIT_TRVL_RATE_FG]     BIT DEFAULT(0)              ,
    [EDIT_DISC_FG]          BIT DEFAULT(0)              ,
    [EDIT_TAX_RATE_FG]      BIT DEFAULT(0)              ,
    [EDIT_REJ_RESN_FG]      BIT DEFAULT(0)              ,
    [EDIT_CNTR_OBLG_FG]     BIT DEFAULT(0)              ,
    [EDIT_LABR_OVRS_FG]     BIT DEFAULT(0)              ,
    [SHEDULED_BY]           VARCHAR (10)                ,
    [DATE_SCHEDULED]        DATETIME DEFAULT(GETDATE()) ,
    [EXECUTE_IMMEDIATELY]   DATETIME DEFAULT(1)         
)
GO

存储过程

CREATE PROCEDURE sp_InsertCopyDataStagingData

@TVP CEQBP_TYP READONLY 

AS
BEGIN
BEGIN TRY
BEGIN TRAN

DECLARE @SHEDDULED_CHANGE_ID uniqueidentifier = NEWID()

INSERT INTO CEQBP (
                     [SHEDDULED_CHANGE_ID]  
                    ,[PRNCPL_ACC_CD]            
                    ,[BRANCH_ACC_CD]            
                    ,[COPY_MISC_DLR_FG]     
                    ,[COPY_LABR_HRS_FG]     
                    ,[COPY_CSUM_COST_FG]        
                    ,[COPY_PART_PRICE_FG]   
                    ,[COPY_TRVL_RATE_FG]        
                    ,[COPY_DISC_FG]         
                    ,[COPY_TAX_RATE_FG]     
                    ,[COPY_REJ_RESN_FG]     
                    ,[COPY_CNTR_OBLG_FG]        
                    ,[COPY_LABR_OVRS_FG]        
                    ,[EDIT_MISC_DLR_FG]     
                    ,[EDIT_LABR_HRS_FG]     
                    ,[EDIT_CSUM_COST_FG]        
                    ,[EDIT_PART_PRICE_FG]   
                    ,[EDIT_TRVL_RATE_FG]        
                    ,[EDIT_DISC_FG]         
                    ,[EDIT_TAX_RATE_FG]     
                    ,[EDIT_REJ_RESN_FG]     
                    ,[EDIT_CNTR_OBLG_FG]        
                    ,[EDIT_LABR_OVRS_FG]        
                    ,[SHEDULED_BY]          
                    ,[DATE_SCHEDULED]       
                    ,[EXECUTE_IMMEDIATELY]) 
SELECT
                     CONVERT(char(255), @SHEDDULED_CHANGE_ID) 
                    ,[PRNCPL_ACC_CD]            
                    ,[BRANCH_ACC_CD]            
                    ,[COPY_MISC_DLR_FG]     
                    ,[COPY_LABR_HRS_FG]     
                    ,[COPY_CSUM_COST_FG]        
                    ,[COPY_PART_PRICE_FG]   
                    ,[COPY_TRVL_RATE_FG]        
                    ,[COPY_DISC_FG]         
                    ,[COPY_TAX_RATE_FG]     
                    ,[COPY_REJ_RESN_FG]     
                    ,[COPY_CNTR_OBLG_FG]        
                    ,[COPY_LABR_OVRS_FG]        
                    ,[EDIT_MISC_DLR_FG]     
                    ,[EDIT_LABR_HRS_FG]     
                    ,[EDIT_CSUM_COST_FG]        
                    ,[EDIT_PART_PRICE_FG]   
                    ,[EDIT_TRVL_RATE_FG]        
                    ,[EDIT_DISC_FG]         
                    ,[EDIT_TAX_RATE_FG]     
                    ,[EDIT_REJ_RESN_FG]     
                    ,[EDIT_CNTR_OBLG_FG]        
                    ,[EDIT_LABR_OVRS_FG]        
                    ,[SHEDULED_BY]          
                    ,[DATE_SCHEDULED]       
                    ,[EXECUTE_IMMEDIATELY]
FROM @TVP

IF (SELECT EXECUTE_IMMEDIATELY FROM CEQBP WHERE  SHEDDULED_CHANGE_ID = CONVERT(char(255), @SHEDDULED_CHANGE_ID) ) = 1
BEGIN -- CALL STORED PROC TO RUN IMMEDIDATELY 
    WITH CTE AS (SELECT  
                 [PRNCPL_ACC_CD]        
                ,[BRANCH_ACC_CD]        
                ,[COPY_MISC_DLR_FG]
                ,[COPY_LABR_HRS_FG]
                ,[COPY_CSUM_COST_FG]
                ,[COPY_PART_PRICE_FG]
                ,[COPY_TRVL_RATE_FG]
                ,[COPY_DISC_FG]     
                ,[COPY_TAX_RATE_FG]
                ,[COPY_REJ_RESN_FG]
                ,[COPY_CNTR_OBLG_FG]
                ,[COPY_LABR_OVRS_FG]
                 FROM CEQBP WHERE  
                 SHEDDULED_CHANGE_ID = CONVERT(char(255), @SHEDDULED_CHANGE_ID))
    MERGE CEQBM AS T 
    USING CTE S ON  T.PRNCPL_ACC_CD  = S.PRNCPL_ACC_CD
                AND T.BRANCH_ACC_CD = S.BRANCH_ACC_CD
    WHEN MATCHED THEN
                UPDATE SET 
                 T.[PRNCPL_ACC_CD]      = S.[PRNCPL_ACC_CD]     
                ,T.[BRANCH_ACC_CD]      = S.[BRANCH_ACC_CD]     
                ,T.[COPY_MISC_DLR_FG]   = S.[COPY_MISC_DLR_FG]  
                ,T.[COPY_LABR_HRS_FG]   = S.[COPY_LABR_HRS_FG]  
                ,T.[COPY_CSUM_COST_FG]  = S.[COPY_CSUM_COST_FG] 
                ,T.[COPY_PART_PRICE_FG] = S.[COPY_PART_PRICE_FG]    
                ,T.[COPY_TRVL_RATE_FG]  = S.[COPY_TRVL_RATE_FG] 
                ,T.[COPY_DISC_FG]       = S.[COPY_DISC_FG]      
                ,T.[COPY_TAX_RATE_FG]   = S.[COPY_TAX_RATE_FG]  
                ,T.[COPY_REJ_RESN_FG]   = S.[COPY_REJ_RESN_FG]  
                ,T.[COPY_CNTR_OBLG_FG]  = S.[COPY_CNTR_OBLG_FG] 
                ,T.[COPY_LABR_OVRS_FG]  = S.[COPY_LABR_OVRS_FG]
    WHEN NOT MATCHED THEN 
                INSERT 
                    ( [PRNCPL_ACC_CD]       
                     ,[BRANCH_ACC_CD]       
                     ,[COPY_MISC_DLR_FG]    
                     ,[COPY_LABR_HRS_FG]    
                     ,[COPY_CSUM_COST_FG]   
                     ,[COPY_PART_PRICE_FG]  
                     ,[COPY_TRVL_RATE_FG]   
                     ,[COPY_DISC_FG]        
                     ,[COPY_TAX_RATE_FG]    
                     ,[COPY_REJ_RESN_FG]        
                     ,[COPY_CNTR_OBLG_FG]   
                     ,[COPY_LABR_OVRS_FG]   )
                VALUES
                    (
                      S.[PRNCPL_ACC_CD]     
                     ,S.[BRANCH_ACC_CD]     
                     ,S.[COPY_MISC_DLR_FG]  
                     ,S.[COPY_LABR_HRS_FG]  
                     ,S.[COPY_CSUM_COST_FG] 
                     ,S.[COPY_PART_PRICE_FG 
                     ,S.[COPY_TRVL_RATE_FG] 
                     ,S.[COPY_DISC_FG]      
                     ,S.[COPY_TAX_RATE_FG]  
                     ,S.[COPY_REJ_RESN_FG]  
                     ,S.[COPY_CNTR_OBLG_FG] 
                     ,S.[COPY_LABR_OVRS_FG]     );

END 
-- Using while loop to clean up any records that may have had an issue
-- This should only ever be one record at this stage
WHILE(SELECT TOP 1 * FROM CEQBP WHERE DATE_EXECUTED IS NULL AND EXECUTE_IMMEDIATELY = 1) > 0
    BEGIN
        DECLARE @UserIdVar          VARCHAR(10)
        DECLARE @DealerCodeVar      VARCHAR(10)
        DECLARE @FromBranchVar      VARCHAR(10)
        WITH CTE AS (SELECT TOP 1 * FROM CEQBP WHERE DATE_EXECUTED IS NULL AND EXECUTE_IMMEDIATELY = 1)
        SELECT  @UserIdVar = SHEDULED_BY 
               ,@DealerCodeVar = PRNCPL_ACC_CD 
               ,@FromBranchVar = BRANCH_ACC_CD FROM CTE
        EXEC sp_CopyData 
    END
COMMIT TRAN

END TRY
BEGIN CATCH

ROLLBACK TRAN

DECLARE 
            @ErrorMessage NVARCHAR(4000),
            @ErrorSeverity INT,
            @ErrorState INT

SELECT 
            @ErrorMessage = ERROR_MESSAGE(),
            @ErrorSeverity = ERROR_SEVERITY(),
            @ErrorState = ERROR_STATE();

END CATCH
END
GO

0 个答案:

没有答案