想根据不同的传入参数,对不同的表进行查询(以下的代码总是提示有误,请问具体应该怎么实现,可有更好的方法)create procedure (@flag tinyint,@clsId int)
As
declare @cur cursor;
set @cur = cursor for
case @flag
 when 1 then
   select * from [tab1] where clsId=@clsId
 when 2 then
   select * from [tab2] where clsId=@clsId
end
open @cur

解决方案 »

  1.   

    動態cursor用exec('declare c1 cursor for select xx from '+@table)
      

  2.   

    create procedure alterImgSta(@flag tinyint,@clsId int)
    As
    declare @cur cursor;
    set @cur = cursor for
    case @flag
     when 1 then
      select * from [tab1] where clsId=@clsId
     when 2 then
      select * from [tab2] where clsId=@clsId
    end
    open @cur
    fetch ...
      

  3.   

    create procedure sp_test (@flag tinyint,@clsId int)
    As
    if @flag=1
      select * from [tab1] where clsId=@clsId
    else if @flag= 2 
      select * from [tab2] where clsId=@clsId
    GO这样就行了
      

  4.   


    create procedure aaa
    (@flag tinyint,@clsId int)
    As
    declare @sql varchar(8000)
    set @sql='declare cur cursor for '
    if (@flag=1)
      set @sql=@sql+'select 字段 from [tab1] where clsId='''+@clsId+''
    else if (@flag=2)
       set @sql=@sql+'select 字段 from [tab1] where clsId='''+@clsId+''
    exec (@sql)
    open cur
    /*....以下省略n行.....*/
      

  5.   

    不好意思,刚才写错了下,没看清楚你的@clsID是int,要进行格式转换create procedure aaa
    (@flag tinyint,@clsId int)
    As
    declare @sql varchar(8000)
    set @sql='declare cur cursor for '
    if (@flag=1)
      set @sql=@sql+'select 字段 from [tab1] where clsId='+cast(@clsId as varchar(10))+''
    else if (@flag=2)
       set @sql=@sql+'select 字段 from [tab2] where clsId='+cast(@clsId as varchar(10))+''
    exec (@sql)
    open cur
    /*....以下省略n行.....*/