473,396 Members | 1,707 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

getting return from srtored procedure

hi everyone i am very very new to the sql server (2 days actually and ) so
far i learned creating SPs etc but couldnt workout how to get return value
from my prodecure

my sp as follows
________________________
CREATE PROCEDURE [dbo].[page_all_artists]
@PageIndex INT,
@PageSize INT,
@Total INT OUTPUT
AS
BEGIN
WITH Entries AS (
SELECT ROW_NUMBER() OVER (ORDER BY ArtistID asc)
AS Row, Artist, SongCount
FROM artists_table)
SELECT Row, Artist, SongCount
FROM Entries
WHERE Row between
(@PageIndex - 1) * @PageSize + 1 and @PageIndex*@PageSize
-----------------THIS ONE TO GET TOTAL RECORD COUNT AND I HAVE NO IDE HOW
IT SHOULD WORK---------------------

Select @total=Count(ArtistID) from (Select ArtistID From Entries e group by
e.ArtistID) b;

END
__________________________________________________ _

and this is the asp page that i am usign to get record set
__________________________________________________ _

<%
objConn ="Provider=SQLOLEDB.1;" & _
"Data Source=MYSERVER;" & _
"Initial Catalog=MYdb;" & _
"User ID=MYID;" & _
"Password=MYPW"
Set objRS = Server.CreateObject("ADODB.Recordset")
strSQL = "page_all_artists " & startFrom & "," & perPage
objRS.Open strSQL, objConn

Do While Not objRS.EOF %>

<%=objRS("Artist")%>

<%
objRS.MoveNext
Loop
objRS.Close
Set objRS=Nothing
Set objConn=Nothing
%>
__________________________________________________ ______________

can somebody show me how can i get @Total value in to my asp code so i can
calculate paging.

Thanks in advance


Dec 11 '06 #1
5 2171

"noLoveLusT" <no***@here.comwrote in message
news:Qv*******************@fe3.news.blueyonder.co. uk...
hi everyone i am very very new to the sql server (2 days actually and ) so
far i learned creating SPs etc but couldnt workout how to get return
value from my prodecure

my sp as follows
________________________
CREATE PROCEDURE [dbo].[page_all_artists]
@PageIndex INT,
@PageSize INT,
@Total INT OUTPUT
AS
BEGIN
WITH Entries AS (
SELECT ROW_NUMBER() OVER (ORDER BY ArtistID asc)
AS Row, Artist, SongCount
FROM artists_table)
SELECT Row, Artist, SongCount
FROM Entries
WHERE Row between
(@PageIndex - 1) * @PageSize + 1 and @PageIndex*@PageSize
-----------------THIS ONE TO GET TOTAL RECORD COUNT AND I HAVE NO IDE HOW
IT SHOULD WORK---------------------

Select @total=Count(ArtistID) from (Select ArtistID From Entries e group
by e.ArtistID) b;

END
__________________________________________________ _

and this is the asp page that i am usign to get record set
__________________________________________________ _

<%
objConnstr ="Provider=SQLOLEDB.1;" & _
"Data Source=MYSERVER;" & _
"Initial Catalog=MYdb;" & _
"User ID=MYID;" & _
"Password=MYPW"
Set objRS = Server.CreateObject("ADODB.Recordset")
strSQL = "page_all_artists " & startFrom & "," & perPage
objRS.Open strSQL, objConn

Do While Not objRS.EOF %>

<%=objRS("Artist")%>

<%
objRS.MoveNext
Loop
objRS.Close
Set objRS=Nothing
Set objConn=Nothing
%>
__________________________________________________ ______________

can somebody show me how can i get @Total value in to my asp code so i can
calculate paging.
Your procedure creates two recordsets that are returned at the same time.
The contents of the second one can be accessed through the NextRecordSet
method:

<%
Set ObjConn = Server.CreateObject("ADODB.Connection")
objConnStr ="Provider=SQLOLEDB.1;" & _
"Data Source=MYSERVER;" & _
"Initial Catalog=MYdb;" & _
"User ID=MYID;" & _
"Password=MYPW"
Set objRS = Server.CreateObject("ADODB.Recordset")
objConn.Open objConnStr
objConn.page_all_artists startFrom, perPage, objRS

