CREATE PROCEDURE Pages
@startIndex int='',                   --分页开始标记
@endIndex int='',                    --分页结束标记
@SQL varchar(8000)=''
AS
begin
        set nocount on
       declare @indextable table(id int identity(1,1),nid int)
       set rowcount @endIndex
       insert into @indextable(nid) select OrderID from [order details] order by OrderID desc
      SELECT * FROM (sp_executesql @SQL) c  inner join @indextable t on 
       c.OrderID=t.nid
       where t.id between @startIndex and @endIndex 
       order by t.id desc
end这段代码老是提示 @SQL附近有语法错误,请各位帮忙看一下是怎么回事,谢谢!

解决方案 »

  1.   

    CREATE PROCEDURE Pages
    @startIndex int null, --分页开始标记
    @endIndex int  null, --分页结束标记
    @SQL varchar(8000) null
    AS
    begin
      set nocount on
      declare @indextable table(id int identity(1,1),nid int)
      set rowcount @endIndex
      insert into @indextable(nid) select OrderID from [order details] order by OrderID desc
      SELECT * FROM (sp_executesql @SQL) c inner join @indextable t on  
      c.OrderID=t.nid
      where t.id between @startIndex and @endIndex  
      order by t.id desc
    end
      

  2.   

    CREATE PROCEDURE Pages
    @startIndex int=0, --分页开始标记
    @endIndex int=0, --分页结束标记
    -- 或者用成对应类型的默认值看看。
      

  3.   

    ++
    像INT的默认值应该设置为0
      

  4.   

    SELECT * FROM (sp_executesql @SQL) c inner join @indextable t on  
      

  5.   

    CREATE PROCEDURE Pages
    @startIndex int , --分页开始标记
    @endIndex int , --分页结束标记
    @SQL varchar(8000) 
    AS
    begin
      set nocount on
      declare @indextable table(id int identity(1,1),nid int)
      set rowcount @endIndex
      insert into @indextable(nid) select OrderID from [order details] order by OrderID desc
      select * from ((sp_executesql @SQL)k) c inner join @indextable t on  
      c.OrderID=t.nid
      where t.id between @startIndex and @endIndex  
      order by t.id desc
    end我改成这样后,还是提示 错误170:第11行:'@SQL'附近有语法错误SQL 2000
      

  6.   

    外部语句 @SQL 的查询结构要知道,在存储内部再建一个表变量,然后用下面的方法:
    CREATE PROCEDURE Pages
    @startIndex int='', --分页开始标记
    @endIndex int='', --分页结束标记
    @SQL varchar(8000)=''
    AS
    begin
      set nocount on
      declare @indextable table(id int identity(1,1),nid int)
      set rowcount @endIndex
      insert into @indextable(nid) select OrderID from [order details] order by OrderID descdeclare @c table(....)
    insert into @c exec (@sql)
      SELECT * FROM @c c inner join @indextable t on  
      c.OrderID=t.nid
      where t.id between @startIndex and @endIndex  
      order by t.id desc
    end
      

  7.   

    对了,存储过程的两个参数如为 int 型的话,默认值应为 0.
      

  8.   

    谢谢大家!特别是qianjin036a
    问题已解决,代码贴出来:
    CREATE PROCEDURE Pages
    @startIndex int='', --分页开始标记
    @endIndex int='', --分页结束标记
    @SQL varchar(8000)=''
    AS
    begin
      set nocount on
      declare @indextable table(id int identity(1,1),nid int)
      set rowcount @endIndex
      insert into @indextable(nid) select OrderID from [order details] order by OrderID asccreate table tmp(
    [OrderID] [int] NOT NULL ,
    [ProductID] [int] NOT NULL ,
    [UnitPrice] [money] NOT NULL ,
    [Quantity] [smallint] NOT NULL ,
    [Discount] [real] NOT NULL ,
    [LastEditDate] [datetime] NULL ,
    [CreationDate] [datetime] NULL 
    )
    insert into tmp exec (@SQL)  SELECT * FROM tmp c inner join @indextable t on  
      c.OrderID=t.nid
      where t.id between @startIndex and @endIndex  
      order by t.id descdrop table tmp
    end
    GO