473,395 Members | 1,456 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,395 software developers and data experts.

Why is my Output Parameter not working?

Hi,

I am currently creating an ASP page that returns a recordset of search
result based on multiple keywords. The where string is dynamically
built on the server page and that part work quite well. However I want
to also return the number of records in the recordset and I can not
manage to get any output parameter working. Even if I assign SELECT
@mycount=100 (or SET @mycount=100) in the SP the only value set in
mycount is always NULL. I tested various theories (e.g. early exit,
order & naming of parameters etc. but I can not make the SP set the
output parameters - has it anything to do with the execute?). @mycount
also returns NULL if I test it in SQL QA.

What's wrong with this SP (as regards mycount):

CREATE PROCEDURE dbo.spFindProducts
@mycount integer OUTPUT,
@whereString varchar (1000)
AS

--SET NOCOUNT ON

--Set a Default value for the Wherestring which will return all records
if the Wherestring is blank
IF @whereString is Null
SELECT @whereString = 'AND TblProduct.ProductID is not null'

--Declare a variable to hold the concatenated SQL string
DECLARE @SQL varchar(2500)

-- AND (((UPPER([TblProduct].[ProductName] + [ProductGroupCode] +
[AttributeValue1] +
-- [SearchWords] + [tblSupplier].[SupplierCode] + [SupplierDesc]))
Like '%screw%'))

SELECT @SQL = 'SELECT TblProduct.ProductID, TblProduct.ProductName,
TblProduct.ChapterCode, tblProduct.ProductGroupCode' +
' FROM (TblProduct LEFT JOIN TblStockItem ON TblProduct.ProductID =
TblStockItem.ProductID) ' +
' LEFT JOIN tblSupplier ON TblProduct.SupplierCode =
tblSupplier.SupplierCode' +
' WHERE 1=1 ' + @whereString +
' GROUP BY TblProduct.ProductID, TblProduct.ProductName,
TblProduct.ChapterCode, TblProduct.ProductGroupCode'

SELECT @mycount = 200; -- test

execute (@SQL);
-- next line seems to be ignored ?
SELECT @mycount = @@rowcount;
GO

tia
Axel

Jul 23 '05 #1
11 11064
Axel (re***********@hotmail.com) writes:
I am currently creating an ASP page that returns a recordset of search
result based on multiple keywords. The where string is dynamically
built on the server page and that part work quite well. However I want
to also return the number of records in the recordset and I can not
manage to get any output parameter working. Even if I assign SELECT
@mycount=100 (or SET @mycount=100) in the SP the only value set in
mycount is always NULL. I tested various theories (e.g. early exit,
order & naming of parameters etc. but I can not make the SP set the
output parameters - has it anything to do with the execute?). @mycount
also returns NULL if I test it in SQL QA.


That is more likely to have to with how you call the procedure. You
must specify the parameter as OUTPUT. In T-SQL:

EXEC spFindProducts @cnt OUTPUT, @string

I can tell you should do it in ASP, since I don't know how you call the
procedure. Assuming that you use ADO, there is an adDirectionOutput
somewhere.

Then again, if this is your idea about stored procedures, I think you
are better off stop using stored procedures at all and generate all
SQL in the client. This only adds to your complexity, but you win nothing.

I have an article on my web site which discusses various techniques
to implement dynamic searches, that may be useful to you:
http://www.sommarskog.se/dyn-search.html

--
Erland Sommarskog, SQL Server MVP, es****@sommarskog.se

Books Online for SQL Server SP3 at
http://www.microsoft.com/sql/techinf...2000/books.asp
Jul 23 '05 #2
Axel,

Try this...

Return @@rowcount

Stored procedure returns an integer as a return code, use it for
anything you want!!!

Jul 23 '05 #3
Sorry,
I just noticed that you are executing a sql statement.
In that case remember that a brand new environment is created for that
execution and has nothing to do with your currently stored procedure.
Hence the nulls on @@rowcount...
The only way to do this is to send your output to a #table, then do a
select count(*) from #table.
So, do a select into #table and it will work...
Yes, executing sql is very tricky...

Good luck!

Jul 23 '05 #4
> > output parameters - has it anything to do with the execute?). @mycount
also returns NULL if I test it in SQL QA.
That is more likely to have to with how you call the procedure. You
must specify the parameter as OUTPUT. In T-SQL: EXEC spFindProducts @cnt OUTPUT, @string
thanks Erland, now I know that my SP works at least. That already helps
a great deal!
I can tell you should do it in ASP, since I don't know how you call the procedure. Assuming that you use ADO, there is an adDirectionOutputsomewhere.


