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

时间:2015-10-16 13:57:48

标签: sql-server tsql stored-procedures

我在使用此存储过程时遇到问题。我已经研究了几次,所以我猜我只是遗漏了一些明显的东西。如果我尝试实际删除选择列表的任何部分,那么SSMS会给我同样的错误。如果我让它们相等,那么VS debug会给我错误。我对那个有点困惑。大部分程序如下 -

ALTER PROCEDURE [dbo].[Billing_Misc_Update_Test]
(
@ID as  varchar(100), @CandidateID as varchar(100), @EntryType as varchar(100), @Amount as money, @EntryDate as smalldatetime, @InvoiceBillingDate as smalldatetime, @ClientKey as varchar(100), @BillingMemo as varchar(100), @UserID as varchar(100), @RegBilled as varchar(100), @OTBilled as varchar(100), @RegPaid as varchar(100), @OTPaid as varchar(100), @chkStatus as varchar(25)  )

-- This is an insert
if @ID = 0 
Begin
    If not exists (select 1 from AccoutingReports with (nolock) 
                    where Verify = @CandidateID 
                      and BillingDate = @EntryDate 
                      and InvoiceBillingDate = @InvoiceBillingDate
                      and BillingMiscType = @BillingMiscType

                    )
    Begin
    if @EntryType = 'Expense' or  @EntryType = 'Bonus' or @EntryType = 'other'
    Begin
        Insert into AccoutingReports ( 
                        [BillingDate], 
                        [ConsultantName],  
                        [HoursWorked],  
                        [OTHoursWork],  
                        [HoursBilled],  
                        [RegHoursBilled],  
                        [OTHoursBilled],  
                        [PayDollars],  
                        [BilledDollars],  
                        [RegBilledDollars],  
                        [NegBilledDollars],  
                        [OTBilledDollars],  
                        [NegOTBilled_Dollars],  
                        [MedicalCost],  
                        [FicsSutaSS],  
                        [PerDiem],  
                        [PerDiemPay],  
                        [COGS],  
                        [GrossProfit],  
                        [GrossMargin],  
                        [CommissionPercent],  
                        [CommissionSales],  
                        [CommissionRecruiter],      
                        [Verify],  
                        [BurdenPercent],  
                        [ClientKey],  
                        [W1],  
                        [W2],  
                        [EarnedDays],  
                        [VacationDays],        
                        [Memo],    
                        [TSDate],  
                        [isImported],   
                        [BillingMiscType],  
                        [BillingMiscMemo],  
                        [InvoiceBillingDate],  
                        [InvoiceStatus])    


                    Select  
                        @EntryDate,  
                        'Updated By UserID: ' + @UserID,  
                        0,   
                        0, 
                        0, 
                        0, 
                        0, 
                        @amount, 
                        @amount, 
                        @amount, 
                        @amount, 
                        0, 
                        0, 
                        0, 
                        0, 
                        0, 
                        0, 
                        0, 
                        0, 
                        0, 
                        0, 
                        0, 
                        0,  
                        @CandidateID, 
                        0, 
                        @ClientKey, 
                        0, 
                        0, 
                        0, 
                        0, 
                        @BillingMemo, 
                        getdate(), 
                        0,  
                        @BillingMiscType, 
                        @BillingMemo,  
                        @InvoiceBillingDate,  
                        @chkStatus
    End Else
    Begin
        Insert into AccoutingReports ( 
                        [BillingDate], 
                        [ConsultantName],  
                        [HoursWorked],  
                        [OTHoursWork],  
                        [HoursBilled],  
                        [RegHoursBilled],  
                        [OTHoursBilled],  
                        [PayDollars],  
                        [BilledDollars],  
                        [RegBilledDollars],  
                        [NegBilledDollars],  
                        [OTBilledDollars],  
                        [NegOTBilled_Dollars],  
                        [MedicalCost],  
                        [FicsSutaSS],  
                        [PerDiem],  
                        [PerDiemPay],  
                        [COGS],  
                        [GrossProfit],  
                        [GrossMargin],  
                        [CommissionPercent],  
                        [CommissionSales],  
                        [CommissionRecruiter],      
                        [Verify],  
                        [BurdenPercent],  
                        [ClientKey],  
                        [W1],  
                        [W2],  
                        [EarnedDays],  
                        [VacationDays],        
                        [Memo],    
                        [TSDate],  
                        [isImported],   
                        [BillingMiscType],  
                        [BillingMiscMemo],  
                        [InvoiceBillingDate],  
                        [InvoiceStatus])    


        Select          @EntryDate, 
                        'Updated By UserID: ' + @UserID,  
                        convert(money, @RegPaid) + convert(money, @OTPaid), 
                        @OTPaid,  
                        convert(money, @RegBilled) + convert(money, @OTBilled),        
                        @RegBilled,        
                        @OTBilled,             
                        (convert(money, @RegPaid) * @payrate) + (convert(money, @OTPaid) + @PayRate),  
                        (convert(money, @RegBilled) * @BillRate) + (convert(money, @OTBilled) + @BillRate),                   
                        0,                  
                        0,                  
                        0,                      
                        0,              
                        0,             
                        0,          
                        0,             
                        0,       
                        0,              
                        0,              
                        0,                    
                        0,                  
                        0,                      
                        0,  
                        @CandidateID,              
                        0,    
                        @ClientKey,     
                        0,     
                        0,             
                        0,               
                        0,  
                        @BillingMemo,   
                        getdate(),           
                        0,  
                        @BillingMiscType,     
                        @BillingMemo,  
                        @InvoiceBillingDate,  
                        @chkStatus 
    End
        Select RowsUpdated = @@Rowcount, Error = 'New Entry: Transaction Successfull'
    End Else
    Begin
        Select RowsUpdated = 0, Error = 'Entry Already Exists'
    End 
