在 Oracle SQL Developer 代码窗口中运行带有变量的 sql 代码
我正在使用 Oracle SQL Developer 编写代码。我有一个简单的 select 语句,可以工作:
SELECT
CFS.CAE_SEC_ID,
CFS.FM_SEC_CODE,
CFS.LAST_USER_ID,
case
when 1 = 1 then
sl.usbank_to_edit
else
case
when 'ENT\CB174' = CFS.last_user_id then
sl.owner_to_edit
else
sl.to_edit
end
end canEdit
FROM
CAEDBO.CAE_FOF_SECURITY CFS
INNER JOIN caedbo.CAE_DATA_SET_ELEMENT CDSE_STAT
ON (CDSE_STAT.DATA_SET_ELEMENT_ID = CFS.APPR_STATUS)
INNER JOIN caedbo.CAE_STATE_LOOKUP sl
ON (sl.object_state = CDSE_STAT.data_set_element_id)
where
cfs.CAE_SEC_ID in (3741, 3744, 3748, 3752);
但是我想向其中添加一些变量并引用语句中的变量,类似于下面的内容,并在代码窗口中运行它。我该如何正确地做到这一点?
DECLARE
p_USBank_n NUMBER;
p_user_id_c VARCHAR2(20);
BEGIN
p_USBank_n := 1;
p_user_id_c := 'ENT\CB174';
SELECT
CFS.CAE_SEC_ID,
CFS.FM_SEC_CODE,
CFS.LAST_USER_ID,
case
when p_USBank_n = 1 then
sl.usbank_to_edit
else
case
when p_user_id_c = CFS.last_user_id then
sl.owner_to_edit
else
sl.to_edit
end
end canEdit
FROM
CAEDBO.CAE_FOF_SECURITY CFS
INNER JOIN caedbo.CAE_DATA_SET_ELEMENT CDSE_STAT
ON (CDSE_STAT.DATA_SET_ELEMENT_ID = CFS.APPR_STATUS)
INNER JOIN caedbo.CAE_STATE_LOOKUP sl
ON (sl.object_state = CDSE_STAT.data_set_element_id)
where
cfs.CAE_SEC_ID in (3741, 3744, 3748, 3752);
END;
当我在 sql 窗口中运行此命令时,我收到以下消息:
Error report:
ORA-06550: line 8, column 5:
PLS-00428: an INTO clause is expected in this SELECT statement
06550. 00000 - "line %s, column %s:\n%s"
*Cause: Usually a PL/SQL compilation error.
*Action:
I'm writing code using Oracle SQL Developer. I have a simple select statement that works:
SELECT
CFS.CAE_SEC_ID,
CFS.FM_SEC_CODE,
CFS.LAST_USER_ID,
case
when 1 = 1 then
sl.usbank_to_edit
else
case
when 'ENT\CB174' = CFS.last_user_id then
sl.owner_to_edit
else
sl.to_edit
end
end canEdit
FROM
CAEDBO.CAE_FOF_SECURITY CFS
INNER JOIN caedbo.CAE_DATA_SET_ELEMENT CDSE_STAT
ON (CDSE_STAT.DATA_SET_ELEMENT_ID = CFS.APPR_STATUS)
INNER JOIN caedbo.CAE_STATE_LOOKUP sl
ON (sl.object_state = CDSE_STAT.data_set_element_id)
where
cfs.CAE_SEC_ID in (3741, 3744, 3748, 3752);
However I want to add some variables to it and reference the variables in the statement, similar to below, and also run it in the code window. How do I do this correctly?
DECLARE
p_USBank_n NUMBER;
p_user_id_c VARCHAR2(20);
BEGIN
p_USBank_n := 1;
p_user_id_c := 'ENT\CB174';
SELECT
CFS.CAE_SEC_ID,
CFS.FM_SEC_CODE,
CFS.LAST_USER_ID,
case
when p_USBank_n = 1 then
sl.usbank_to_edit
else
case
when p_user_id_c = CFS.last_user_id then
sl.owner_to_edit
else
sl.to_edit
end
end canEdit
FROM
CAEDBO.CAE_FOF_SECURITY CFS
INNER JOIN caedbo.CAE_DATA_SET_ELEMENT CDSE_STAT
ON (CDSE_STAT.DATA_SET_ELEMENT_ID = CFS.APPR_STATUS)
INNER JOIN caedbo.CAE_STATE_LOOKUP sl
ON (sl.object_state = CDSE_STAT.data_set_element_id)
where
cfs.CAE_SEC_ID in (3741, 3744, 3748, 3752);
END;
When I run this in a sql window I get the message below:
Error report:
ORA-06550: line 8, column 5:
PLS-00428: an INTO clause is expected in this SELECT statement
06550. 00000 - "line %s, column %s:\n%s"
*Cause: Usually a PL/SQL compilation error.
*Action:
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
如果我正确阅读本文,我认为您正在寻找 Oracle 替换变量.
每次都会提示你输入值,通过使用 &val 会提示你 @runtime
将其更改为 &&var 让它保留该值,然后使用它
来清除它
现在你可以通过使用 DEFINE 在页面顶部设置这些(从而避免提示),这样
双与号将“保留”该值,直到您取消定义它(见上文)或重新定义它。
if I am reading this correctly, I think you are looking for Oracle Substitution variables.
this will prompt you each time to input the values, by using &val it will prompt you @ runtime
change it to &&var to have it retain the value, then use
to clear it
Now you can set these at the top of the page (thus avoiding the prompt) by utilizing DEFINE as such
the double ampersand will 'retain' the value until you UNDEFINE it (see above) or redefine it.
错误消息解释了问题;在 PL/SQL 块内,您必须选择
INTO
某些内容,并且不能像使用普通 SQL 那样将查询结果转储到屏幕上。 (有很多方法可以做到这一点,但对于您想要在这里实现的目标来说可能过于复杂)。如果您不想按照 @Harrison 建议使用替换变量,则可以使用在单独的匿名块中开始时定义的绑定变量。然后您可以在普通 SQL 中引用绑定变量:
The error message explains the problem; inside a PL/SQL block you have to select
INTO
something, and you can't just dump the results of a query to screen as you can with plain SQL. (There are ways to do it but probably overly complicated for what it looks like you're trying to achieve here).If you don't want to use substitution variables as @Harrison suggested, you can use bind variables which you define at the start in a separate anonymous block. You can then refer to the bind variable in the plain SQL: