I have an MSSQL stored procedure as shown below:
ALTER procedure [dbo].[GetDataFromTable]
(
@rowval varchar(50),
@tablename varchar(50),
@oby varchar(50)
)
as
begin
EXEC('Select top (' + @rowval + ') * from '+@tablename+ 'ORDER BY '+@oby+' DESC')
end
When executed, it gives the following error: Msg 156, level 15, state 1, line 1 Invalid syntax next to the keyword "BY". I also tried following, still the same error:
ALTER procedure [dbo].[GetDataFromTable]
(
@rowval varchar(50),
@tablename varchar(50),
@oby varchar(50)
)
as
begin
EXEC('Select top (' + @rowval + ') * from '+@tablename+ 'ORDER BY sno DESC')
end
Note: @rowval represents the number of rows to be selected, @tablename represents the table name, @oby represents the column based on which ordering should be performed. Note. I am using ASP.Net with C # on the interface to run this procedure and use MSSQL 2008 R2 Express Edition on the backend
source
share