End Else
Begin
    If not exists (select 1 from AccoutingReports with (nolock) 
                    where Verify = @CandidateID 
                      and BillingDate = @EntryDate 
                      and InvoiceBillingDate = @InvoiceBillingDate
                      and BillingMiscType = @BillingMiscType
                      and ID <> @ID
                    )
    Begin                       
    if @EntryType = 'Expense' or @EntryType = 'Bonus' or @EntryType = 'other'
    Begin
        Update AccoutingReports 
           Set [BillingDate] = @EntryDate
             , [ConsultantName] = 'Updated By UserID: ' + @UserID
             , [HoursWorked] = 0
             , [OTHoursWork] = 0
             , [HoursBilled] = 0
             , [RegHoursBilled] = 0
             , [OTHoursBilled]   = 0             
             , [PayDollars] = @amount
             , [BilledDollars] = @amount
             , [RegBilledDollars] = @amount
             , [NegBilledDollars] = @amount
             , BillingMiscType = @BillingMiscType
             , BillingMiscMemo = @BillingMemo
             , InvoiceBillingDate = @InvoiceBillingDate 
             , InvoiceStatus = @chkStatus
         where ID = @ID
    End Else
    Begin
        Update AccoutingReports 
           Set [BillingDate] = @EntryDate
             , [ConsultantName] = 'Updated By UserID: ' + @UserID
             , [HoursWorked] = convert(money, @RegPaid) + convert(money, @OTPaid)
             , [OTHoursWork] = @OTPaid
             , [HoursBilled] = convert(money, @RegBilled) + convert(money, @OTBilled)
             , [RegHoursBilled] = @RegBilled
             , [OTHoursBilled]   = @OTBilled             
             , [PayDollars] = (convert(money, @RegPaid) * @payrate) + (convert(money, @OTPaid) + @PayRate)
             , [BilledDollars] = (convert(money, @RegBilled) * @BillRate) + (convert(money, @OTBilled) + @BillRate)
             , [RegBilledDollars] = 0
             , [NegBilledDollars] = 0
             , BillingMiscType = @BillingMiscType
             , BillingMiscMemo = @BillingMemo
             , InvoiceBillingDate = @InvoiceBillingDate 
             , InvoiceStatus = @chkStatus
         where ID = @ID
    End
        Select RowsUpdated = @@Rowcount, Error = 'Update Entry: Transaction Successfull'
    End Else
    Begin
        Select RowsUpdated = 0, Error = 'Entry Already Exists'
    End 
End 

ID通常会导致0,而{I}开始的EntryType是Bonus。所以它应该运行第一个insert / select语句块,但是会出错。

值得注意的是,我所做的更改是将新列InvoiceStatus添加到AccountingReports表中以将chkStatus存储为varchar(25)。将这个新列添加到表中并修改insert / select语句后,它给了我这个错误。我很失落所以任何建议都会有所帮助,谢谢!

1 个答案:

答案 0 :(得分:0)

我最终在数据库中创建了另一个表,第二个插入/选择语句将使用相同的ID +候选ID,然后使用参数添加过滤的chkstatus列。我不知道为什么原始解决方案不起作用,但这是一个相当简单的解决方法,不应该偏离理想的解决方案。干杯!