here is the code I am using on the ASP page. I am building only the
where string in HTML (as I want to keep the number of fields searched
variable without having to change the SP or having lots of parameters
in it), from the results I am quite happy with that solution. If I get
time I will read your article which I am sure has even better ways to
do this.

Attached is the code of the ASP page - at this stage myu theory is I am
missing a step between CmdSP.Execute() and reading the parameter -
maybe a refresh of some sorts? Any idea?

tia Axel

Dim CmdSP

Set CmdSP = Server.CreateObject("ADODB.Command")
'-- Make an ODBC connection to the (local) SQL server,
Dim rs
Dim lRecCount
Set rs = Server.CreateObject("ADODB.Recordset")

CmdSP.ActiveConnection = Application("cnn")
CmdSP.CommandText = "spFindProducts"
CmdSP.CommandType = 4 ' adCmdStoredProc

' first parameter is always the return value!!!!!!!!!
CmdSP.Parameters.Append _
CmdSP.CreateParameter("RETURN_VALUE", adInteger, _
adParamReturnValue, 4)

CmdSP.Parameters.Append _
CmdSP.CreateParameter("@mycount", adInteger, adParamOutput)
CmdSP.Parameters.Append _
CmdSP.CreateParameter("@whereString", adVarChar, _
adParamInput, len(sWhere),
sWhere)
Set rs = CmdSP.Execute( )

lRecCount = CmdSP.Parameters("@mycount").Value

===> lRecCount is always empty!!! why?

Jul 23 '05 #5
Thanks Mario,

Using SQL QA, I found out in the meantime that the rowcount actually
does work. Its the ASP page that is not able to read it for some
reason. Maybe it works once I close the recordset but I need to display
the search results _after_ displaying the number of records, e.g.

we found XX items
1..
2..
3..
etc.

Only difference in QUery Analyzer is that it displays the search
recordset first, then the contents of @mycount. So it might still be a
chicken and egg situation (SQL Server not being able to count the
records without MoveLast and closing recordset ???????)

Jul 23 '05 #6
Axel (re***********@hotmail.com) writes:
I can tell you should do it in ASP, since I don't know how you call the
procedure. Assuming that you use ADO, there is an adDirectionOutput
somewhere.


That was supposed to read "I can't tell". Nevermind, it's an ADO problem,
and ADO I know something about.

I believe that you are using a server-side cursor. At least when I tried
an example I was not able to retrieve the value of the output parameter
with a server-side cursor. Unless you have very compelling reasons, you
should anyway use client-side cursors, which use specify by setting
cnn.CursorLocation to adUseClient. In this case you get the output
parameter.
--
Erland Sommarskog, SQL Server MVP, es****@sommarskog.se

Books Online for SQL Server SP3 at
http://www.microsoft.com/sql/techinf...2000/books.asp
Jul 23 '05 #7
Thanks Erland,

I had tried changing tro a client side cursor but it still produces the
same result

(from my global.asa)
<SCRIPT LANGUAGE=VBScript RUNAT=Server>
Sub Application_OnStart

dim cnnDem

Set cnnDem = Server.CreateObject("ADODB.Connection")

cnnDem.CursorLocation=3 ' adUseClient=3 (Server=2)
Call cnnDem.Open("DSN=TestDB;UID=sa;PWD=;DATABASE=TestD B")

Application("cnn") = cnnDem

End Sub
</SCRIPT>

it doesn't seem to make any difference. How can I force SQL Server to
fully transmit the recordset (I read in another post that the output
params are not marshalled back until all rs are fully transmitted
[according to Bob Barrows on microsoft.public.inetserver.asp.db])? Do I
need to MoveLast or even close the recordset for this? Kind of defeats
the purpose (because I want to iterate after displaying mycount) -
looks like I have to do GetRows and copy to an array if all else fails.

tia
Axel

Jul 23 '05 #8
Axel (re***********@hotmail.com) writes:
I had tried changing tro a client side cursor but it still produces the
same result

(from my global.asa)
<SCRIPT LANGUAGE=VBScript RUNAT=Server>
Sub Application_OnStart

dim cnnDem

Set cnnDem = Server.CreateObject("ADODB.Connection")

cnnDem.CursorLocation=3 ' adUseClient=3 (Server=2)
Call cnnDem.Open("DSN=TestDB;UID=sa;PWD=;DATABASE=TestD B")

Application("cnn") = cnnDem

End Sub
</SCRIPT>


Is adUseClient really 3? Does ASP not support the use of the constants
instead?

