根据传递到存储过程的参数在两个游标之间切换
我的过程中有两个游标,它们仅在它们连接到的表名称上有所不同。 使用的游标由传递到过程中的参数确定,
if (param = 'A') then
DECLARE CURSOR myCursor IS
SELECT x,y,z
FROM table1 a, table2 b
BEGIN
FOR aRecord in myCursor
LOOP
proc2(aRecord.x, aRecord.y, aRecord.z);
END LOOP;
COMMIT;
END;
elsif (param = 'B') then
DECLARE CURSOR myCursor IS
SELECT x,y,z
FROM table1 a, table3 b -- different table
BEGIN
FOR aRecord in myCursor
LOOP
proc2(aRecord.x, aRecord.y, aRecord.z);
END LOOP;
COMMIT;
END;
end if
我不想为了一个不同的表而重复代码。 关于如何改进这一点有什么建议吗?
提前致谢
I have two cursors in my procedure that only differ on the table name that they join to.
The cursor that is used is determined by a parameter passed into the procedure
if (param = 'A') then
DECLARE CURSOR myCursor IS
SELECT x,y,z
FROM table1 a, table2 b
BEGIN
FOR aRecord in myCursor
LOOP
proc2(aRecord.x, aRecord.y, aRecord.z);
END LOOP;
COMMIT;
END;
elsif (param = 'B') then
DECLARE CURSOR myCursor IS
SELECT x,y,z
FROM table1 a, table3 b -- different table
BEGIN
FOR aRecord in myCursor
LOOP
proc2(aRecord.x, aRecord.y, aRecord.z);
END LOOP;
COMMIT;
END;
end if
I don't want to repeat the code for the sake of one different table.
Any suggestions on how to improve this?
Thanks in advance
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您可以像这样使用 REF CURSOR(为了方便起见,我使用了 EMP 和 DEPT 表):
You can use a REF CURSOR like this (I used the EMP and DEPT tables for convenience):
尽管托尼·安德鲁斯提出的解决方案是正确的方法,但这里有一个快速而肮脏的替代方法:
显然,这可能比托尼提出的干净解决方案慢得多。
Though Tony Andrews proposed solution is the right way to do it, here is a little quick-and-dirty alternative way:
Obviously, this can be considerably slower than the clean solution proposed by Tony.