从存储过程返回值 [英] Return value from Stored Procedure

查看:62
本文介绍了从存储过程返回值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个存储过程需要大量输入,批量插入
,然后输出一个记录集。当我在

服务器管理工​​作室中运行存储过程时,我也从存储的

过程中得到一个返回值,这是一个INT。


我想在我的ASP / VBScript页面上访问此返回值,但不要

知道如何访问它。


这是我的代码到目前为止:


<%

Dim rsImport

Dim rsImport_cmd

Dim rsImport_numRows


设置rsImport_cmd = Server.CreateObject(" ADODB.Command")

rsImport_cmd.ActiveConnection = MM_aclv4test_STRING

rsImport_cmd.CommandText =" ; {call dbo.PriceUpdateImport(?,?,?,?,?)}"

rsImport_cmd.Prepared = true

rsImport_cmd.Parameters.Append

rsImport_cmd.Parameters.Append rsImport_cmd.CreateParameter(" param1",

200,1,255,rsImport__vendor)

rsImport_cmd.Parameters.Append rsImport_cmd.CreateParameter (" param2",

200,1,255,rsImport __name)

rsImport_cmd.Parameters.Append rsImport_cmd.CreateParameter(" param3",

200,1,255,rsImport__filename)

rsImport_cmd。 Parameters.Append rsImport_cmd.CreateParameter(" param4",

200,1,255,rsImport__validfrom)

rsImport_cmd.Parameters.Append rsImport_cmd.CreateParameter(" param5",

200,1,255,rsImport__user)


设置rsImport = rsImport_cmd.Execute

rsImport_numRows = 0

%>


上面有更多代码为每个参数设置变量。


我试过添加这个:rsImport_cmd .CreateParameter(" @ RETURN_VALUE",

3,4)但是我收到了这个错误:


用于ODBC驱动程序的Microsoft OLE DB提供程序错误''80040e21 ''


多步OLE DB操作产生错误。检查每个OLE DB

状态值(如果有)。没有工作。


我的存储过程没有声明输出返回值,这可能是问题,但是当我执行存储时服务器中的程序

管理工作室给我一个返回值。


任何想法或指示?


干杯,


史蒂夫

I have a stored procedure that takes a number of inputs, does a bulk
insert, and then outputs a recordset. When I run the stored procedure in
Server Management Studio I also get a return value from the stored
procedure which is an INT.

I want to access this return value on my ASP/VBScript page, but do not
know how to access it.

Here is my code so far:

<%
Dim rsImport
Dim rsImport_cmd
Dim rsImport_numRows

Set rsImport_cmd = Server.CreateObject ("ADODB.Command")
rsImport_cmd.ActiveConnection = MM_aclv4test_STRING
rsImport_cmd.CommandText = "{call dbo.PriceUpdateImport(?,?,?,?,?)}"
rsImport_cmd.Prepared = true
rsImport_cmd.Parameters.Append
rsImport_cmd.Parameters.Append rsImport_cmd.CreateParameter("param1",
200, 1, 255, rsImport__vendor)
rsImport_cmd.Parameters.Append rsImport_cmd.CreateParameter("param2",
200, 1, 255, rsImport__name)
rsImport_cmd.Parameters.Append rsImport_cmd.CreateParameter("param3",
200, 1, 255, rsImport__filename)
rsImport_cmd.Parameters.Append rsImport_cmd.CreateParameter("param4",
200, 1, 255, rsImport__validfrom)
rsImport_cmd.Parameters.Append rsImport_cmd.CreateParameter("param5",
200, 1, 255, rsImport__user)

Set rsImport = rsImport_cmd.Execute
rsImport_numRows = 0
%>

There is more code above that sets the variable for each parameter.

I have tried adding this: rsImport_cmd.CreateParameter("@RETURN_VALUE",
3, 4) but I got this error:

Microsoft OLE DB Provider for ODBC Drivers error ''80040e21''

Multiple-step OLE DB operation generated errors. Check each OLE DB
status value, if available. No work was done.

My stored procedure doesn''t declare an output return value, which may be
the problem, but when I execute the stored procedure in server
management studio I am given a return value.

Any ideas or pointers?

Cheers,

Steve

推荐答案

Dooza写道:
Dooza wrote:

我有一个存储过程需要多个输入,批量插入,然后输出一个记录集。当我在Server Management Studio中运行存储过程

时,我也从存储的

过程获得一个返回值,这是一个INT。


我想在我的ASP / VBScript页面上访问此返回值,但不要

知道如何访问它。


这是我的代码到目前为止:


<%

Dim rsImport

Dim rsImport_cmd

Dim rsImport_numRows


设置rsImport_cmd = Server.CreateObject(" ADODB.Command")

rsImport_cmd.ActiveConnection = MM_aclv4test_STRING

rsImport_cmd.CommandText =" ; {call dbo.PriceUpdateImport(?,?,?,?,?)}"

rsImport_cmd.Prepared = true

rsImport_cmd.Parameters.Append

rsImport_cmd.Parameters.Append rsImport_cmd.CreateParameter(" param1",

200,1,255,rsImport__vendor)

rsImport_cmd.Parameters.Append rsImport_cmd.CreateParameter (" param2",

200,1,255,rsImport__na me)

rsImport_cmd.Parameters.Append rsImport_cmd.CreateParameter(" param3",

200,1,255,rsImport__filename)

rsImport_cmd。 Parameters.Append rsImport_cmd.CreateParameter(" param4",

200,1,255,rsImport__validfrom)

rsImport_cmd.Parameters.Append rsImport_cmd.CreateParameter(" param5",

200,1,255,rsImport__user)


设置rsImport = rsImport_cmd.Execute

rsImport_numRows = 0

%>


上面有更多代码为每个参数设置变量。


我试过添加这个:

rsImport_cmd.CreateParameter(" @ RETURN_VALUE",3,4)但我得到了这个

错误:
I have a stored procedure that takes a number of inputs, does a bulk
insert, and then outputs a recordset. When I run the stored procedure
in Server Management Studio I also get a return value from the stored
procedure which is an INT.

I want to access this return value on my ASP/VBScript page, but do not
know how to access it.

Here is my code so far:

<%
Dim rsImport
Dim rsImport_cmd
Dim rsImport_numRows

Set rsImport_cmd = Server.CreateObject ("ADODB.Command")
rsImport_cmd.ActiveConnection = MM_aclv4test_STRING
rsImport_cmd.CommandText = "{call dbo.PriceUpdateImport(?,?,?,?,?)}"
rsImport_cmd.Prepared = true
rsImport_cmd.Parameters.Append
rsImport_cmd.Parameters.Append rsImport_cmd.CreateParameter("param1",
200, 1, 255, rsImport__vendor)
rsImport_cmd.Parameters.Append rsImport_cmd.CreateParameter("param2",
200, 1, 255, rsImport__name)
rsImport_cmd.Parameters.Append rsImport_cmd.CreateParameter("param3",
200, 1, 255, rsImport__filename)
rsImport_cmd.Parameters.Append rsImport_cmd.CreateParameter("param4",
200, 1, 255, rsImport__validfrom)
rsImport_cmd.Parameters.Append rsImport_cmd.CreateParameter("param5",
200, 1, 255, rsImport__user)

Set rsImport = rsImport_cmd.Execute
rsImport_numRows = 0
%>

There is more code above that sets the variable for each parameter.

I have tried adding this:
rsImport_cmd.CreateParameter("@RETURN_VALUE", 3, 4) but I got this
error:



return参数应该是您追加的第一个参数。你将会赞赏我的免费Command对象代码生成器

这里: http://common.mvps.org/barrowsb/Clas..._generator.zip


我的存储过程没有声明输出返回值,这可能是问题,
My stored procedure doesn''t declare an output return value, which may
be the problem,



编号SQL Server的特性就是它不会发送输出或

返回参数,直到程序生成的所有结果集都被调用者消耗掉了。这是在

查询管理器中为您完成的。在ADO代码中,您必须自己执行此操作。您必须

导航到记录集中的最后一条记录,或者在尝试读取输出或返回参数之前关闭

记录集

值。切换到客户端游标可确保消耗当前的结果集。


此外,您需要确保该过程不生成任何

意外的结果集。 SQL Server的另一个特点是它会发送那些x行受到影响的消息。消息给调用者关闭

结果集。如果您的ADO代码没有处理它们,您将永远不会看到输出或返回参数

。所以,你的标准做法应该是通过包括SET NOCOUNT ON行来消除这些消息。在你的程序开始的




