在insert语句中使用标量函数会导致异常

时间:2015-09-30 14:22:50

标签: c# asp.net sql-server function

当我执行以下查询时,我得到以下异常:

  

找不到列“dbo”或用户定义的函数或   聚合“dbo.GET_Vendor_Order_DeductionAmount”,或者名称是   暧昧

我的SQL:

string strinsert5 = "INSERT INTO VendorWallet(
  VendorID, 
  Amount, 
  WalletTransactionTypeID, 
  Status, 
  DateAdded, 
  OrderID, 
  OrderDeductedAmount,
  ClosingBalance
) values (
  @VendorID,
  @Amount,
  @WalletTransactionTypeID,
  @Status,
  getdate(),
  @OrderID, 
  dbo.[GET_Vendor_Order_DeductionAmount] (@VendorID, " + OperatorID + ",@Amount), 
  dbo.GET_Wallet_Amount(@VendorID)+@Amount)";

请帮帮我。

1 个答案:

答案 0 :(得分:0)

为什么这一切都在这样的字符串中?为什么一切参数化除了OperatorID?在发布的极少量代码中有很多代码味道。但是,您可以通过使用select而不是值列表来完成此工作。我会敦促你正确地参数化所有内容,而不是大部分内容。

string strinsert5 = "INSERT INTO VendorWallet(
  VendorID, 
  Amount, 
  WalletTransactionTypeID, 
  Status, 
  DateAdded, 
  OrderID, 
  OrderDeductedAmount,
  ClosingBalance
) 
SELECT 
  @VendorID,
  @Amount,
  @WalletTransactionTypeID,
  @Status,
  getdate(),
  @OrderID, 
  dbo.[GET_Vendor_Order_DeductionAmount] (@VendorID, " + OperatorID + ",@Amount), 
  dbo.GET_Wallet_Amount(@VendorID)+@Amount";