[英]Dynamic loop in PL/SQL
I am currently looping through values in PL/SQL with the following: 我目前正在使用以下内容遍历PL / SQL中的值:
for c in (select * from example_table where name is not null) loop
-- logic
end loop;
I would like to replace the SQL statement with a dynamic one, for example: 我想用动态语句替换SQL语句,例如:
l_sql := 'select * from example_table where || l_col || is not null';
for c in (l_sql) loop
-- logic
end loop;
Is this possible? 这可能吗?
Best regards 最好的祝福
That's not possible with an implicit cursor loop ( select inside for loop
). 这对于隐式游标循环是不可能的( for loop
选择内部)。 You may use the conventional OPEN .. FETCH .. LOOP
through a REFCURSOR
with a record variable of tablename%ROWTYPE
您可以通过带有tablename%ROWTYPE
的记录变量的REFCURSOR
使用常规的OPEN .. FETCH .. LOOP
DECLARE
t_rec example_table%ROWTYPE;
l_sql VARCHAR2(1000);
v_cur SYS_REFCURSOR;
l_col varchar2(32) := 'MY_COLUMN';
BEGIN
l_sql := 'select * from example_table where '|| l_col || ' is not null';
OPEN v_cur FOR l_sql;
LOOP
FETCH v_cur INTO t_rec; --fetch a row
EXIT WHEN v_cur%NOTFOUND;
-- your logic using t_rec columns.
END LOOP;
CLOSE v_cur;
END;
/
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.