Select Top @variable

I want to do something like this: SELECT TOP @variable.
I cannot use stored procedure for this report, 'cause I do not have access to prod db, I'm just sending rdl for upload. How to make it work on dataset level or through the report layout

I know SQLServer doesn't recognize "Select Top @variable" statement. 
SELECT    top &variable, Ticket.problem AS Issue, COUNT(Solutions.solution_date) AS [Tickets Touched]
FROM         Ticket INNER JOIN
                      Solutions ON Ticket.ticket_number = Solutions.ticket_number
WHERE     GROUP BY Ticket.problem

Or

SELECT     Ticket.problem AS Issue, COUNT(Solutions.solution_date) AS [Tickets Touched]
FROM         Ticket INNER JOIN
                      Solutions ON Ticket.ticket_number = Solutions.ticket_number
WHERE     GROUP BY Ticket.problem
HAVING      (COUNT(Solutions.solution_date) > CAST(@num AS int)) doesn't work Sad

Please, help!Idea




Answer this question

Select Top @variable

  • daniel 01

    You can use an expression based query, i.e.

    ="SELECT    TOP (" & Parameters!Variable.Value & ") Ticket.problem AS Issue, COUNT(Solutions.solution_date) AS [Tickets Touched]
    FROM Ticket INNER JOIN Solutions ON Ticket.ticket_number = Solutions.ticket_number WHERE GROUP BY Ticket.problem"


  • Joseph Fluckiger

    Hi

    you can do without any Dynamic Query

    ---------

    declare @NoOfRecords
    Set @NoOfRecords = 10

    set Rowcount @NoOfRecords

    SELECT * FROM TABLENAME

    SET Rowcount 0


  • thompson16

    Is there a way to do this from within Visual Studio 2005

    For example, if I open the Data tab for a report that currently returns 7000+ rows, I can enter "TOP 100" directly into the SELECT statement and all works as expected. But when I try either of the methods mentioned above, I get syntax errors.


  • MichMash

    If you are using SQL Server 2005 the following should do the needful

    declare @limit int
    Set @limit=5
    select top(@limit) * from TableName


  • Chibuzo

    you  can use also "set rowcount " command  (sql)
    ex :

    --setup  the max number of rows

    Set rowcount @top
    Select ..... from  XXX
    -- reinitialize
    set rowcount 0

  • Select Top @variable