The idea is to have a generic stored procedure to get data from all tables. This query gives an error
Incorrect Syntax new @OrderByClause.
Where am I going wrong?
declare @TableName nvarchar(50), @ColName nvarchar(50),
@OrderByClause nvarchar(50), @Code nvarchar(max),
@StartIndex nvarchar(50), @EndIndex nvarchar(50)
set @TableName = 'tblCountry'
set @ColName = 'countryname'
set @OrderByClause = 'desc'
set @StartIndex = '2'
set @EndIndex = '10'
select @Code = 'With temp as (select row_number()
over (order by @ColName @OrderByClause) as row, * from @TableName)
select * from temp where row between @StartIndex and @EndIndex'
set @param = '@TableName nvarchar(50), @ColName nvarchar(50),
@OrderByClause nvarchar(50), @StartIndex nvarchar(50),
@EndIndex nvarchar(50)'
execute sp_executesql @Code, @param @colname, @OrderByClause, @TableName,
@StartIndex, @EndIndex
EDIT:
This is working though....
select @code = 'with temp as (select row_number() over (order by '+
@colname+' '+@OrderByClause+') as row, * from '+@tablename+')
select * from temp where row between '+@startIndex+' and '+@EndIndex
execute sp_executesql @code
QUOTENAMEfunction here