Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2000 Forums
 SQL Server Development (2000)
 I think, this is a bug of sql server 7

Author  Topic 

frogrammer82
Starting Member

7 Posts

Posted - 2002-06-27 : 10:00:11
In the stored procedure below, i want to return recordset and also recordcount number of the selected records by the query.. sql server 7 returns recordset successfully but it doesnt return RecCount OUTPUT variable, why? i dont know, thanx all..


CREATE PROC GetCheapestCompanies
@LocID int = 0,
@CatID int = 0,
@BrandID int = 0,
@FeeLimit int = 0,
@RecCount int = 0 OUTPUT
WITH ENCRYPTION
AS

SET NOCOUNT ON

IF ((@iLocationID+@iCategoryID+@iBrandID) IS NULL
OR (@iLocationID+@iCategoryID+@iBrandID) = 0)
BEGIN
RETURN 0
END

-- BEGIN TRANSACTION

DECLARE @sSQL nvarchar(500)

SET @sSQL = '
SELECT CheapestCompanies.CompID AS [CompID]
FROM
(
SELECT p.CompID, c.LocID,
MIN(p.ProductFee) AS [ProductFee]
FROM tbProducts p, tbCompanies c
WHERE
c.CompID = p.CompID '

IF (@LocID <> 0)
SET @sSQL = @sSQL + 'AND c.LocID = '+ CAST(@LocID AS nvarchar(10))

IF (@CatID <> 0)
SET @sSQL = @sSQL + 'AND p.iProductCategoryID = '+ CAST(@CatID AS nvarchar(10))

IF (@BrandID <> 0)
SET @sSQL = @sSQL + 'AND p.BrandID = '+ CAST(@BrandID AS nvarchar(10))

SET @sSQL = @sSQL +'GROUP BY p.CompID, c.LocID '

IF (@FeeLimit <> 0)
SET @sSQL = @sSQL + 'HAVING MIN(p.ProductFee) > '+ CAST(@FeeLimit AS nvarchar(10))

SET @sSQL = @sSQL +'
) AS CheapestCompanies'

-- ## execute the query
EXEC sp_executesql @sSQL

IF ((@@ROWCOUNT <= 0) OR (@@ERROR <> 0))
SET @RecCount = 0
ELSE
SET @RecCount = @@ROWCOUNT

-- COMMIT TRANSACTION
GO



Edited by - frogrammer82 on 06/27/2002 11:05:40

Edited by - frogrammer82 on 06/27/2002 11:07:29

Kevin Snow
Posting Yak Master

149 Posts

Posted - 2002-06-27 : 11:10:32
quote:

.. sql server 7 returns recordset successfully but it doesnt return RecCount OUTPUT variable, why? i dont know, thanx all..




Make sure you are calling it correctly
ie.
Declare @RecCount int
EXEC GetCheapestCompanies @LocID,@CatID,@BrandID,@FeeLimit,@RecCount
OUTPUT
Select @RecCount

P.S I verified it on SQL7 running sp4.

The output designation is required when calling the stored procedure ( Even though it is declared on the receiving end). Assuming you are calling it correctly, you should be getting at least some value returned from the SProc.


Edited by - kevin snow on 06/27/2002 11:19:27
Go to Top of Page

frogrammer82
Starting Member

7 Posts

Posted - 2002-06-27 : 11:24:12
Sorry, i didnt send the execution code.
quote:

The output designation is required when calling the stored procedure ( Even though it is declared on the receiving end). Assuming you are calling it correctly, you should be getting at least some value returned from the SProc.


I was already made it so, but it wasnt work also..

The code at below returning 2 rows when it runs. and again it doesnt return @RecCount OUTPUT variable to me...

My execution code is:

DECLARE @RecCount INT

EXEC spGetCheapestCompanies
@LocID = 7,
@CatID = 5,
@BrandID = 4,
@RecCount = @RecCount OUTPUT

SELECT @RecCount
--PRINT CAST(@RecCount AS varchar(10)) +" recs are returned."




Edited by - frogrammer82 on 06/27/2002 11:25:22

Edited by - frogrammer82 on 06/27/2002 11:26:37
Go to Top of Page

MakeYourDaddyProud

184 Posts

Posted - 2002-06-27 : 11:25:11
A bit off topic but do you really need to use EXEC?

