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.
"Mary Bray [MVP]" <reply@tonewsgroup.com.NOSPAMPLEASE> wrote in message
news:%23v4dLnYMFHA.1176@TK2MSFTNGP12.phx.gbl...
> 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
>
> "Andy Jones" <ajones@rheemac.com> wrote in message
> news:eHYJKgWMFHA.1528@TK2MSFTNGP09.phx.gbl...
>> 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
>> "Mary Bray [MVP]" <reply@tonewsgroup.com.NOSPAMPLEASE> wrote in message
>> news:e4QU02PMFHA.3492@TK2MSFTNGP10.phx.gbl...
>>> 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
>>>
>>> "Mike Epprecht (SQL MVP)" <mike@epprecht.net> wrote in message
>>> news:%23QVCUsMMFHA.2940@TK2MSFTNGP15.phx.gbl...
>>>> 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/ >>>>
>>>> "Andy Jones" <ajones@rheemac.com> wrote in message
>>>> news:OgZFGlMMFHA.1476@TK2MSFTNGP09.phx.gbl...
>>>>> 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
>>>>>
>>>>>
>>>>
>>>>
>>>
>>>
>>
>>
>
>