Do While Not objRS.EOF
Response.Write objRS("Artist") & "<br>"
objRS.MoveNext
Loop

Set objRS = objRS.NextRecordSet
Response.Write "Total records: " & objRS(0)

objRS.Close
Set objRS=Nothing
Set objConn=Nothing
%>

Assuming you would actually want to know the total number of records before
you processed them, you might want to change the order of the recordsets in
the stored proc, or you can use GetRows to put the first recordset into an
array for later use, then access the contents of the second recordset.

--
Mike Brind
Dec 11 '06 #2
Mike Brind wrote:
"noLoveLusT" <no***@here.comwrote in message
news:Qv*******************@fe3.news.blueyonder.co. uk...
>hi everyone i am very very new to the sql server (2 days actually
and ) so far i learned creating SPs etc but couldnt workout how to
get return value from my prodecure

my sp as follows
________________________
CREATE PROCEDURE [dbo].[page_all_artists]
@PageIndex INT,
@PageSize INT,
@Total INT OUTPUT
AS
BEGIN
WITH Entries AS (
SELECT ROW_NUMBER() OVER (ORDER BY ArtistID asc)
AS Row, Artist, SongCount
FROM artists_table)
SELECT Row, Artist, SongCount
FROM Entries
WHERE Row between
(@PageIndex - 1) * @PageSize + 1 and @PageIndex*@PageSize
-----------------THIS ONE TO GET TOTAL RECORD COUNT AND I HAVE NO
IDE HOW IT SHOULD WORK---------------------

Select @total=Count(ArtistID) from (Select ArtistID From Entries e
group by e.ArtistID) b;

END
<snip>
>
Your procedure creates two recordsets that are returned at the same
time. The contents of the second one can be accessed through the
NextRecordSet method:
Huh? I only see one resultset, unless you are talking about the
informational message returned as a closed recordset because of the lack of
"set nocount on"..
He's using an output parameter to return the total records.

--
Microsoft MVP - ASP/ASP.NET
Please reply to the newsgroup. This email account is my spam trap so I
don't check it very often. If you must reply off-line, then remove the
"NO SPAM"
Dec 11 '06 #3
noLoveLusT wrote:
hi everyone i am very very new to the sql server (2 days actually and
) so far i learned creating SPs etc but couldnt workout how to get
return value from my prodecure

my sp as follows
________________________
CREATE PROCEDURE [dbo].[page_all_artists]
@PageIndex INT,
@PageSize INT,
@Total INT OUTPUT
AS
BEGIN
The first line here should be:
SET NOCOUNT ON

to prevent the spurious "x rows effected" messages from being returned to
the client as closed recordsets
WITH Entries AS (
SELECT ROW_NUMBER() OVER (ORDER BY ArtistID asc)
AS Row, Artist, SongCount
FROM artists_table)
SELECT Row, Artist, SongCount
FROM Entries
WHERE Row between
(@PageIndex - 1) * @PageSize + 1 and @PageIndex*@PageSize
-----------------THIS ONE TO GET TOTAL RECORD COUNT AND I HAVE NO
IDE HOW IT SHOULD WORK---------------------

Select @total=Count(ArtistID) from (Select ArtistID From Entries e
group by e.ArtistID) b;
I've never used CTEs ... is Entries still available at this point? Does this
procedure do what you want in Query Analyzer? Test it like this:

DECLARE @total int
EXEC page_all_artists '20060101',25, @total output
select @total as Total

<snip>

You need to use an explicit Command object, appending Parameter objects to
its Parameters collection in order to retrieve the value of the output
parameter. Coding these Parameter objects is tedious and error-prone so I
created a utility page to do it for me. You can get it from:
http://common.mvps.org/barrowsb/Clas..._generator.zip

Using your procedure's declaration, this is the output of the code
generator:

Dim cmd, param

