SQL Server Performance

Select top N records depending on input N

Discussion in 'SQL Server 2008 General Developer Questions' started by Rahul_Dubey, Jul 31, 2009.

  1. Rahul_Dubey New Member

    Hi,I have to write a procedure which should have two input parameters: 'Table_name' and 'No_of_rows' and we need to fetch top 'No_of_rows' records from the 'Table_name'. Can anybody please help me writing such query?Even if you tell me how to fetch top 'No_of_rows' records from any table where 'No_of_rows' is input parameter. it should do.
  2. RickNZ New Member

    You can use an argument for TOP, but it needs to be in parens. Like this:
    declare @nrows int
    select top (@nrows) YourColumn from YourTable
  3. preethi Member

    [quote user="RickNZ"]
    You can use an argument for TOP, but it needs to be in parens. Like this:
    declare @nrows int
    select top (@nrows) YourColumn from YourTable
    [/quote]
    In case you want to pass the tabke name as a parameter you may have to use dynamic sql
  4. FrankKalis Moderator

    Personally I would rather use individual procedures for each table instead of one generic "SELECT *" dynamic SQL procedure. Gives you much more granular control over what is to be returned and doesn't force you to grant permissions one the base tables.
  5. Madhivanan Moderator

    [quote user="Rahul_Dubey"]Hi, I have to write a procedure which should have two input parameters: 'Table_name' and 'No_of_rows' and we need to fetch top 'No_of_rows' records from the 'Table_name'. Can anybody please help me writing such query? Even if you tell me how to fetch top 'No_of_rows' records from any table where 'No_of_rows' is input parameter. it should do.[/quote]
    Why do you need this?
    How many columns do you need?
    Note that you cant design any report with this approch
  6. FrankKalis Moderator

    [quote user="Madhivanan"]
    [quote user="Rahul_Dubey"]Hi, I have to write a procedure which should have two input parameters: 'Table_name' and 'No_of_rows' and we need to fetch top 'No_of_rows' records from the 'Table_name'. Can anybody please help me writing such query? Even if you tell me how to fetch top 'No_of_rows' records from any table where 'No_of_rows' is input parameter. it should do.[/quote]
    Why do you need this?
    How many columns do you need?
    Note that you cant design any report with this approch
    [/quote]
    Is he speaking of reports?
  7. Madhivanan Moderator

    [quote user="FrankKalis"]
    [quote user="Madhivanan"]
    [quote user="Rahul_Dubey"]Hi, I have to write a procedure which should have two input parameters: 'Table_name' and 'No_of_rows' and we need to fetch top 'No_of_rows' records from the 'Table_name'. Can anybody please help me writing such query? Even if you tell me how to fetch top 'No_of_rows' records from any table where 'No_of_rows' is input parameter. it should do.[/quote]
    Why do you need this?
    How many columns do you need?
    Note that you cant design any report with this approch
    [/quote]
    Is he speaking of reports?
    [/quote]
    No. I precautiously told him. I have seen such questions earlier where OPs wanted to build report based on dynamic source

Share This Page