Oracle 存储过程与 case when 位于 where 原因中
以下存储过程出现错误。看来错误是在 where 原因的 case 语句中。我该如何修复它?
create or replace PROCEDURE APPEND_HIST_TBLS_PROC AS
CurTerm varchar2(4) := '1222';
PS_ACAD_PROG_HISTORY varchar2(35) := 'PS_ACAD_PROG_HISTORY_' || CurTerm;
Begin
execute immediate 'insert into ' || PS_ACAD_PROG_HISTORY
|| '(select sysdate as date_created,
EFFDT,
ADMIT_TERM,
EXP_GRAD_TERM,
CAMPUS
from ERP_ACAD
where CASE WHEN SUBSTR(ADMIT_TERM,4,1)= '6' THEN SUBSTR(ADMIT_TERM,1,3) || '9' ELSE ADMIT_TERM END = '||CurTerm ||'
)';
END APPEND_HIST_TBLS_PROC;
The following stored procedure complied with errors. It seems the errors are in the case statement in the where cause. How do I fix it?
create or replace PROCEDURE APPEND_HIST_TBLS_PROC AS
CurTerm varchar2(4) := '1222';
PS_ACAD_PROG_HISTORY varchar2(35) := 'PS_ACAD_PROG_HISTORY_' || CurTerm;
Begin
execute immediate 'insert into ' || PS_ACAD_PROG_HISTORY
|| '(select sysdate as date_created,
EFFDT,
ADMIT_TERM,
EXP_GRAD_TERM,
CAMPUS
from ERP_ACAD
where CASE WHEN SUBSTR(ADMIT_TERM,4,1)= '6' THEN SUBSTR(ADMIT_TERM,1,3) || '9' ELSE ADMIT_TERM END = '||CurTerm ||'
)';
END APPEND_HIST_TBLS_PROC;
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
动态SQL很难维护和调试。过程编译成功这一事实没有告诉关于动态语句本身的信息 - 它只是说过程“按原样”没有错误。
编写语句并将其存储到局部变量中,然后显示其内容是一个好习惯;一旦确认没问题,就执行它。
另外,由于您必须转义单引号(有时它会变得令人讨厌,有几个连续的单引号可以满足您的需要),因此使用
q-quoting
机制可以让您编写“正常”语句(那么单引号确实是单引号)。像这样的事情:
让我们尝试一下:
输出看起来很难看(这是在过程中让它漂亮的代价),但是 - 如果你格式化它 - 它看起来像这样:
所以,如果你真的可以执行它(我不能,我没有你的表),然后取消注释
立即执行
并使用该过程。否则,请修复该语句。Dynamic SQL is difficult to maintain and debug. The fact that procedure compiled successfully tells nothing about the dynamic statement itself - it just says that there were no errors in the procedure "as is".
It is a good habit to compose the statement and store it into a local variable and then display its contents; once you verify it is OK, then execute it.
Also, as you have to escape single quotes (sometimes it becomes nasty, with several consecutive single quotes which do what you need), use the
q-quoting
mechanism which lets you write "normal" statements (single quotes really are single, then).Something like this:
Let's try it:
Output looks ugly (that's the price of having it pretty in the procedure), but - if you format it - it looks like this:
So, if you can really execute it (I can't, I don't have your tables), then uncomment
execute immediate
and use the procedure. Otherwise, fix the statement.似乎是引用的问题。您是否尝试过此操作:
假设您想在 admin_term 子字符串的末尾附加 9,也使用
CONCAT
而不是双管道。确实取决于您的数据和您要实现的逻辑。该过程现在应该可以编译,但它是否达到所需的结果 - 取决于您在此处尝试实现的逻辑。
Seems like an issue with quoting. Have you tried this:
Also used
CONCAT
instead of double pipe assuming you want to append 9 at the end of sub string of admin_term.Really depends on your data and the logic you are trying to implement. This procedure should now compile but whether it achieves the desired result - depends on the logic you are trying to implement here.