在经典asp中插入后无法获取scope_identity

发布于 2024-11-08 05:23:11 字数 905 浏览 0 评论 0原文

我必须为经典 asp 执行快速而肮脏的 sql 会话存储,因此我有以下存储过程来完成插入工作。当我从 sql management studio 或通过 asp 代码运行它时,它插入得很好,但没有返回scope_identity。奇怪的是,当我从代码执行该过程时,我在检查 rs.eof 的行上收到“对象关闭时不允许操作”错误消息。

create procedure InsertSessionValue

    @keyname varchar(50),
    @value varchar(max)

as

    declare @ID int
    delete from storedsessionsimple where datesaved < dateadd(d, -1, getdate())

    insert into dbo.StoredSessionSimple (SessionKey, SessionValue, DateSaved) values (@keyname, @value, getdate())

    set @ID = SCOPE_IDENTITY()

    return @ID

asp代码是...

set cnLocal = GetConnection()
    cnLocal.open()

    set rs = cnLocal.execute("exec InsertSessionValue '" & SessionKey & "','" & SessionValue & "'")
    if not rs.eof then
        id = rs(0)
    end if
    rs.close: set rs = nothing
    end if
    cnLocal.close: set cnLocal = nothing

I have to do a quick and dirty sql session storage for classic asp so I have the following stored procedure that does the inserting work. When I run it from sql management studio or through asp code, it inserts fine but it's not returning the scope_identity. What's odd is that when I execute the procedure from code I'm getting "operation is not allowed when the object is closed" error message on the line where it checks for rs.eof.

create procedure InsertSessionValue

    @keyname varchar(50),
    @value varchar(max)

as

    declare @ID int
    delete from storedsessionsimple where datesaved < dateadd(d, -1, getdate())

    insert into dbo.StoredSessionSimple (SessionKey, SessionValue, DateSaved) values (@keyname, @value, getdate())

    set @ID = SCOPE_IDENTITY()

    return @ID

and the asp code is...

set cnLocal = GetConnection()
    cnLocal.open()

    set rs = cnLocal.execute("exec InsertSessionValue '" & SessionKey & "','" & SessionValue & "'")
    if not rs.eof then
        id = rs(0)
    end if
    rs.close: set rs = nothing
    end if
    cnLocal.close: set cnLocal = nothing

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

锦欢 2024-11-15 05:23:12

如果您想返回记录集中的 SCOPE_IDENTITY ,您的存储过程将需要进行一些更改。当前实现 IDENTITY 的方式是作为 RETURN_VALUE 参数返回,您必须使用 ADO Command 对象来检索该参数。返回值将是 命令中的第一个参数.Parameters集合。请参阅以下文章< /a> 为例。

如果您想返回记录集中的结果,请尝试更改该行

set @ID = SCOPE_IDENTITY()

SELECT SCOPE_IDENTITY() AS NEW_ID

删除 return 语句。

Your stored procedure will need a couple changes if you want to return the SCOPE_IDENTITY in a recordset. The way it is currently implemented the IDENTITY is being returned as a RETURN_VALUE parameter, which you would have to retrieve using the ADO Command object. The return value will be the first parameter in the Command.Parameters collection. See the following article for an example.

If you want to return the results in a recordset then try changing the line

set @ID = SCOPE_IDENTITY()

to

SELECT SCOPE_IDENTITY() AS NEW_ID

and removing the return statement.

痴意少年 2024-11-15 05:23:12

只需使用

set nocount on ; 

带有“;”的 Sql 插入查询

set nocount off ;

并使用波纹管方法

Set rsInsert = conn.execute(SQLQuery)


if not rsInsert.EOF then
   response.write(rsInsert(0))
end if

效果很好。我测试过。

Just Use

set nocount on ; 

Your Sql Insert Query with ";"

set nocount off ;

And use bellow method

Set rsInsert = conn.execute(SQLQuery)


if not rsInsert.EOF then
   response.write(rsInsert(0))
end if

It works fine. I tested.

染年凉城似染瑾 2024-11-15 05:23:11

在存储过程中使用 RETURN 通常用于返回状态或错误代码。如果您想从存储过程中以记录集形式返回数据,请使用 SELECT 而不是 RETURN

更新:听起来好像仍然没有发现您的存储过程现在正在返回记录集。根据 ADO 命令的 MSDN 文档:

如果命令不打算返回结果(例如,SQL UPDATE 查询),只要指定了选项 adExecuteNoRecords,提供程序就不会返回 Nothing;否则,执行返回一个关闭的记录集。

Using RETURN in your stored proc is typically used for returning a status or error code. If you want to return data as a recordset from your stored proc, use SELECT instead of RETURN.

Update: It sounds like it's still not picking up that your stored procedure is now returning a recordset. According to the MSDN documentation for an ADO Command:

If the command is not intended to return results (for example, an SQL UPDATE query) the provider returns Nothing as long as the option adExecuteNoRecords is specified; otherwise Execute returns a closed Recordset.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文