使用输入参数调用 SQL 存储过程

Calling SQL stored procedure with input parameter

我需要编写一点 VBScript 来调用带有输入变量的 SQL 过程。我对 VBscript 没有太多经验,但我遇到了一个无法找到解决方案的错误。我收到的错误是:

OnNewRecord (Line 14): [ODBC Firebird Driver][Firebird]Dynamic SQL Error Input parameter mismatch for procedure TEST

我写的VBScript是:

Const Connection = "DRIVER=Firebird/InterBase(r) driver;UID=SYSDBA;PWD=masterkey;DBNAME=C:\Users\wouter\FOR-TESTING.fdb;"
Const adParamInput = 1
Const adInteger = 3
Set myConn = CreateObject("ADODB.Connection")
myConn.Open Connection

Set spCommand = CreateObject("ADODB.Command")
spCommand.Commandtext = "TEST" 
spCommand.CommandType = 4

Set parameter = spCommand.CreateParameter(, adInteger, adParamInput, 4, 5)

Set spCommand.ActiveConnection = myConn
spCommand.Execute 
myConn.Close

此 VBscript 正在调用以下过程:

CREATE OR ALTER PROCEDURE TEST (ID INTEGER)
AS BEGIN
UPDATE MATERIAL_LABEL SET PRINTED = 'T', HANDLED_DATE = CURRENT_TIMESTAMP 
WHERE ID = :ID;
END

ID INTEGER 应该是一个变量输入,但为了测试代码,我使用了数值 5。我认为它与需要不同类型输入的过程有关,但我不能找到哪个。

我已经通过使用不同的方法插入我的数值解决了这个问题:

Const Connection = "DRIVER=Firebird/InterBase(r) driver;UID=SYSDBA;PWD=masterkey;DBNAME=C:\Users\wouter\FOR-TESTING.fdb;"
Set myConn = CreateObject("ADODB.Connection")

myConn.Open Connection

SQL = "execute procedure TEST 5"
Set dbconn = CreateObject("ADODB.Connection")
dbconn.Open connection
dbconn.Execute(SQL)
myConn.Close

-- 更新--

自从 Ansgar Wiechers 和 Mihai Adrian 告诉我错误的原因并且 Geert bellekens 警告我存在 SQL 注入的风险后,我尝试将 "append line" 添加到我的 VBScript 中,这也工作完美!

在此我完成的 VBScript:

Const Connection = "DRIVER=Firebird/InterBase(r) driver;UID=SYSDBA;PWD=masterkey;DBNAME=C:\Users\wouter\FOR-TESTING.fdb;"
Const adParamInput = 1
Const adInteger = 3
Set myConn = CreateObject("ADODB.Connection")
myConn.Open Connection

Set spCommand = CreateObject("ADODB.Command")
spCommand.Commandtext = "TEST" 
spCommand.CommandType = 4

Set parameter = spCommand.CreateParameter(, adInteger, adParamInput, 4, 5)
spCommand.Parameters.Append parameter

Set spCommand.ActiveConnection = myConn
spCommand.Execute 
myConn.Close

谢谢!

正如 Ansgar Wiechers 所说,您必须通过附加参数将参数传递给存储过程:

spCommand.Parameters.Append parameter