帮助解决 SQL 命令不起作用的问题
我有一个正在尝试执行的 SQL 命令。
语法如下
Public Shared Function CanRaiseWorkOrder(connection As IDbConnection, ProductID As Guid) As Boolean
Using sqlCmd As IDbCommand = connection.CreateCommand
With sqlCmd
.CommandTimeout = 30
.CommandType = CommandType.Text
.CommandText = "DECLARE @CanRaiseWorkOrder BIT, @WorkOrderQtyCount INT, @ProductAvailCount INT SET @WorkOrderQtyCount = (SELECT SUM(Qty) FROM WorkOrder WHERE ProductID = @ProductID AND WorkOrder.Status <> 4) --4 = Voided SET @ProductAvailCount = (SELECT Qty FROM Product WHERE ProductID = @ProductID) IF @WorkOrderQtyCount < @ProductAvailCount BEGIN SET @CanRaiseWorkOrder = 1 END ELSE BEGIN SET @CanRaiseWorkOrder = 0 END SELECT @CanRaiseWorkOrder AS CanRaiseWorkOrder"
Dim params As New List(Of IDbDataParameter)({
ProductDAL.CreateTSqlParameter("@ProductID", DbType.Guid, ProductID)
})
.Parameters.AddRange(params)
Return .ExecuteScaler(Of Boolean)()
End With
End Using
End Function
,您可能会注意到,关于如何创建参数和执行命令,有一些自定义,但您可以假设系统的这些方面按要求工作(我们有大量可以正确运行的代码使用这些方法)。
我可能会有些人问为什么这不是一个存储过程,答案是“因为我的老板这么说”。
我已经运行了 SQL 探查器,下面是该查询实际生成的输出。
exec sp_executesql N'DECLARE @CanRaiseWorkOrder BIT, @WorkOrderQtyCount INT, @ProductAvailCount INT SET @WorkOrderQtyCount = (SELECT SUM(Qty) FROM WorkOrder WHERE ProductID = @ProductID AND WorkOrder.Status <> 4) --4 = Voided SET @ProductAvailCount = (SELECT Qty FROM Product WHERE ProductID = @ProductID) IF @WorkOrderQtyCount < @ProductAvailCount BEGIN SET @CanRaiseWorkOrder = 1 END ELSE BEGIN SET @CanRaiseWorkOrder = 0 END SELECT @CanRaiseWorkOrder',N'@ProductID uniqueidentifier',@ProductID='0908C780-763F-4CE6-B074-CEC01F4451B4'
在查询分析器中运行代码(当我最初创建它时)工作正常,但如果我运行从 SQL 命令输出的上述查询,我得到的只是“命令成功完成。
”
任何想法?
I have an SQL command I am attempting to execute.
The syntax is as follows
Public Shared Function CanRaiseWorkOrder(connection As IDbConnection, ProductID As Guid) As Boolean
Using sqlCmd As IDbCommand = connection.CreateCommand
With sqlCmd
.CommandTimeout = 30
.CommandType = CommandType.Text
.CommandText = "DECLARE @CanRaiseWorkOrder BIT, @WorkOrderQtyCount INT, @ProductAvailCount INT SET @WorkOrderQtyCount = (SELECT SUM(Qty) FROM WorkOrder WHERE ProductID = @ProductID AND WorkOrder.Status <> 4) --4 = Voided SET @ProductAvailCount = (SELECT Qty FROM Product WHERE ProductID = @ProductID) IF @WorkOrderQtyCount < @ProductAvailCount BEGIN SET @CanRaiseWorkOrder = 1 END ELSE BEGIN SET @CanRaiseWorkOrder = 0 END SELECT @CanRaiseWorkOrder AS CanRaiseWorkOrder"
Dim params As New List(Of IDbDataParameter)({
ProductDAL.CreateTSqlParameter("@ProductID", DbType.Guid, ProductID)
})
.Parameters.AddRange(params)
Return .ExecuteScaler(Of Boolean)()
End With
End Using
End Function
As you will probably notice there is some customization there in regards to how the parameters are created and the command executed but you can assume those aspects of the system work as required (We have a significant amount of code that functions correctly using those methods).
I will probably get some people asking why this is not a stored procedure and the answer is "because my boss said so".
I have run SQL profiler and here is the output that this query actually generates.
exec sp_executesql N'DECLARE @CanRaiseWorkOrder BIT, @WorkOrderQtyCount INT, @ProductAvailCount INT SET @WorkOrderQtyCount = (SELECT SUM(Qty) FROM WorkOrder WHERE ProductID = @ProductID AND WorkOrder.Status <> 4) --4 = Voided SET @ProductAvailCount = (SELECT Qty FROM Product WHERE ProductID = @ProductID) IF @WorkOrderQtyCount < @ProductAvailCount BEGIN SET @CanRaiseWorkOrder = 1 END ELSE BEGIN SET @CanRaiseWorkOrder = 0 END SELECT @CanRaiseWorkOrder',N'@ProductID uniqueidentifier',@ProductID='0908C780-763F-4CE6-B074-CEC01F4451B4'
Running the code in query analyser (when I originally created it) works fine but if I run the above query as outputted from the SQL command all I get is "Command(s) completed successfully.
"
Any ideas?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我会回答这个问题,但也想确保我的回答是准确的(考虑到我现在已经起床多久了:grin:)。
从我所看到的(这可能是由于为了使其成为代码文件中的一行而压缩了您的语句)您的语句中间有一个注释声明:
发生的情况是您只执行您的 DECLARE 和 first SET 命令(完成时没有错误),但语句的其余部分将被忽略,因为它遵循注释声明 (
--
)。如果您确实压缩了查询,请确保删除注释行。一旦它们在一行上,SQL 就不会关心并且会忽略
--
之后的任何内容。Figure I'll make it an answer, but also wanted to make sure I was accurate (considering how long I've been up now :grin:).
From what I can see (and this is probably due to condensing your statement for the sake of making it a one-liner in a code file) you have a comment declaration in the middle of your statement:
What's happening is you're executing only your DECLARE and first SET command (which are done without error) but the rest of your statement is being ignored because it follows the comment declaration (
--
).Make sure if you do condense your query that you remove commented lines. Once they're on one line, SQL doesn't care and WILL ignore anything past the
--
.