想根据不同的传入参数,对不同的表进行查询(以下的代码总是提示有误,请问具体应该怎么实现,可有更好的方法)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
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
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 ...
As
if @flag=1
select * from [tab1] where clsId=@clsId
else if @flag= 2
select * from [tab2] where clsId=@clsId
GO这样就行了
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行.....*/
(@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行.....*/