Set cmd=server.CreateObject("ADODB.Command")
With cmd
.CommandType=adcmdstoredproc
.CommandText = "page_all_artists"
set .ActiveConnection=cnSQL
set param = .createparameter("@RETURN_VALUE", adInteger, _
adParamReturnValue, 0)
.parameters.append param
set param = .createparameter("@PageIndex", adInteger, _
adParamInput, 0, [put value here])
.parameters.append param
set param = .createparameter("@PageSize", adInteger, _
adParamInput, 0, [put value here])
.parameters.append param
set param = .createparameter("@Total", adInteger, _
adParamInputOutput, 0, [put value here])
.parameters.append param
.execute ,,adexecutenorecords
end with

This assumes you have the ADO constants defined either for the page or the
application - see http://www.aspfaq.com/show.asp?id=2112

You would modify it as follows:

Dim cmd, param

Set cmd=server.CreateObject("ADODB.Command")
With cmd
.CommandType=adcmdstoredproc
.CommandText = "page_all_artists"
set .ActiveConnection=objConn
set param = .createparameter("@RETURN_VALUE", adInteger, _
adParamReturnValue, 0)
.parameters.append param
set param = .createparameter("@PageIndex", adInteger, _
adParamInput, 0, startFrom )
.parameters.append param
set param = .createparameter("@PageSize", adInteger, _
adParamInput, 0, perPage)
.parameters.append param
set param = .createparameter("@Total", adInteger, _
adParamOutput)
.parameters.append param
set objRS = .execute
'the procedure returns records so don't specify no-records
end with

The first step is to process and close the recordset so the output parameter
value will be retrieved. I typically do this by using a GetRows array:

dim arData
if not objRS.eof then arData = objRS.GetRows
objRS.close: set objRS = nothing

Then get the output parameter value - since that was the last parameter
defined, param still refers to it so:
dim total: total = param.value

If it wasn't the last parameter, then get it explicitly by:
dim total: total = cmd.parameters("@Total").value

--
Microsoft MVP - ASP/ASP.NET
Please reply to the newsgroup. This email account is my spam trap so I
don't check it very often. If you must reply off-line, then remove the
"NO SPAM"
Dec 11 '06 #4
Oh my god ! i fee so dumb :)

i couldnt get it working :S. all i want is to get a return rom my
stored procedure so i can calculate my paging

Select @total=Count(ArtistID) from (Select ArtistID From Entries e
group by e.ArtistID) b;

i found this on the net and trying to get it working but i also noticed
that i can get it as pard of record set see :
http://www.4guysfromrolla.com/webtech/062899-1.shtml
there

-- Now, return the set of paged records, plus, an indiciation of we
-- have more records or not!
SELECT *,
MoreRecords =
(
SELECT COUNT(*)
FROM #TempItems TI
WHERE TI.ID >= @LastRec
)
FROM #TempItems
WHERE ID @FirstRec AND ID < @LastRe
loogs like what i am looking for
so i have used it as follows and it didnt work either. there was
noreturn as objRs("MoreRecords")

set ANSI_NULLS ON
set QUOTED_IDENTIFIER ON
go


ALTER PROCEDURE [dbo].[page_all_artists]
@PageIndex INT,
@PageSize INT
AS

BEGIN

WITH Entries AS (
SELECT ROW_NUMBER() OVER (ORDER BY ArtistID asc)
AS Row, ArtistID,Artist, SongCount
FROM artists_table)

SELECT Row, ArtistID,Artist, SongCount,

MoreRecords = (
SELECT COUNT(*)
FROM entries TI
WHERE TI.ArtistID >= @PageIndex*@PageSize
)

FROM Entries
WHERE Row between
(@PageIndex - 1) * @PageSize + 1 and @PageIndex*@PageSize

END

Dec 11 '06 #5
Thanks to everyone i have managed to get row count as follows, i can get
total from server as recordset and move to next record set

set ANSI_NULLS ON

set QUOTED_IDENTIFIER ON

go

ALTER PROCEDURE [dbo].[search_with_like]

@PageIndex INT,

@PageSize INT,

@q varchar(100),

@Total INT OUTPUT

AS

SET NOCOUNT ON

BEGIN