If you use client-side cursor, there is another solution which may be
more palatable: use the .RecordCount property on the recordset.
--
Erland Sommarskog, SQL Server MVP, es****@sommarskog.se

Books Online for SQL Server SP3 at
http://www.microsoft.com/sql/techinf...2000/books.asp
Jul 23 '05 #9
>Is adUseClient really 3?
yes, according to Microsoft's ADOVBS_Short.INC
Does ASP not support the use of the constants instead? Yes. Either by including aformentioned file (best practise to clip it
down to not have 15 kbyte waste per page / session) or simply declaring
yourself
Const adUseClient = 3

however, it did not support using them "out of the box" - the Server
simply did not understand them, that's why I originally used numerals
in this code. I dislike unreadable code myself so I am sure to change
it once the prototype phase is over.
If you use client-side cursor, there is another solution which may be

more palatable: use the .RecordCount property on the recordset.

I had tried the RecordCount solution as well (didn't work), but can not
remember if I tried it with the client side cursor. I remember under VB
it was a no brainer to use this property but in ASP you probably have
to MoveLast to fill it with a meaningful value - a bit like in DAO
under Access (shudder). I will do some tests once I am back in the
office (monday).

As the bottom line my prior knowledge of ADODB helped with the syntax
but its actual behavior in ASP / IIS varies...

one learns... hopefully I will have time to read the longish article on
dynamic searches until then - somebody from
microsoft.public.inetserver.asp.db pointed me to the same one.

regards & have a nice weekend
Axel

Jul 23 '05 #10
Axel,

SQL Server send the output parameters and the return value in the last packet
it returns to the client, so in order to acces them, you have to process all
resultsets (or abort pulling the data). See "Parameter Markers" in BOL for
more info.
AMB

--
Message posted via http://www.sqlmonster.com
Jul 23 '05 #11
Axel (re***********@hotmail.com) writes:
I had tried the RecordCount solution as well (didn't work), but can not
remember if I tried it with the client side cursor. I remember under VB
it was a no brainer to use this property but in ASP you probably have
to MoveLast to fill it with a meaningful value - a bit like in DAO
under Access (shudder). I will do some tests once I am back in the
office (monday).


I don't see why ADO would work differently in ASP than i Visual Basic,
but all tests I ever will conduct with ADO will be in Visual Basic, since
ASP is nothing I know.

..RecordCount should work with a static cursor, and as well with a keyset.
Don't remember off-hand which is the default cursor type when you use
server-side cursor, but I would guess for fast forward, and you can of
course not get a direct .RecordCount with these. Client-side cursors
are always static.

--
Erland Sommarskog, SQL Server MVP, es****@sommarskog.se

Books Online for SQL Server SP3 at
http://www.microsoft.com/sql/techinf...2000/books.asp
Jul 23 '05 #12

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

Similar topics

1
by: Sandie Towers | last post by:
We use a number of similar databases and frequently create a new database using a backup restore of another similar database. We try to keep changes between databases in _Additional tables - like...
5
by: vivienne.netherwood | last post by:
I am developing an Access Project front end with a SQL server database. I have written a stored procedure that returns a record set and also a value via an output parameter. The procedure is as...
8
by: Patreek | last post by:
Hi, On the line where I'm assigning RecordCount to be the value of my output parameter, I'm getting the generic "Object reference not set to an instance of an object" error. I've isolated it...
0
by: rockdale | last post by:
Hi, All How to get the output parameter's value when you use the SQLHelper (Microsoft Data Access Block)? When I try to access my ourput parm I got the following error. ...
6
by: c676228 | last post by:
Hi everyone, I wrote a store procedure that fetch one row data in the database based on the parameter value I entered. After I created the store procedure, the store procedure code looks like...
8
by: Alec MacLean | last post by:
Hi, I'm using the DAAB Ent Lib (Jan 2006) for .NET 2.0, with VS 2005 Pro. My project is a Web app project (using the WAP add in). Background: I'm creating a survey system for our company, for...
7
by: ashtek | last post by:
Hi, I have a generic function that executes a stored procedure & returns a data table. Code: === public static DataTable ExecuteStoredProcedure(string strProc,SqlParameter paramArray) {...
1
by: John Bailo | last post by:
This is a my solution to getting an Output parameter from a SqlDataSource. I have seen a few scant articles but none of them take it all the way to a solution. Hopefully this will help some...
4
by: jpatchak | last post by:
OK, not sure if there is a way around this. I have some relatively complicated SQL code in a stored proc. The proc is working fine. When I execute this code in SSMS, it returns a record set and an...
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:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
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...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
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
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
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...

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.