来自procedure参数的Dynamic Cursor

时间:2016-11-03 13:27:24

标签: sql oracle dynamic plsql cursor

我有一个带参数的程序。我想基于该参数循环遍历查询。

例如:

参数传递:salary

光标应该是(从emp中选择工资)并循环遍历它。

我该怎么做?

1 个答案:

答案 0 :(得分:0)

DECLARE @ARGUMENT VARCHAR(50) = 'salary';
DECLARE @CURSOR_BODY VARCHAR(MAX) = '
declare @variable varchar(max)
declare cur cursor for 
select '+ @argument +  ' from emp
OPEN cur;
FETCH NEXT FROM cur INTO @variable
WHILE @@FETCH_STATUS=0
    BEGIN
    select @variable
    FETCH NEXT FROM cur INTO @variable;
    END
CLOSE cur   
DEALLOCATE cur'

print @cursor_body

EXEC (@CURSOR_BODY)

试试吧。像这样