无法访问命令文本中的输出变量
Can't access output variable in command text
Set cmd = Server.CreateObject("ADODB.Command")
With cmd
.ActiveConnection = db
.CommandText = "SELECT @date = '2019-01-01'"
.Parameters.Append(.CreateParameter("@date", adDBDate, adParamOutput))
.Execute
End With
给予...
Must declare the scalar variable "@date".
为什么我无法访问查询文本中的输出参数?
错误来自 T-SQL,因为变量 @date
尚未声明。
调整你传递给ADODB.Command
的T-SQL字符串来声明变量;
Set cmd = Server.CreateObject("ADODB.Command")
With cmd
.ActiveConnection = db
.CommandText = "DECLARE @date AS DATETIME; SELECT @date = '2019-01-01';"
.Parameters.Append(.CreateParameter("@date", adDBDate, adParamOutput))
.Execute
End With
调试此类问题的一种简单方法是使用 SQL Server Management Studio 运行 原始查询。
SELECT @date = '2019-01-01'
如果您在没有声明的情况下尝试 运行 查询,您会得到同样的错误,但会得到更详细的信息。
Msg 137, Level 15, State 1, Line 1
Must declare the scalar variable "@date".
命名参数在双方(服务器:SQL 服务器,客户端:ADODB 和 VBScript 为您的情况)上按预期工作,仅当提供者支持它时。对于 SQL 服务器提供程序,仅支持配置为调用带有命名参数的存储过程的命令(其中 cmd.CommandType
设置 adCmdStoredProc
和 cmd.NamedParameters
设置 True
)。
对于像您这样的普通命令,服务器无法识别命名参数,只有 ?
占位符被识别为查询中的参数。
所以你应该尝试下面的方法。
Set cmd = Server.CreateObject("ADODB.Command")
With cmd
.ActiveConnection = db
.CommandText = "SELECT ? = '2019-01-01'"
.Parameters.Append(.CreateParameter("@dummyForServerNotForClient", adDBDate, adParamOutput))
.Execute
' must print: 2019-01-01
Response.Write .Parameters("@dummyForServerNotForClient").Value
End With
由于服务器会忽略参数名称,您可以通过省略参数名称来编写相同的代码,并使用参数在集合中的序号位置来访问参数的值。恕我直言,由于缺少明确命名的参数,代码变得更具逻辑性和可读性。
Set cmd = Server.CreateObject("ADODB.Command")
With cmd
.ActiveConnection = db
.CommandText = "SELECT ? = '2019-01-01'"
.Parameters.Append(.CreateParameter(, adDBDate, adParamOutput))
.Execute
' also must print: 2019-01-01
Response.Write .Parameters(0).Value
End With
我希望这有助于您理解这个概念。
Set cmd = Server.CreateObject("ADODB.Command")
With cmd
.ActiveConnection = db
.CommandText = "SELECT @date = '2019-01-01'"
.Parameters.Append(.CreateParameter("@date", adDBDate, adParamOutput))
.Execute
End With
给予...
Must declare the scalar variable "@date".
为什么我无法访问查询文本中的输出参数?
错误来自 T-SQL,因为变量 @date
尚未声明。
调整你传递给ADODB.Command
的T-SQL字符串来声明变量;
Set cmd = Server.CreateObject("ADODB.Command")
With cmd
.ActiveConnection = db
.CommandText = "DECLARE @date AS DATETIME; SELECT @date = '2019-01-01';"
.Parameters.Append(.CreateParameter("@date", adDBDate, adParamOutput))
.Execute
End With
调试此类问题的一种简单方法是使用 SQL Server Management Studio 运行 原始查询。
SELECT @date = '2019-01-01'
如果您在没有声明的情况下尝试 运行 查询,您会得到同样的错误,但会得到更详细的信息。
Msg 137, Level 15, State 1, Line 1
Must declare the scalar variable "@date".
命名参数在双方(服务器:SQL 服务器,客户端:ADODB 和 VBScript 为您的情况)上按预期工作,仅当提供者支持它时。对于 SQL 服务器提供程序,仅支持配置为调用带有命名参数的存储过程的命令(其中 cmd.CommandType
设置 adCmdStoredProc
和 cmd.NamedParameters
设置 True
)。
对于像您这样的普通命令,服务器无法识别命名参数,只有 ?
占位符被识别为查询中的参数。
所以你应该尝试下面的方法。
Set cmd = Server.CreateObject("ADODB.Command")
With cmd
.ActiveConnection = db
.CommandText = "SELECT ? = '2019-01-01'"
.Parameters.Append(.CreateParameter("@dummyForServerNotForClient", adDBDate, adParamOutput))
.Execute
' must print: 2019-01-01
Response.Write .Parameters("@dummyForServerNotForClient").Value
End With
由于服务器会忽略参数名称,您可以通过省略参数名称来编写相同的代码,并使用参数在集合中的序号位置来访问参数的值。恕我直言,由于缺少明确命名的参数,代码变得更具逻辑性和可读性。
Set cmd = Server.CreateObject("ADODB.Command")
With cmd
.ActiveConnection = db
.CommandText = "SELECT ? = '2019-01-01'"
.Parameters.Append(.CreateParameter(, adDBDate, adParamOutput))
.Execute
' also must print: 2019-01-01
Response.Write .Parameters(0).Value
End With
我希望这有助于您理解这个概念。