写了一个存储过程,不知道哪错了!请高手指点一下吧,谢谢了!
CREATE PROCEDURE Role_GetMembers
@roleid int
asdeclare @str nvarchar(500)
declare user_cursor cursor for 
select userid from userinrole where roleid = @roleidopen user_cursorfetch next from user_cursor into @str
while @@fetch_status = 0 
begin
set @str = @str + ','
end
close user_cursor
deallocate user_cursor
select * from users where [id] in (@str)go其中,userid字段为INT类型。

解决方案 »

  1.   

    select * from users where [id] in (@str)-- 改为:
    select * from users where charindex(',' + [id] + ',', ',' + @str + ',')>0
      

  2.   

    另外, while之前要加一句:
    set @str =''
      

  3.   

    另外, 其实这样就可以了, 不要写得那么复杂CREATE PROCEDURE Role_GetMembers
    @roleid int
    as
    select * from users where [id] in(
    select userid from userinrole where roleid = @roleid)
      

  4.   

    CREATE PROCEDURE Role_GetMembers
    @roleid int
    asdeclare @str nvarchar(500)
    set @str=',-1'
    declare @userid int
    declare user_cursor cursor for 
    select userid from userinrole where roleid = @roleidopen user_cursorfetch next from user_cursor into @userid
    while @@fetch_status = 0 
    begin
    set @str = @str + ',' + @userid
    fetch next from user_cursor into @userid
    end
    close user_cursor
    deallocate user_cursorset @str=stuff(@str, 1, 1, '')
    select * from users where [id] in (@str)go