ALTER PROCEDURE [dbo].[zufanglistpager]
(@startIndex int,@endIndex int,@sqlstr nvarchar(500))
AS
declare @indextable table(id int identity(1,1),nid int)
set rowcount @endIndex
insert into @indextable(nid) select Rentalid from Rental order by @sqlstr
select * from Rental inner join @indextable t on Rentalid=t.nid where t.id between @startIndex and @endIndex order by t.id
set nocount off
提示出错了,由 ORDER BY 编号 1 标识的 SELECT 项包含一个变量,作为标识列位置的表达式的一部分。按照引用列名的表达式排序时,只允许使用变量。
不知道order by 后面的怎么写?请指教

解决方案 »

  1.   

    2005的话,可以这样INSERT @indextable(nid) EXEC('SELECT rentalid FROM Rental ORDER BY ' + @sqlstr);
      

  2.   

    ALTER PROCEDURE [dbo].[zufanglistpager]
    (
    @startIndex int,
    @endIndex int,
    @sqlstr nvarchar(500))
    AS
    declare @indextable table(id int identity(1,1),nid int)
    set rowcount @endIndex
    declare @s varchar(2000)
    set @s='insert into @indextable(nid) select Rentalid from Rental order by '+@sqlstr+''
    sp_executesq(@s)
    select * from Rental inner join @indextable t on Rentalid=t.nid where t.id between @startIndex and @endIndex order by t.id
    set nocount off