Ask a Question related to ASP Database, Design and Development.
-
Ken VdB #1
RecordCount with Stored Procedure in SQL Server
Hi everyone,
I have noticed that if I used a stored procedure to populate an ADO
RecordSet it only returns a .RecordCount property if that stored procedure
contains nothing but a SELECT statement. For example, I am using the
following stored procedure to get a list of items in a particular product
category. If I eliminate the part that sets the @OrderDate to the current
date, it returns a proper RecordCount. If I leave it in there (which I need
to do), I get (-1) ever time. This is a problem because then I cannot use
the RecordCount property to warn the user that they are not seeing all
items. Any thoughts are welcome.
Cheers,
Ken.
CREATE PROCEDURE [dbo].[FL_KenTest]
(@CategoryCode VARCHAR(5),
@OrderDate SMALLDATETIME = NULL)
AS
BEGIN
IF @OrderDate IS NULL
SET @OrderDate = GETDATE()
--Select just enough items to make the limit of 200 items per
--category go over so the .RecordCount property can be used to
--warn us. But don't waste resources transferring excessive
--amounts of data between tiers.
SELECT TOP 201
p.ItemNumber,
Description
FROM Products
WHERE CategoryCode = @CategoryCode
END
Ken VdB Guest
-
SQL Server Stored Procedure Authentication
Hello, I am tyring to add a level of security to my application with using a username and password to authenticate to the database with when... -
Legacy ASP, SQL Server, Paging Stored Procedure - resend
I'm not sure if this made it so I'm resending it (with correction): This is the best I could come up with. The only other solutions I could think... -
Inserting Full Stops into SQL Server 2000 using ASP and stored procedure
Hi All, I am attempting to use a standard HTML form to pass a parameter to an ASP stored procedure, which searches a database for customer... -
Deploying a Db2 PL stored procedure on the production server
We are setting up a DB2 (UDB 8.1) environment. I need some guidance to set up the development and deployment process. Lets say I have a... -
Stored procedure help, makes server jump to 100%
Hello, we have a users table with the following fields: client_id varchar 20 counter1 int counter2 int .. .. .. counter50 int -
Aaron Bertrand - MVP #2
Re: RecordCount with Stored Procedure in SQL Server
Ken, can you show table structure (CREATE TABLE), some sample data (INSERT
statements), and the actual code used to call the stored procedure. We
can't reproduce, never mind fix, what we can't see.
(As an aside, instead of using recordcount on the client to see if you
overstepped the boundary, why not just SELECT TOP 200, or SET ROWCOUNT 200?
You could also pass back a separate value in the select statement that tells
you whether or not the actual rowcount may have been higher than the
restricted resultset.)
"Ken VdB" <puffthemagicdragon@nospam.floridus.com> wrote in message
news:uYH$hsylDHA.2820@TK2MSFTNGP10.phx.gbl...need> Hi everyone,
>
> I have noticed that if I used a stored procedure to populate an ADO
> RecordSet it only returns a .RecordCount property if that stored procedure
> contains nothing but a SELECT statement. For example, I am using the
> following stored procedure to get a list of items in a particular product
> category. If I eliminate the part that sets the @OrderDate to the current
> date, it returns a proper RecordCount. If I leave it in there (which I> to do), I get (-1) ever time. This is a problem because then I cannot use
> the RecordCount property to warn the user that they are not seeing all
> items. Any thoughts are welcome.
>
> Cheers,
>
> Ken.
>
> CREATE PROCEDURE [dbo].[FL_KenTest]
> (@CategoryCode VARCHAR(5),
> @OrderDate SMALLDATETIME = NULL)
> AS
>
> BEGIN
> IF @OrderDate IS NULL
> SET @OrderDate = GETDATE()
>
> --Select just enough items to make the limit of 200 items per
> --category go over so the .RecordCount property can be used to
> --warn us. But don't waste resources transferring excessive
> --amounts of data between tiers.
> SELECT TOP 201
> p.ItemNumber,
> Description
> FROM Products
> WHERE CategoryCode = @CategoryCode
> END
>
>
Aaron Bertrand - MVP Guest
-
Tom B #3
Re: RecordCount with Stored Procedure in SQL Server
I think this should work. You'll then get a recordset that contains the
field TotalRecords in every row. I'm sure there's better ways...but...
CREATE PROCEDURE [dbo].[FL_KenTest]
(@CategoryCode VARCHAR(5),
@OrderDate SMALLDATETIME = NULL)
AS
BEGIN
IF @OrderDate IS NULL
SET @OrderDate = GETDATE()
DECLARE @TotalRecords
SELECT @TotalRecords=Count(*) FROM Products WHERE CategoryCode=@CategoryCode
--Select just enough items to make the limit of 200 items per
--category go over so the .RecordCount property can be used to
--warn us. But don't waste resources transferring excessive
--amounts of data between tiers.
SELECT TOP 201
p.ItemNumber,
Description, @TotalRecords
FROM Products
WHERE CategoryCode = @CategoryCode
END
"Ken VdB" <puffthemagicdragon@nospam.floridus.com> wrote in message
news:uYH$hsylDHA.2820@TK2MSFTNGP10.phx.gbl...need> Hi everyone,
>
> I have noticed that if I used a stored procedure to populate an ADO
> RecordSet it only returns a .RecordCount property if that stored procedure
> contains nothing but a SELECT statement. For example, I am using the
> following stored procedure to get a list of items in a particular product
> category. If I eliminate the part that sets the @OrderDate to the current
> date, it returns a proper RecordCount. If I leave it in there (which I> to do), I get (-1) ever time. This is a problem because then I cannot use
> the RecordCount property to warn the user that they are not seeing all
> items. Any thoughts are welcome.
>
> Cheers,
>
> Ken.
>
> CREATE PROCEDURE [dbo].[FL_KenTest]
> (@CategoryCode VARCHAR(5),
> @OrderDate SMALLDATETIME = NULL)
> AS
>
> BEGIN
> IF @OrderDate IS NULL
> SET @OrderDate = GETDATE()
>
> --Select just enough items to make the limit of 200 items per
> --category go over so the .RecordCount property can be used to
> --warn us. But don't waste resources transferring excessive
> --amounts of data between tiers.
> SELECT TOP 201
> p.ItemNumber,
> Description
> FROM Products
> WHERE CategoryCode = @CategoryCode
> END
>
>
Tom B Guest
-
Bob Barrows #4
Re: RecordCount with Stored Procedure in SQL Server
Ken VdB wrote:
Use SET NOCOUNT ON at the start of the procedure, after the AS keyword.> Create a stored procedure that selects any number of fields from any
> table. Use it to populate any kind of recordset other then "forward
> only" on the client and check your RecordCount property. Then
> declare any variable you like in the strored procedure and SET it to
> anything you like and watch your RecordCount property disapear
> (become -1). You don't even need to use the variable for anything
> else in the procedure. The data, table layout and particulars of
> exactly what the variable is used for for seem to be unimportant to
> this problem. I find it quite strange. I have attached some samples
> that use Pubs.
>
HTH,
Bob Barrows
--
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 Guest
-
Bob Barrows #5
Re: RecordCount with Stored Procedure in SQL Server
My advice is to stop using an expensive non-forwardonly cursor merely to get
a record count. There are other more efficient ways to get a record count.
My favorite is to use GetRows to stuff the data from the recordset into an
array, allowing me to close and destroy the recordset and connection
(allowing another thread to use the connection). With the GetRows array, it
is a simple matter to get the count:
Ubound(arResults,2) + 1
HTH,
Bob Barrows
--
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 Guest
-
Ken VdB #6
Re: RecordCount with Stored Procedure in SQL Server
This does not help.
"Bob Barrows" <reb01501@NOyahoo.SPAMcom> wrote in message
news:#MiXwyzlDHA.1072@TK2MSFTNGP09.phx.gbl...> Ken VdB wrote:> Use SET NOCOUNT ON at the start of the procedure, after the AS keyword.> > Create a stored procedure that selects any number of fields from any
> > table. Use it to populate any kind of recordset other then "forward
> > only" on the client and check your RecordCount property. Then
> > declare any variable you like in the strored procedure and SET it to
> > anything you like and watch your RecordCount property disapear
> > (become -1). You don't even need to use the variable for anything
> > else in the procedure. The data, table layout and particulars of
> > exactly what the variable is used for for seem to be unimportant to
> > this problem. I find it quite strange. I have attached some samples
> > that use Pubs.
> >
> HTH,
> Bob Barrows
> --
> 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.
>
>
Ken VdB Guest
-
Bob Barrows #7
Re: RecordCount with Stored Procedure in SQL Server
Ken VdB wrote:
Hmm. I could not get a recordcount with your code even with the SET> This does not help.
statement commented out.
The only way I was able to get a static cursor (check the cursortype
property before attempting to read the recordcount) was to use a client-side
cursor. This is not the way it is supposed to behave. I am going to need to
come back and look at this later when I have more time.
Bob Barrows
--
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 Guest
-
Ken Schaefer #8
Re: RecordCount with Stored Procedure in SQL Server
Don't use the .RecordCount property. It's very expensive.
Instead:
a) Use .GetRows (this gives you the data in a VBSciprt array), and
UBound(arrResults, 2) + 1 gives you the recordcount
b) Use a SELECT COUNT() statement if you just need a count (no data)
Cheers
Ken
"Ken VdB" <puffthemagicdragon@nospam.floridus.com> wrote in message
news:uYH$hsylDHA.2820@TK2MSFTNGP10.phx.gbl...
: Hi everyone,
:
: I have noticed that if I used a stored procedure to populate an ADO
: RecordSet it only returns a .RecordCount property if that stored procedure
: contains nothing but a SELECT statement. For example, I am using the
: following stored procedure to get a list of items in a particular product
: category. If I eliminate the part that sets the @OrderDate to the current
: date, it returns a proper RecordCount. If I leave it in there (which I
need
: to do), I get (-1) ever time. This is a problem because then I cannot use
: the RecordCount property to warn the user that they are not seeing all
: items. Any thoughts are welcome.
:
: Cheers,
:
: Ken.
:
: CREATE PROCEDURE [dbo].[FL_KenTest]
: (@CategoryCode VARCHAR(5),
: @OrderDate SMALLDATETIME = NULL)
: AS
:
: BEGIN
: IF @OrderDate IS NULL
: SET @OrderDate = GETDATE()
:
: --Select just enough items to make the limit of 200 items per
: --category go over so the .RecordCount property can be used to
: --warn us. But don't waste resources transferring excessive
: --amounts of data between tiers.
: SELECT TOP 201
: p.ItemNumber,
: Description
: FROM Products
: WHERE CategoryCode = @CategoryCode
: END
:
:
Ken Schaefer Guest
-
Bob Barrows #9
Re: RecordCount with Stored Procedure in SQL Server
Ken VdB wrote:
Well, I've learned something here. I've never tried creating a server-side> This does not help.
scrollable recordset from a stored procedure. I guess it is not possible.
The only way I can get a non-forwardonly cursor from a stored procedure is
to use a client-side cursor. Ken, this applies to whether or not there is a
parameter whose value is set in the procedure.
..
Here is the code I've tried. If anyone can see something that I've failed to
do here, I'd be glad to hear about it.
<%
Option explicit
Dim strConn, strSQL
Dim conn, rsTest
dim cmd
strConn = "provider=sqloledb;data source=xxx;user id = xxx;" & _
"password=xxx;initial catalog=pubs"
Set conn = Server.CreateObject("ADODB.Connection")
Set rsTest = Server.CreateObject("ADODB.Recordset")
conn.open strConn
Response.Write "Parameter Optional" & "<BR><BR>"
strSQL = "Exec RecordCountProblemDemo"
'rsTest.CursorLocation=adUseClient
rsTest.CursorType=adOpenStatic
Response.Write "Before open, using stored procedure executed via " & _
"dynamic sql, cursortype = " & _
rsTest.CursorType & "<BR>"
rsTest.Open strSQL, conn,,,adCmdText
Response.Write "After open, using stored procedure executed via " & _
"dynamic sql, cursor type = " & _
rsTest.CursorType & "; RecordCount = " & rsTest.RecordCount & "<BR><BR>"
rsTest.Close
rsTest.CursorType=adOpenStatic
Response.Write "Before open, using stored procedure executed via " & _
"procedure-as-connection-method, cursortype = " & _
rsTest.CursorType & "<BR>"
conn.RecordCountProblemDemo rsTest
Response.Write "After open, using stored procedure executed via " & _
"procedure-as-connection-method, cursor type = " & _
rsTest.CursorType & "; RecordCount = " & rsTest.RecordCount & "<BR><BR>"
rsTest.Close
set cmd=Server.CreateObject("adodb.command")
With cmd
Set .ActiveConnection = conn
.CommandText = "RecordCountProblemDemo"
.CommandType = adCmdStoredProc
End With
rsTest.CursorType=adOpenStatic
Response.Write "Before open, using stored procedure executed via " & _
"Command object, cursortype = " & _
rsTest.CursorType & "<BR>"
rsTest.Open cmd
Response.Write "After open, using stored procedure executed via " & _
"Command object, cursor type = " & _
rsTest.CursorType & "; RecordCount = " & rsTest.RecordCount & "<BR><BR>"
rsTest.Close
rsTest.CursorType=adOpenStatic
Response.Write "Before open, using dynamic sql, cursortype = " & _
rsTest.CursorType & "<BR>"
strSQL = "SELECT au_fname, au_lname FROM authors"
rsTest.Open strSQL, conn,,,adCmdText
Response.Write "After open, using dynamic sql, cursor type = " & _
rsTest.CursorType & "; RecordCount = " & rsTest.RecordCount & "<BR><BR><BR>"
Response.Write "Parameter Required" & "<BR><BR>"
strSQL = "Exec RecordCountProblemDemoParmRequired 'test'"
'rsTest.CursorLocation=adUseClient
rsTest.Close
rsTest.CursorType=adOpenStatic
Response.Write "Before open, using stored procedure executed via " & _
"dynamic sql, cursortype = " & _
rsTest.CursorType & "<BR>"
rsTest.Open strSQL, conn,,,adCmdText
Response.Write "After open, using stored procedure executed via " & _
"dynamic sql, cursor type = " & _
rsTest.CursorType & "; RecordCount = " & rsTest.RecordCount & "<BR><BR>"
rsTest.Close
rsTest.CursorType=adOpenStatic
Response.Write "Before open, using stored procedure executed via " & _
"procedure-as-connection-method, cursortype = " & _
rsTest.CursorType & "<BR>"
conn.RecordCountProblemDemoParmRequired "test",rsTest
Response.Write "After open, using stored procedure executed via " & _
"procedure-as-connection-method, cursor type = " & _
rsTest.CursorType & "; RecordCount = " & rsTest.RecordCount & "<BR><BR>"
rsTest.Close
With cmd
Set .ActiveConnection = conn
.CommandText = "RecordCountProblemDemoParmRequired"
.CommandType = adCmdStoredProc
.Parameters.append .createparameter("@test",advarchar,adparaminput,11 , _
"test")
End With
rsTest.CursorType=adOpenStatic
Response.Write "Before open, using stored procedure executed via " & _
"Command object, cursortype = " & _
rsTest.CursorType & "<BR>"
rsTest.Open cmd
Response.Write "After open, using stored procedure executed via " & _
"Command object, cursor type = " & _
rsTest.CursorType & "; RecordCount = " & rsTest.RecordCount & "<BR><BR>"
rsTest.Close
conn.Close
Set rsTest = Nothing
Set conn = Nothing
%>
create PROCEDURE [dbo].[RecordCountProblemDemo]
(@SomeVarThatDoesntGetUsed VARCHAR(11) = NULL)
AS
BEGIN
SET NOCOUNT ON
--activate the following two lines and the RecordCount stops working
IF @SomeVarThatDoesntGetUsed IS NULL
SET @SomeVarThatDoesntGetUsed = 'hello world'
SELECT au_fname, au_lname FROM authors
END
GO
create PROCEDURE [dbo].[RecordCountProblemDemoParmRequired]
(@SomeVarThatDoesntGetUsed VARCHAR(11))
AS
BEGIN
SET NOCOUNT ON
--activate the following two lines and the RecordCount stops working
IF @SomeVarThatDoesntGetUsed IS NULL
SET @SomeVarThatDoesntGetUsed = 'hello world'
SELECT au_fname, au_lname FROM authors
END
GO
Bob Barrows
--
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 Guest



Reply With Quote