-

Microsoft MVP - ASP / ASP.NET

请回复新闻组。我的From

标题中列出的电子邮件帐户是我的垃圾邮件陷阱,因此我不经常检查它。通过发布到新闻组,您将获得更快的回复。

No. A peculiarity of SQL Server is that it will not send output or
return parameters until all resultsets generated by the procedure have
been consumed by the caller. This is done for you behind the scenes in
Query Manager. In ADO code, you have to do this yourself. You have to
either navigate to the last record in the recordset, or close the
recordset, before attempting to read the output or return parameter
values. Switching to a client-side cursor guarantees that the current
resultset is consumed.

Moreover, you need to make sure the procedure is not generating any
unexpected resultsets. Another peculiarity of SQL Server is that it
sends those "x rows were affected" messages to the caller as closed
resultsets. If your ADO code does not handle them, you will never see
the output or return parameters. So, your standard practice should be to
suppress those messages by including the line "SET NOCOUNT ON" at the
beginning of your procedure.

--
Microsoft MVP -- ASP/ASP.NET
Please reply to the newsgroup. The email account listed in my From
header is my spam trap, so I don''t check it very often. You will get a
quicker response by posting to the newsgroup.


Bob Barrows [MVP]写道:
Bob Barrows [MVP] wrote:

返回参数应该是您追加的第一个参数。你将会赞赏我的免费Command对象代码生成器

这里: http://common.mvps.org/barrowsb/Clas..._generator.zip



谢谢鲍勃,我我会试一试。

Thanks Bob, I will give it a go.


>我的存储过程没有声明输出返回值,这可能是
成为问题,
>My stored procedure doesn''t declare an output return value, which may
be the problem,



否.SQL Server的一个特点是,在生成所有结果集之前,它不会发送输出或

返回参数通过程序已经被调用者消耗了
。这是在

查询管理器中为您完成的。在ADO代码中,您必须自己执行此操作。您必须

导航到记录集中的最后一条记录,或者在尝试读取输出或返回参数之前关闭

记录集

值。切换到客户端游标可确保消耗当前的

结果集。

No. A peculiarity of SQL Server is that it will not send output or
return parameters until all resultsets generated by the procedure have
been consumed by the caller. This is done for you behind the scenes in
Query Manager. In ADO code, you have to do this yourself. You have to
either navigate to the last record in the recordset, or close the
recordset, before attempting to read the output or return parameter
values. Switching to a client-side cursor guarantees that the current
resultset is consumed.



Aha,馅饼的缺失部分,将解释它。

Aha, the missing piece of the pie, that will explain it.


此外,你需要确保该过程不会产生任何意外的结果集。 SQL Server的另一个特点是它会发送那些x行受到影响的消息。消息给调用者关闭

结果集。如果您的ADO代码没有处理它们,您将永远不会看到输出或返回参数

。所以,你的标准做法应该是通过包括SET NOCOUNT ON行来消除这些消息。在你的程序开始的


Moreover, you need to make sure the procedure is not generating any
unexpected resultsets. Another peculiarity of SQL Server is that it
sends those "x rows were affected" messages to the caller as closed
resultsets. If your ADO code does not handle them, you will never see
the output or return parameters. So, your standard practice should be to
suppress those messages by including the line "SET NOCOUNT ON" at the
beginning of your procedure.



这就是我一直让Server Studio Manager为我做的事情

我创建SP的时间......我没有知道它是什么开始,所以

用Google搜索并意识到它的重要性。


感谢您的建议,我会告诉您我的意思继续。


Steve

Thats something I have always let Server Studio Manager do for me each
time I create an SP... I didn''t know what it was to begin with, so
Googled it and realised how important it was.

Thanks for the advice, I will let you know how I get on.

Steve


Bob Barrows [MVP]写道:
Bob Barrows [MVP] wrote:

返回参数应该是您追加的第一个参数。你将会赞赏我的免费Command对象代码生成器

这里: http://common.mvps.org/barrowsb/Clas..._generator.zip



鲍勃,只是为了让你知道这在Firefox中没有做任何事情,但它确实可以在Internet Explorer中运行。


Steve

Bob, just to let you know this doesn''t do anything in Firefox, but it
does work in Internet Explorer.

Steve


这篇关于从存储过程返回值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