插入时的表值参数错误

时间:2016-04-03 07:38:52

标签: c# visual-studio-2010 sql-server-2008 table-valued-parameters

我正在使用以下sp在3个表中插入值,所以当我的TVPCheque和TVP Cash有记录时,它的工作没有任何错误但是, 当我的表值参数在后续sp上为空时,则在c#

中引发错误
ALTER PROCEDURE [Ordering].[Orders_Add]
    -- Add the parameters for the stored procedure here
@OrderID int,@CustomerFK nvarchar(50),@ShipperFK int=null,@OrderDate Char(10),@RequiredDate char(10)=null,@ExpireDate char(10)=null,@StoreFK int,@ProductCategoryFK int,@ProductFK int,@Quantity float,@Price decimal,@ShipAddress nvarchar(512)=null,@Description1 ntext=null,@Description2 ntext=null,@Discount decimal=null,@Discount2 decimal=null,@OrderWayBill int=null,@OrderStatusFK int,@PaymentTermFK int=null,@DriverName nvarchar(50),@FromBalance bit=null,@FromBalanceTopicFK nvarchar(50),@FromBalancePrice decimal,@FromBalanceDescription nvarchar(100),@UserAddFK int,@FinancialPeriodFK tinyint,@CompanyInfoFK tinyint,@TVPCash TableType_ReceivedCash READONLY,@TVPCheque TableType_ReceivedCheque READONLY,@CredentialFK int=null,@error nvarchar(100)=null output,@TableID int=null output
AS
BEGIN
    -- SET NOCOUNT ON added to prevent extra result sets from
    -- interfering with SELECT statements.
    SET NOCOUNT ON;
BEGIN TRANSACTION
BEGIN TRY

     set @error=null

if exists(select * from Ordering.Orders where OrderID=@OrderID and FinancialPeriodFK=@FinancialPeriodFK) 
begin
exec @OrderID=[dbo].[GetNextID] 'Ordering.Orders','OrderID',@FinancialPeriodFK
end
    insert into Ordering.Orders(OrderID,CustomerFK,ShipperFK,OrderDate,RequiredDate,[ExpireDate],StoreFK,ProductCategoryFK,ProductFK,Quantity,Price,ShipAddress,Description1,Description2,Discount,Discount2,OrderWayBill,OrderStatusFK,PaymentTermFK,DriverName,FromBalance,FromBalanceTopicFK,FromBalanceDescription,FromBalancePrice,UserAddFK,CredentialFK,FinancialPeriodFK,CompanyInfoFK)
    values (@OrderID,@CustomerFK,@ShipperFK,@OrderDate,@RequiredDate,@ExpireDate,@StoreFK,@ProductCategoryFK,@ProductFK,@Quantity,@Price,@ShipAddress,@Description1,@Description2,@Discount,@Discount2,@OrderWayBill,@OrderStatusFK,@PaymentTermFK,@DriverName,@FromBalance,@FromBalanceTopicFK,@FromBalanceDescription,@FromBalancePrice,@UserAddFK,@CredentialFK,@FinancialPeriodFK,@CompanyInfoFK)


insert into banking.receivedcash(ReceivedCashID,Date,Time,ReceivedFromAccount,PayedToAccount,Serial,Description,Price,SalesInvoiceHeaderFK,CostCenterFK,FinancialPeriodFK,CompanyInfoFK,OrderFK)
select (select  isnull(Max(ReceivedCashID),0) from Banking.ReceivedCash where FinancialPeriodFK=@FinancialPeriodFK)+
    ROW_NUMBER() OVER(ORDER BY t.Date,t.Time),t.Date,t.Time,t.ReceivedFromAccount,t.PayedToAccount,t.Serial,t.Description,t.Price,t.SalesInvoiceHeaderFK,t.CostCenterFK,t.FinancialPeriodFK,t.CompanyInfoFK,@OrderID from @TVPCash as t
    where t.ReceivedCashID=-1

insert into Banking.ReceivedCheque(ChequeSerial,ReceivedFromAccount,Date,DueDate,[Time],BankName,BankBranch,BankAccountOwner,BankAccountNo,Description,Price,StatusFK,StatusDate,StatusAccount,DefaultBankAccount,SalesInvoiceHeaderFK,CostCenterFK,FinancialPeriodFK,CompanyInfoFK,OrderFK)
select t1.ChequeSerial,t1.ReceivedFromAccount,t1.Date,t1.DueDate,t1.Time,t1.BankName,t1.BankBranch,t1.BankAccountOwner,t1.BankAccountNo,t1.Description,t1.Price,t1.StatusFK,t1.Date,t1.DefaultBankAccount,t1.DefaultBankAccount,t1.SalesInvoiceHeaderFK,t1.CostCenterFK,t1.FinancialPeriodFK,t1.CompanyInfoFK,@OrderID from @TVPCheque as t1




        if (@@ROWCOUNT <= 0)
    begin

    return -1
    end
    else
    begin
set @TableID=@OrderID
    end


    COMMIT
    end TRY
    BEGIN CATCH

   --if an exception occurs execute your rollback, also test that you have had some successful transactions
   IF @@TRANCOUNT > 0 ROLLBACK;  

END CATCH
END

错误 error when TVP parameters are null

1 个答案:

答案 0 :(得分:1)

问题在于Zohar描述您在未提交或回滚待处理交易的情况下返回。

如果在返回之前添加ROLLBACK TRANSACTION(或COMMIT TRANSACTION),则不应该出现此问题。

    if (@@ROWCOUNT <= 0)
    begin
        ROLLBACK TRANSACTION
        return -1
    end
    (..)