ora-00933:SQL 命令未正确结束

发布于 2024-08-02 11:15:03 字数 1179 浏览 5 评论 0原文

我有以下代码:

begin
for i in 1..2 loop
insert into dba_xy.despatch
select desp_id_seq.nextval,
      dbms_random.string('U',5),
      trunc(dbms_random.value(0000,9999)),
      prod_id from dba_xy.product 
              prod_name from dba_xy.product;        
end loop;

结束;

当我运行它时,oracle 给出以下错误消息:

prod_name from dba_xy.product;
                        *

ERROR at line 8: ORA-06550: 第 8 行,第 29 列: PL/SQL: ORA-00933: SQL 命令未正确结束 ORA-06550: 第 3 行,第 2 列: PL/SQL:忽略 SQL 语句

我想要做的是将现有的 prod_id 和 prod_name 与插入到调度表中的新数据链接起来。我已将 prod_name 设置为产品表中的唯一键,将 prod_id 设置为主键,并将两者设置为调度表中的外键约束。我需要将 prod_name 包含到调度表中,以便该表的读者能够更多地了解需要找到什么 prod_name 等,而不是仅仅提供 prod_id ,这对他们来说根本没有意义。但也许我在想我在调度表中不需要 prod_id 。 请帮忙。

从调度表中删除 prod_id 列后,我更改了代码:

begin
for i in 1..2 loop
insert into dba_xy.despatch
select desp_id_seq.nextval,
      dbms_random.string('U',5),
      trunc(dbms_random.value(0000,9999)),
              prod_name from dba_xy.product;        
end loop;

end; /

并且出现以下关于唯一约束的错误消息: 开始 * 第 1 行错误: ORA-00001: 违反唯一约束 (DBA_XY.PROD_NAME_UC) ORA-06512: 在第 3 行

I have the following code:

begin
for i in 1..2 loop
insert into dba_xy.despatch
select desp_id_seq.nextval,
      dbms_random.string('U',5),
      trunc(dbms_random.value(0000,9999)),
      prod_id from dba_xy.product 
              prod_name from dba_xy.product;        
end loop;

end;

When I run it, oracle gives me the following error message:

prod_name from dba_xy.product;
                        *

ERROR at line 8:
ORA-06550: line 8, column 29:
PL/SQL: ORA-00933: SQL command not properly ended
ORA-06550: line 3, column 2:
PL/SQL: SQL Statement ignored

What I'm trying to do is link the existing prod_id and prod_name with new data inserted into the despatch table. I have set prod_name as a unique key in the product table and prod_id as the primary key and have set both as foreign key constraints in the despatch table. I need to include the prod_name into the despatch table to allow readers of the table to have more understanding of what prod_name needs to be found etc, rather than just giving the prod_id which will make no sense to them at all. But maybe I was thinking that I don't need prod_id in the despatch table.
Please help.

After dropping the prod_id column from the despatch table, i altered my code:

begin
for i in 1..2 loop
insert into dba_xy.despatch
select desp_id_seq.nextval,
      dbms_random.string('U',5),
      trunc(dbms_random.value(0000,9999)),
              prod_name from dba_xy.product;        
end loop;

end;
/

and the following error message came up about the unique constraint:
begin
*
ERROR at line 1:
ORA-00001: unique constraint (DBA_XY.PROD_NAME_UC) violated
ORA-06512: at line 3

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

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

发布评论

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

评论(2

北斗星光 2024-08-09 11:15:03

您的 ORA-00933 错误是由于 SELECT 语句格式不正确造成的:

SELECT desp_id_seq.nextval,
       dbms_random.string('U',5),
       TRUNC(dbms_random.value(0000,9999)),
       prod_id from dba_xy.product 
       prod_name from dba_xy.product; 

...应该是:

SELECT DESP_ID_SEQ.nextval,
       DBMS_RANDOM.string('U',5),
       TRUNC(DBMS_RANDOM.value(0000,9999)),
       t.prod_id,
       t.prod_name 
  FROM dba_xy.product t; 

您缺少分隔 prod_idprod_name 列的逗号,并且另外,在错误的位置有多余的 FROM dba_xy.product 声明。

也就是说,dba_xy.despatch 表应该只包含 prod_id。如果您需要提供人类可读的数据版本,我建议您构建一个视图。例子:

CREATE VIEW despatch_vw AS
SELECT t.prod_id,
       p.prod_name
  FROM dba_xy.despatch t
  JOIN dba_xy.product p ON p.prod_id = t.prod_id

Your ORA-00933 error is due to an incorrectly formatted SELECT statement:

SELECT desp_id_seq.nextval,
       dbms_random.string('U',5),
       TRUNC(dbms_random.value(0000,9999)),
       prod_id from dba_xy.product 
       prod_name from dba_xy.product; 

...when it should be:

SELECT DESP_ID_SEQ.nextval,
       DBMS_RANDOM.string('U',5),
       TRUNC(DBMS_RANDOM.value(0000,9999)),
       t.prod_id,
       t.prod_name 
  FROM dba_xy.product t; 

You were missing the comma to separate the prod_id and prod_name columns, and additionally had a redundant FROM dba_xy.product declaration in the wrong location.

That said, the dba_xy.despatch table should only contain the prod_id. If you need to provide a human readable version of the data, I recommend you construct a view. Example:

CREATE VIEW despatch_vw AS
SELECT t.prod_id,
       p.prod_name
  FROM dba_xy.despatch t
  JOIN dba_xy.product p ON p.prod_id = t.prod_id
Bonjour°[大白 2024-08-09 11:15:03

您是否会因为插入相同的行两次而违反唯一约束? “i”应该用在插入语句的 where 子句中还是您真的希望将行插入两次?

您的第一个语句有两个 FROM 子句,这就是您收到语法错误的原因。

select desp_id_seq.nextval,
          dbms_random.string('U',5),
          trunc(dbms_random.value(0000,9999)),
          prod_id, --from dba_xy.product
              prod_name from dba_xy.product;    

Could you be getting the unique constraint violation because you are inserting the same rows twice? Is "i" supposed to be used in the where clause of the insert statement or do you really want the rows inserted twice?

Your first statement has two FROM clauses, which is why you are getting a syntax error.

select desp_id_seq.nextval,
          dbms_random.string('U',5),
          trunc(dbms_random.value(0000,9999)),
          prod_id, --from dba_xy.product
              prod_name from dba_xy.product;    
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文