SELECT Count(LyricID) FROM lyrics_table where SongName LIKE '%' +@q + '%'

SET @Total = @@ROWCOUNT;

WITH Entries AS (

SELECT ROW_NUMBER() OVER (ORDER BY LyricID asc)

AS Row,LyricID, Artist, SongName

FROM lyrics_table where SongName LIKE '%' +@q + '%')

SELECT Row, LyricID,Artist, SongName

FROM Entries

WHERE Row between

(@PageIndex - 1) * @PageSize + 1 and @PageIndex*@PageSize

END







<no********@gmail.comwrote in message
news:11*********************@f1g2000cwa.googlegrou ps.com...
Oh my god ! i fee so dumb :)

i couldnt get it working :S. all i want is to get a return rom my
stored procedure so i can calculate my paging

Select @total=Count(ArtistID) from (Select ArtistID From Entries e
group by e.ArtistID) b;

i found this on the net and trying to get it working but i also noticed
that i can get it as pard of record set see :
http://www.4guysfromrolla.com/webtech/062899-1.shtml
there

-- Now, return the set of paged records, plus, an indiciation of we
-- have more records or not!
SELECT *,
MoreRecords =
(
SELECT COUNT(*)
FROM #TempItems TI
WHERE TI.ID >= @LastRec
)
FROM #TempItems
WHERE ID @FirstRec AND ID < @LastRe
loogs like what i am looking for
so i have used it as follows and it didnt work either. there was
noreturn as objRs("MoreRecords")

set ANSI_NULLS ON
set QUOTED_IDENTIFIER ON
go


ALTER PROCEDURE [dbo].[page_all_artists]
@PageIndex INT,
@PageSize INT
AS

BEGIN

WITH Entries AS (
SELECT ROW_NUMBER() OVER (ORDER BY ArtistID asc)
AS Row, ArtistID,Artist, SongCount
FROM artists_table)

SELECT Row, ArtistID,Artist, SongCount,

MoreRecords = (
SELECT COUNT(*)
FROM entries TI
WHERE TI.ArtistID >= @PageIndex*@PageSize
)

FROM Entries
WHERE Row between
(@PageIndex - 1) * @PageSize + 1 and @PageIndex*@PageSize

END

Dec 11 '06 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Vipul Pathak | last post by:
Hello Friends ! I have the Following Code, that Executes a Stored Procedure and Attempt to read a Returned Integer Value from the StoredProc. But It gives Error ... ADODB.Command (0x800A0BB9)...
4
by: Richard G | last post by:
I'm a database guy, so go easy on me here. :) How can I get the rowcount of the affected rows of a SQL statement from a stored procedure call? I know that "set nocount on" does not return the...
1
by: Greg Smith | last post by:
I am trying to write a SQL query that will return a record count to my C# application. I wrote the following query: ALTER PROCEDURE up_justification_duplicate AS SELECT COUNT(*) FROM...
4
by: Mike Dinnis | last post by:
Hi, I've been working through a number of turorials to try to learn more about retrieving data from a SQL database. I think i've mastered techniques where i create a sql string in the page and...
2
by: Martin Raychev | last post by:
Hi all, I have the following problem: I have a private method that returns a SqlDataReader. For this to work I have not to close the DB connection in the above method. I do this only to
6
by: Max | last post by:
Anyone know why I'm always getting 0 returned? My stored procedure returns -1. Dim iErrorCode As Int32 iErrorCode = Convert.ToInt32(SqlHelper.ExecuteScalar(AppVars.strConn, _ "gpUpdateMember",...
6
by: Uday | last post by:
Hi everyone, I have a ASP page that triggers a db-side stored procedure. At the end of the procedure, it spits out a log file, that this ASP page reads and displays for the users. But the...
5
by: dgleeson3 | last post by:
Hello all I have used the LoginForm1 class in a Visual studio 2005 VB application. Its the standard Username, Pasword request for user input. I was hoping to use property procedures to get...
1
by: raghuvendra | last post by:
Hi I have a jsp page with 4 columns: namely Category name , Category order, Input field and a submit button. All these are aligned in a row. And Each Category Name has its corresponding Category...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.