這個想法是有一個通用的存儲過程來從所有表中獲取數據。此查詢給出錯誤不正確的語法 - T-Sql
不正確的語法new @OrderByClause。
我哪裏錯了?
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
編輯:
這是工作雖然....
select @code = 'with temp as (select row_number() over (order by '+
@colname+' '[email protected]+') as row, * from '[email protected]+')
select * from temp where row between '[email protected]+' and '[email protected]
execute sp_executesql @code
你不能參數化的SQL的任意部分,你是試。這裏使用動態SQL的要點是首先爲無法參數化的部分通過串聯創建所需的字符串。閱讀SQL注入和'QUOTENAME'函數[here](http://www.sommarskog.se/dynamic_sql.html) –