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.
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 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
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.
[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 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 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