i.e.
...
WHERE
c.CompID = p.CompID
and c.locID = (case when @locID<>0 then CAST(@LocID AS nvarchar(10) else c.LocID)
and c.CatID = (case when @CatID<>0 then CAST(@CatID AS nvarchar(10) else c.CatID)
and c.BrandID = (case when @BrandID<>0 then CAST(@BrandID AS nvarchar(10) else c.BrandID)
GROUP BY p.CompID, c.LocID
HAVING MIN(p.ProductFee) > + CAST(@FeeLimit AS nvarchar(10)

The use of dynamically building the SQL is replaced by equating col = col which is always true but using a case to split out the difference.

If all your p.ProductFees is never < 0 then

just add

HAVING MIN(p.ProductFee) > + CAST(@FeeLimit AS nvarchar(10)

If @feelimit is zero, it is equivalent of not having the HAVING at all.


Daniel Small MIAP
www.danielsmall.com IT Factoring
Go to Top of Page

Kevin Snow
Posting Yak Master

149 Posts

Posted - 2002-06-27 : 11:30:48
quote:

@RecCount = @RecCount OUTPUT



Why the equal sign? Did you try using just "@reccount output"?
It's set to a default value when passes, and set again based on the @@rowcount.

I changed the sp_executeSQL string to pull records out of Pubs.authors and I'm getting the records and the counts.

Go to Top of Page

MuffinMan
Posting Yak Master

107 Posts

Posted - 2002-06-27 : 11:48:32
If the RecCount parameter "doesn't return" then you're probably calling it incorrectly.

However, I guessing that RecCount is always returning as 0 (zero). This is due to the logic in your code. The problem is this section of code:


IF ((@@ROWCOUNT <= 0) OR (@@ERROR <> 0))
SET @RecCount = 0
ELSE
SET @RecCount = @@ROWCOUNT



By checking the values of @@ROWCOUNT and @@ERROR, you are "setting" @@ROWCOUNT to 0 because the statement "IF ((@@ROWCOUNT <= 0) OR (@@ERROR <> 0))" returns zero records.

This code shows you how to handle this issue:


set nocount on

declare @RecCount int
select @RecCount = -1

create table #tmp (value int not null)
insert #tmp values (1)
insert #tmp values (2)
insert #tmp values (3)
insert #tmp values (4)


select * from #tmp

IF ((@@ROWCOUNT <= 0) OR (@@ERROR <> 0))
SET @RecCount = 0
ELSE
SET @RecCount = @@ROWCOUNT

select @RecCount as RecCount


declare @RecCount2 int , @err int
select @RecCount2 = -1

select * from #tmp

select @RecCount2 = @@ROWCOUNT, @err = @@ERROR


IF ((@RecCount2 <= 0) OR (@err <> 0))
SET @RecCount2 = 0
--ELSE
--SET @RecCount = @@ROWCOUNT


select @RecCount2 as RecCount2


drop table #tmp

-- here is the output
value
-----------
1
2
3
4

RecCount
-----------
0

value
-----------
1
2
3
4

RecCount2
-----------
4


Go to Top of Page

frogrammer82
Starting Member

7 Posts

Posted - 2002-06-27 : 11:56:16
quote:
and c.locID = (case when @locID<>0 then CAST(@LocID AS nvarchar(10) else c.LocID)


thanx for the cool tips but what do you meant on the upper sentence?

When @LocID is not null, write @LocID as nvarchar(10), else, write it as c.LocID, but why?

The job of the my previous statement was 'when the user of the stored procedure was give value to the locid make it a filter of the sql query, otherwise, dont make it a filter'.. is this making same job? Because i've write those codes and executed, what it gave me an error..

Go to Top of Page

frogrammer82
Starting Member

7 Posts

Posted - 2002-06-27 : 12:15:43
Thank you thank you thank you MuffinMan!
You've solved my problem...

Thank you so much..
Take care yourself!..

Go to Top of Page

MakeYourDaddyProud

184 Posts

Posted - 2002-06-27 : 13:02:24
There was a ')' missing off the end of each varchar(10) so syntax error. The idea behind it allows a dynamic scalar value to match i.e.

if @locId = 4

then the AND clause will look like

AND C.locID = '4'

otherwise

AND C.LocID = C.LocID -- Think about it for a moment, because this line is equivalent to not having it at all hence the reason to avoid the EXEC call.

Daniel Small MIAP
www.danielsmall.com IT Factoring
Go to Top of Page

frogrammer82
Starting Member

7 Posts

Posted - 2002-06-27 : 13:53:03
thank you danny2sweet, you've made me more performatic !
i love this site:)

Go to Top of Page
   

- Advertisement -