Table插入时值参数错误
Table Value Parameter error when Insert
我正在使用以下 sp 在 3 个表中插入值,所以当我的 TVPCheque 和 TVP Cash 有记录时,它可以正常工作但是,
当我的 Table 值参数在以下 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
错误
这里的问题正如 Zohar 所描述的那样,您 return 没有提交或回滚您的未决事务。
如果您在 return 之前添加一个 ROLLBACK TRANSACTION(或 COMMIT TRANSACTION),您应该不会遇到这个问题。
if (@@ROWCOUNT <= 0)
begin
ROLLBACK TRANSACTION
return -1
end
(..)
我正在使用以下 sp 在 3 个表中插入值,所以当我的 TVPCheque 和 TVP Cash 有记录时,它可以正常工作但是, 当我的 Table 值参数在以下 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
错误
这里的问题正如 Zohar 所描述的那样,您 return 没有提交或回滚您的未决事务。
如果您在 return 之前添加一个 ROLLBACK TRANSACTION(或 COMMIT TRANSACTION),您应该不会遇到这个问题。
if (@@ROWCOUNT <= 0)
begin
ROLLBACK TRANSACTION
return -1
end
(..)