CREATE PROCEDURE Book_PageGetBooks1
(
  @ClassIDs varchar(500)
)
as
set nocount on
begin   
      declare @indextable table(id int identity(1,1),nid int)      --查询
      select O.BookID 
      from Book_Books O,
           @indextable t 
      where O.BookID=t.nid 
            and charindex(','+convert(varchar,BookID)+',',','+@ClassIDS +',')>0  
      order by t.id
end
set nocount off
GO

解决方案 »

  1.   

    或者CREATE PROCEDURE Book_PageGetBooks1
    (
      @ClassIDs varchar(500)
    )
    as
    set nocount on
    begin   
          create table #(id int identity(1,1),nid int)
          insert into # select Bookid from Book_Books        --查询
          exec(' select O.BookID'+ 
               ' from Book_Books O,'+
               ' # t '+
               ' where O.BookID=t.nid'+ 
               ' and BookID in ('+@ClassIDS +')'+  
               ' order by t.id'
              )       drop table #
    end
    set nocount off
    GO
      

  2.   

    to: vivianfdlpw
    你的charindex(','+convert(varchar,BookID)+',',','+@ClassIDS +',')>0  
    得不到任何结果
    上面的那个函数可以代替BookID in ('+@ClassIDs+')吗
      

  3.   

    ","是ID之间的分隔符,你要用你的@ClassIDS得分隔符替换
      

  4.   

    EXEC Book_PageGetBooks1 @ClassIDS="2,62,26,11,9,110,83,87,113"
    我是这样执行存储过程的,应该算是用分隔符了吧
    我刚刚也查询了一下charindex的用法,感觉我会被一些逗号搞晕掉
    sql里面的符号感觉特别麻烦
      

  5.   

    比如你有一个bookid为2,那么当@ClassIDS为2的时候可以得到一条记录。但是当@ClassIDS为12,22,32....等包括数字2的值的时候也可以得到纪录。所以两边要加分隔符来区分
      

  6.   

    to xcz1943(小钊)
    vivianfdlpw()的第一个方法的@indextable t表为空!当然没有结果!
      

  7.   

    第一个方法写的时候意外删掉了一行,原来的一句:
    insert into @indextable(nid) select Bookid from Book_Books  
    丢失了