我有一个带参数的程序。我想基于该参数循环遍历查询。
例如:
参数传递:salary
光标应该是(从emp中选择工资)并循环遍历它。
我该怎么做?
答案 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)
试试吧。像这样