declare
l_statement varchar2(2000);
date2 date:= to_date('29-02-2024','dd-mm-yyyy');
begin
l_statement := 'delete from employee_tab where id=125 and joined_date =' || date2;
execute immediate l_statement;
exception
when others then
raise;
end;
/
错误报告-
ORA-00933: SQL command not properly ended
ORA-06512: at line 12
ORA-06512: at line 8
00933. 00000 - "SQL command not properly ended"
*Cause:
*Action:
那是因为你正在执行的语句无效。
样本数据:
SQL> CREATE TABLE employee_tab
2 AS
3 SELECT 125 id, DATE '2024-02-29' joined_date FROM DUAL
4 UNION ALL
5 SELECT 555, TRUNC (SYSDATE) FROM DUAL;
Table created.
SQL> set serveroutput on
我不是盲目地执行它,而是显示语句:
SQL> DECLARE
2 l_statement VARCHAR2 (2000);
3
4 date2 DATE := TO_DATE ('29-02-2024', 'dd-mm-yyyy');
5 BEGIN
6 l_statement := 'delete from employee_tab where id=125 and joined_date =' || date2;
7
8 DBMS_OUTPUT.put_Line (l_statement);
9 -- EXECUTE IMMEDIATE l_statement;
10 EXCEPTION
11 WHEN OTHERS
12 THEN
13 RAISE;
14 END;
15 /
delete from employee_tab where id=125 and joined_date =29.02.24
PL/SQL procedure successfully completed.
SQL>
你能运行吗?不:
SQL> delete from employee_tab where id=125 and joined_date =29.02.24;
delete from employee_tab where id=125 and joined_date =29.02.24
*
ERROR at line 1:
ORA-00933: SQL command not properly ended
SQL>
不要连接日期值;绑定它。
样本数据(再次):
SQL> alter session set nls_date_format = 'dd.mm.yyyy';
Session altered.
SQL> select * From employee_tab;
ID JOINED_DAT
---------- ----------
125 29.02.2024
555 19.06.2024
您的新
代码;关注EXECUTE IMMEDIATE
!
SQL> DECLARE
2 l_statement VARCHAR2 (2000);
3
4 date2 DATE := TO_DATE ('29-02-2024', 'dd-mm-yyyy');
5 BEGIN
6 l_statement := 'delete from employee_tab where id=125 and joined_date = :a';
7
8 EXECUTE IMMEDIATE l_statement
9 USING date2;
10 EXCEPTION
11 WHEN OTHERS
12 THEN
13 RAISE;
14 END;
15 /
PL/SQL procedure successfully completed.
结果:
SQL> select * from employee_tab;
ID JOINED_DAT
---------- ----------
555 19.06.2024
SQL>
对;行已被删除。