【问题标题】:Returning SCOPE_IDENTITY() using Adodb.Command使用 Adodb.Command 返回 SCOPE_IDENTITY()
【发布时间】:2011-08-30 17:43:45
【问题描述】:

我有一个插入语句(存储过程),它在插入后返回 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

或者...可以从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)

【问题讨论】:

    标签: sql-server tsql ado


    【解决方案1】:

    您调用存储过程的命令方法很好。您所要做的就是向命令对象添加一个额外的输出参数。 (我多年来没有做过任何 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)
    

    或定义常量。

    【讨论】:

    • 实际上,我收到了一个错误:ADODB.Command error '800a0bb9' 参数类型错误、超出可接受范围或相互冲突。导致错误的行:LastIDParameter = SQLCOMM.CreateParameter("@LastID",adInteger,adParamOutput)
    猜你喜欢
    • 2015-09-05
    • 1970-01-01
    • 1970-01-01
    • 2018-02-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-02-03
    相关资源
    最近更新 更多