从 Oracle JDBC 瘦驱动程序执行 PL/SQL 开始/结束过程

发布于 2024-11-24 23:36:12 字数 1271 浏览 5 评论 0原文

我正在尝试创建 Oracle PL/SQL 过程并通过 Oracle JDBC(瘦驱动程序)执行它们。这是完整的 PL/SQL 脚本:

begin
for i in (select owner, constraint_name, table_name from all_constraints where owner = 'SCHEMA' and status = 'ENABLED') LOOP
execute immediate 'alter table SCHEMA.'||i.table_name||' disable constraint SCHEMA.'||i.constraint_name||'';
end loop;
end;
/
begin
for i in (select table_name from all_tables where owner = 'SCHEMA') LOOP
execute immediate 'truncate table SCHEMA.'||i.table_name||'';
end loop;
end;
/
begin
for i in (select owner, constraint_name, table_name from all_constraints where owner = 'SCHEMA' and status = 'DISABLED') LOOP
execute immediate 'alter table SCHEMA.'||i.table_name||' enable constraint SCHEMA.'||i.constraint_name||'';
end loop;
end;
/

在 java 中,我按“/”进行拆分,因此每个开始结束块都在单独的语句中执行。执行该语句的 java 代码是:

CallableStatement c = dbc.getConnection().prepareCall(sqlStatement);
c.executeUpdate();

我收到以下错误:

java.sql.SQLSyntaxErrorException: ORA-00933: SQL command not properly ended
ORA-06512: at line 3

如何格式化它并在 JDBC 中执行 PL/SQL?

更新:澄清一下,所有三个语句在执行时都没有分割的“/”分隔符。

更新: Oracle 服务器是以下版本:Oracle Database 11g 版本 11.2.0.1.0 - 64 位生产

I am trying to create Oracle PL/SQL procedures and execute them via Oracle JDBC (thin driver). Here is the full PL/SQL script:

begin
for i in (select owner, constraint_name, table_name from all_constraints where owner = 'SCHEMA' and status = 'ENABLED') LOOP
execute immediate 'alter table SCHEMA.'||i.table_name||' disable constraint SCHEMA.'||i.constraint_name||'';
end loop;
end;
/
begin
for i in (select table_name from all_tables where owner = 'SCHEMA') LOOP
execute immediate 'truncate table SCHEMA.'||i.table_name||'';
end loop;
end;
/
begin
for i in (select owner, constraint_name, table_name from all_constraints where owner = 'SCHEMA' and status = 'DISABLED') LOOP
execute immediate 'alter table SCHEMA.'||i.table_name||' enable constraint SCHEMA.'||i.constraint_name||'';
end loop;
end;
/

In java I am splitting on '/' so each begin end block is executed in a separate statement. The java code to execute the statement is:

CallableStatement c = dbc.getConnection().prepareCall(sqlStatement);
c.executeUpdate();

I'm receiving the following error:

java.sql.SQLSyntaxErrorException: ORA-00933: SQL command not properly ended
ORA-06512: at line 3

How do I format this and execute the PL/SQL in JDBC?

Updated: To clarify, all three statements are executed without the '/' delimiter that is split on.

Updated: The oracle server is the following version: Oracle Database 11g Release 11.2.0.1.0 - 64bit Production

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

一张白纸 2024-12-01 23:36:12

在“enable /diable”约束中,您不应添加架构名称(您的“SCHEMA”)。

从手册中:
ALTER TABLE

您的示例:

begin
    for i in (select owner, constraint_name, table_name
              from   all_constraints
              where  owner = 'SCHEMA'
              and    status = 'ENABLED')
    loop
        execute immediate 'alter table SCHEMA.' || i.table_name ||
                          ' disable constraint ' || i.constraint_name;
    end loop;
end;

测试查询

select ac.constraint_name, ac.table_name, ac.status, ac.owner
from   all_constraints ac
where  ac.owner = 'HR'
and    ac.constraint_name = 'EMP_SALARY_MIN'

结果

CONSTRAINT_NAME                TABLE_NAME                     STATUS   OWNER
------------------------------ ------------------------------ -------- --------------------------------------------------------------------------------
EMP_SALARY_MIN                 EMPLOYEES                      ENABLED  HR

正确的动态sql

begin
    execute immediate 'alter table HR.EMPLOYEES disable constraint EMP_SALARY_MIN';
end;

上一次查询结果

CONSTRAINT_NAME                TABLE_NAME                     STATUS   OWNER
------------------------------ ------------------------------ -------- --------------------------------------------------------------------------------
EMP_SALARY_MIN                 EMPLOYEES                      DISABLED HR

In the "enable /diable" constraint you shouldn't add the schema name (your'SCHEMA).

From the manual:
ALTER TABLE

Your example:

begin
    for i in (select owner, constraint_name, table_name
              from   all_constraints
              where  owner = 'SCHEMA'
              and    status = 'ENABLED')
    loop
        execute immediate 'alter table SCHEMA.' || i.table_name ||
                          ' disable constraint ' || i.constraint_name;
    end loop;
end;

Test Query

select ac.constraint_name, ac.table_name, ac.status, ac.owner
from   all_constraints ac
where  ac.owner = 'HR'
and    ac.constraint_name = 'EMP_SALARY_MIN'

Result

CONSTRAINT_NAME                TABLE_NAME                     STATUS   OWNER
------------------------------ ------------------------------ -------- --------------------------------------------------------------------------------
EMP_SALARY_MIN                 EMPLOYEES                      ENABLED  HR

Correct dynamic sql

begin
    execute immediate 'alter table HR.EMPLOYEES disable constraint EMP_SALARY_MIN';
end;

Previous query result

CONSTRAINT_NAME                TABLE_NAME                     STATUS   OWNER
------------------------------ ------------------------------ -------- --------------------------------------------------------------------------------
EMP_SALARY_MIN                 EMPLOYEES                      DISABLED HR
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文