我有一个Insert语句(存储过程),它在插入后返回SCOPE_IDENTITY(),但是,我试图弄清楚如何在经典ASP中使用ADODB.Command
请求后返回它。
SP最后包括:
SET @LastID = SCOPE_IDENTITY() -- @LastID is an INT OUTPUT param.
这是在经典ASP代码中处理插入查询时调用的内容:
set SQLCOMM = Server.CreateObject("ADODB.Command")
SQLCOMM.ActiveConnection = CONNSTRING
SQLCOMM.CommandText = "Insert_SP_Returns_ScopeID"
SQLCOMM.CommandType = 1
SQLCOMM.CommandTimeout = 0
SQLCOMM.Prepared = true
LastIDParameter = SQLCOMM.CreateParameter("@LastID",adInteger,adParamOutput)
SQLCOMM.Parameters.Add(LastIDParameter)
SQLCOMM.Execute()
LastID = LastIDParameter.Value
set SQLCOMM=Nothing
我是否在set SQLCOMM=Nothing
之前做了这样的事情?
NewID = SQLCOMM("LastID").Value
OR ...可以从Adodb.Recordset
而不是ADODB.Command
执行插入/更新存储过程吗?
The example above gives the following error message:
ADODB.Command error '800a0bb9'
Arguments are of the wrong type, are out of acceptable range,
or are in conflict with one another.
由于这行代码而返回错误:
LastIDParameter = SQLCOMM.CreateParameter("@LastID",adInteger,adParamOutput)
答案 0 :(得分:0)
调用存储过程的命令方法很好。您所要做的就是在命令对象中添加一个额外的输出参数。 (我多年没有做任何vbscript,所以我不确定你是否应该明确键入你的变量)。
在VB脚本中
set SQLCOMM = Server.CreateObject("ADODB.Command")
SQLCOMM.ActiveConnection = CONNSTRING
SQLCOMM.CommandText = "Insert_SP_Returns_ScopeID"
SQLCOMM.CommandType = 1
SQLCOMM.CommandTimeout = 0
SQLCOMM.Prepared = true
Dim LastIDParameter
Dim LastID
LastIDParameter = SQLCOMM.CreateParameter("@LastID",adInteger,adParamOutput)
SQLCOMM.Parameters.Add(LastIDParameter)
SQLCOMM.Execute()
LastID = LastIDParameter.Value
set SQLCOMM=Nothing
然后在您的存储过程中。
CREATE PROCEDURE Insert_SP_Returns_ScopeID
@Value1 int,
@Value2 int,
@LastID int OUTPUT
AS
INSERT INTO TableName(Value1,Value2) VALUES (@Value1,@Value2)
SET @LastID = SCOPE_IDENTITY()
编辑:您可能需要查找adInteger,adParamOutput的值并使用这些值,因为可能没有为您的环境定义常量。如果是这样的话......
SQLCOMM.CreateParameter("@LastID",3,2)
或定义常量。