在ORACLE下面查询工作正常。
DECLARE ABC VARCHAR2(50) :='SOME VALUE';
BEGIN
IF (ABC = 'SOME VALUE')
THEN
dbms_output.put_line('1');
ELSE
dbms_output.put_line('2');
END IF;
END;
但如果我在IF中使用SELECT,则会抛出错误。
DECLARE ABC VARCHAR2(50) :='SOME VALUE';
BEGIN
IF (ABC = 'SOME VALUE')
THEN
select cast('1' as VARCHAR(50)) from dual;
ELSE
select cast('2' as VARCHAR(50)) from dual;
END IF;
END;
ERROR:
ORA-06550: line 6, column 1:
PLS-00428: an INTO clause is expected in this SELECT statement
ORA-06550: line 8, column 1:
PLS-00428: an INTO clause is expected in this SELECT statement
答案 0 :(得分:1)
使用into子句
选择变量DECLARE ABC VARCHAR2(50) :='SOME VALUE';
BEGIN
IF (ABC = 'SOME VALUE')
THEN
select cast('1' as VARCHAR(50)) into ABC from dual;
ELSE
select cast('2' as VARCHAR(50)) into ABC from dual;
END IF;
END;
答案 1 :(得分:1)
您正在将SQL
与PL/SQL
混合。它们不同,如果没有 INTO 子句,则不能在PL / SQL块中包含 SELECT 语句。
由于您已将变量声明为VARCHAR250)
,因此无需使用 CAST 。
发布一个SELECT .. INTO
没有做任何关于你正在获取的值的事情是没有意义的。
例如,
SQL> SET serveroutput ON
SQL> DECLARE
2 ABC VARCHAR2(50) :='SOME VALUE';
3 BEGIN
4 IF (ABC = 'SOME VALUE') THEN
5 SELECT '1' INTO abc FROM dual;
6 ELSE
7 SELECT '2' INTO abc FROM dual;
8 END IF;
9 dbms_output.put_line('Output is '||abc);
10 END;
11 /
Output is 1
PL/SQL procedure successfully completed.
SQL>