【问题标题】:Table Value Parameter error when Insert插入时表值参数错误
【发布时间】:2016-04-03 07:38:52
【问题描述】:

我正在使用以下 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

错误

【问题讨论】:

  • 在提交或回滚事务之前有一个return -1。我不确定这是问题的原因,但我很确定它不应该存在。另外,@@Rowcount 不能小于 0。

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


【解决方案1】:

这里的问题是 Zohar 描述的您返回时没有提交或回滚待处理的事务。

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

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-12-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多