1 alter proc Usp_sqlLs 2 @columnname nvarchar(50) --判断是按哪一列排序的 3 as 4 begin 5 select row_number() over(order by @columnname asc) as rnum,* from books 6 end 7 8 9 declare @cl nvarchar(50) 10 select @cl='PublishDate' 11 exec Usp_sqlLs @cl
有这么一个存储过程,排序的列是我动态输入的,为什么排序没有效果?
但是换成动态SQL语句拼接的话就能实现,求原理......
select row_number() over(order by @columnname asc) as rnum,* from books
等同于
select row_number() over(order by 'xxxxx' asc) as rnum,* from books
@columnname在这里只是一个字符串常量,并不是你的字段名