Ошибка параметра таблицы значений при вставке

Я использую следующий 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

ошибка

1 ответ

Решение

Проблема здесь в том, что Зохар описывает, что вы возвращаетесь без фиксации или отката отложенной транзакции.

если вы добавите ROLLBACK TRANSACTION (или COMMIT TRANSACTION) перед возвратом, у вас не должно возникнуть этой проблемы.

    if (@@ROWCOUNT <= 0)
    begin
        ROLLBACK TRANSACTION
        return -1
    end
    (..)
Другие вопросы по тегам