Groups | Blog | Home
all groups > sql server reporting services > march 2005 >

sql server reporting services : TOP N Error in SQL


Andy Jones
3/24/2005 5:29:04 PM
I am trying to select the top n of a result where the date is a parameter
and the top n needs to be a parameter. I keep getting an ado error on my sql
query. Can someone tell me what I am doing wrong? I would appreciate it.
Thanks in advance.


if @Topn = ' '
BEGIN
SELECT Defects.Defect1, Units.CreateDate, COUNT(*) AS Expr1
FROM Defects INNER JOIN
UnitDefects ON Defects.DefectID = UnitDefects.DefectID
INNER JOIN
Units ON UnitDefects.UnitID = Units.UnitID
GROUP BY Units.CreateDate, Defects.Defect1
HAVING (Units.CreateDate BETWEEN @StartDate AND @EndDate) AND (NOT
(Defects.Defect1 = 'freon charge')) AND (NOT (Defects.Defect1 = 'spc'))
ORDER BY Expr1 DESC

ELSE

BEGIN

SELECT TOP @Topn Defects.Defect1, Units.CreateDate, COUNT(*) AS Expr1
FROM Defects INNER JOIN
UnitDefects ON Defects.DefectID = UnitDefects.DefectID
INNER JOIN
Units ON UnitDefects.UnitID = Units.UnitID
GROUP BY Units.CreateDate, Defects.Defect1
HAVING (Units.CreateDate BETWEEN @StartDate AND @EndDate) AND (NOT
(Defects.Defect1 = 'freon charge')) AND (NOT (Defects.Defect1 = 'spc'))
ORDER BY Expr1 DESC

END

Mike Epprecht (SQL MVP)
3/25/2005 12:41:58 AM
Hi

TOP does not support variables.

SELECT TOP 10 works, SELECT TOP @Topn does not.

You need to use dynamic SQL if you want to do it in the SP.

Regards
--------------------------------
Mike Epprecht, Microsoft SQL Server MVP
Zurich, Switzerland

IM: mike@epprecht.net

MVP Program: http://www.microsoft.com/mvp

Blog: http://www.msmvps.com/epprecht/

[quoted text, click to view]

Andy Jones
3/25/2005 11:47:15 AM
Thanks for the tip. However, I am new to SQL and I do not know what you mean
by dynamic sql?
[quoted text, click to view]

Andy Jones
3/25/2005 12:25:36 PM
thanks for the help. I guess this will not work for. I haven't been able to
get past the query analyzer it keeps giving me errors. Thanks again
[quoted text, click to view]

Andy Jones
3/25/2005 12:26:24 PM
Makes sense now. I guess I jsut need to figure out how I am coding it wrong
since the query keeps giving me errors. Thanks.
[quoted text, click to view]

Mary Bray [MVP]
3/25/2005 4:44:30 PM
You can however set the rowcount using a variable ie:
if @Topn <> ' '
set @@rowcount=@Topn

etc

--

Mary Bray [SQL Server MVP]
Please reply only to newsgroups

[quoted text, click to view]

Mike Epprecht (SQL MVP)
3/25/2005 6:55:57 PM
Hi

http://www.sommarskog.se/dynamic_sql.html

Regards
--------------------------------
Mike Epprecht, Microsoft SQL Server MVP
Zurich, Switzerland

IM: mike@epprecht.net

MVP Program: http://www.microsoft.com/mvp

Blog: http://www.msmvps.com/epprecht/

[quoted text, click to view]

Mary Bray [MVP]
3/26/2005 9:27:18 AM
You need to make the @Topn parameter an integer and allow null values, then:

if @Topn is not null
set @@rowcount=@Topn

SELECT Defects.Defect1, Units.CreateDate, COUNT(*) AS Expr1
FROM Defects INNER JOIN
UnitDefects ON Defects.DefectID = UnitDefects.DefectID
INNER JOIN
Units ON UnitDefects.UnitID = Units.UnitID
GROUP BY Units.CreateDate, Defects.Defect1
HAVING (Units.CreateDate BETWEEN @StartDate AND @EndDate) AND (NOT
(Defects.Defect1 = 'freon charge')) AND (NOT (Defects.Defect1 = 'spc'))
ORDER BY Expr1 DESC


--

Mary Bray [SQL Server MVP]
Please reply only to newsgroups

[quoted text, click to view]

Andy Jones
3/26/2005 10:39:41 AM
Mary,

When using the query builder in reporting services I get an ado error when I
write the query like below. Do you have any other suggestions for me? I
think I am getting desperate. Thanks for all your help though.
[quoted text, click to view]

AddThis Social Bookmark Button