WITH AS sql 查询问题 最近在写存储过程的时候感觉with as 非常好用写起来都比较方便,不过我遇到一个问题。比如传递了参数@startindex,@endindex,@orderby
wth tt as
(
select * from u
)
select * from c where id between @startindex and @endindex order by @orderby 问题就是这个参数@orderby怎么处理好呢》
我最终解决的办法就是声明一个临时表插入 insert into #t2 select * from c 然后exec('select * from #t2 order by '+@orderby+' ')
declare @startindex int set @startindex=16
declare @endindex int set @endindex=25
declare @orderby varchar(20) set @orderby='number'
declare @sql varchar(max)
set @sql='
;with maco as
(
select row_number() over (order by '+@orderby+') as rowid,*
from master..spt_values
)
select * from maco
where rowid between '+ltrim(@startindex)+' and '+ltrim(@endindex)