我想使用以下动态SQL声明光标,但是当我尝试在存储的proc中与之编译时,以下SQL失败了。有一个更好的方法吗?谢谢。
DECLARE selrec CURSOR FOR
SELECT DISTINCT countrycode FROM @InputTableName;
错误msg: -
Must declare the table variable "@InputTableName".
ps: - 我声明了变量名称,并且在构建光标之前也设置了值。
作为一个例子:
declare
var1 varchar2(100);
table1 varchar2(30);
x integer;
begin
if x = 1
then
table1 := 'tablename1';
else
table1 := 'tablename2'
end if;
var1 := 'select * from '||table1;
execute immediate var1;
end;
这对我来说很好(在这两个表的DB中):
declare @i1 int=5
declare @a1 varchar(1000)
set @a1='select top 5 * from '
if (@i1 > 4)
begin
set @a1 = @a1 + ' [dbo].[dts_source]'
end
else
begin
set @a1 = @a1 + ' [dbo].[dts_references]'
end
select @a1
exec (@a1)
您在(
)下运行什么SQL机器立即执行
不是SQL Server语法)?