| 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 OUTPUTWITH ENCRYPTIONAS 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 TRANSACTIONGO Edited by - frogrammer82 on 06/27/2002 11:05:40Edited 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 correctlyie.Declare @RecCount intEXEC GetCheapestCompanies @LocID,@CatID,@BrandID,@FeeLimit,@RecCount OUTPUT Select @RecCountP.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 |
 |
|
|
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 INTEXEC spGetCheapestCompanies @LocID = 7, @CatID = 5, @BrandID = 4, @RecCount = @RecCount OUTPUTSELECT @RecCount--PRINT CAST(@RecCount AS varchar(10)) +" recs are returned." Edited by - frogrammer82 on 06/27/2002 11:25:22Edited by - frogrammer82 on 06/27/2002 11:26:37 |
 |
|
|
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 thenjust 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 MIAPwww.danielsmall.com IT Factoring |
 |
|
|
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. |
 |
|
|
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 = @@ROWCOUNTBy 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 ondeclare @RecCount intselect @RecCount = -1create table #tmp (value int not null)insert #tmp values (1)insert #tmp values (2)insert #tmp values (3)insert #tmp values (4) select * from #tmpIF ((@@ROWCOUNT <= 0) OR (@@ERROR <> 0)) SET @RecCount = 0ELSE SET @RecCount = @@ROWCOUNTselect @RecCount as RecCountdeclare @RecCount2 int , @err intselect @RecCount2 = -1select * from #tmp select @RecCount2 = @@ROWCOUNT, @err = @@ERRORIF ((@RecCount2 <= 0) OR (@err <> 0)) SET @RecCount2 = 0--ELSE --SET @RecCount = @@ROWCOUNTselect @RecCount2 as RecCount2 drop table #tmp-- here is the outputvalue ----------- 1234RecCount ----------- 0value ----------- 1234RecCount2 ----------- 4 |
 |
|
|
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.. |
 |
|
|
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!.. |
 |
|
|
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 = 4then the AND clause will look likeAND C.locID = '4'otherwiseAND 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 MIAPwww.danielsmall.com IT Factoring |
 |
|
|
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:) |
 |
|
|
|