在经典asp中插入后无法获取scope_identity
我必须为经典 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
如果您想返回记录集中的 SCOPE_IDENTITY ,您的存储过程将需要进行一些更改。当前实现 IDENTITY 的方式是作为 RETURN_VALUE 参数返回,您必须使用 ADO Command 对象来检索该参数。返回值将是 命令中的第一个参数.Parameters集合。请参阅以下文章< /a> 为例。
如果您想返回记录集中的结果,请尝试更改该行
并
删除 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
to
and removing the return statement.
只需使用
带有“;”的 Sql 插入查询
并使用波纹管方法
效果很好。我测试过。
Just Use
Your Sql Insert Query with ";"
And use bellow method
It works fine. I tested.
在存储过程中使用
RETURN
通常用于返回状态或错误代码。如果您想从存储过程中以记录集形式返回数据,请使用SELECT
而不是RETURN
。更新:听起来好像仍然没有发现您的存储过程现在正在返回记录集。根据 ADO 命令的 MSDN 文档:
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, useSELECT
instead ofRETURN
.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: