Oracle 使用序列号删除行

发布于 2024-11-06 07:12:27 字数 199 浏览 5 评论 0原文

我的数据库知识非常有限,我尝试使用以下查询删除一行:

DELETE FROM table WHERE (选择序列.CURRVAL FROM DUAL);

这在 .sql 中用于在 Maven 中运行集成测试后清理数据库。我已经用谷歌搜索过,但仍然没有找到删除语句起作用的答案。任何帮助表示赞赏!

My DB knowledge is quite limited and I am trying to delete a row with the following query:

DELETE FROM table
WHERE column in (select sequence.CURRVAL FROM DUAL);

This is used in a .sql to clean up the database after integration tests are run in Maven. I have Googled it but still haven't found an answer for the delete statement to work. Any help is appreciated!

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

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

发布评论

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

评论(2

慈悲佛祖 2024-11-13 07:12:27

您不能在 DELETE 语句中使用 CURRVAL,您可以在此处阅读:http://www .orafaq.com/wiki/ORA-02287

但是由于您使用的是 SQL 脚本,因此您可以在 SQL*Plus 中这样做:

SQL> create table t( id number);

Table created.

SQL> create sequence seq;

Sequence created.

SQL> insert into t (id) values (seq.nextval);

1 row created.

SQL> column i new_value curseqval
SQL> select seq.currval i from dual;

         I
----------
         1

1 row selected.

SQL> delete from t where id = &curseqval;
old   1: delete from t where id = &curseqval
new   1: delete from t where id =          1

1 row deleted.

问候,
抢。

You can't use a CURRVAL in a DELETE statement, as you can read here: http://www.orafaq.com/wiki/ORA-02287

But since you are using SQL scripts, you can do it like this in SQL*Plus:

SQL> create table t( id number);

Table created.

SQL> create sequence seq;

Sequence created.

SQL> insert into t (id) values (seq.nextval);

1 row created.

SQL> column i new_value curseqval
SQL> select seq.currval i from dual;

         I
----------
         1

1 row selected.

SQL> delete from t where id = &curseqval;
old   1: delete from t where id = &curseqval
new   1: delete from t where id =          1

1 row deleted.

Regards,
Rob.

关于从前 2024-11-13 07:12:27

1) DUAL中的sequence.CURRVAL是单个离散值;使用 IN 是不合适的

2) 因为您引用的是序列。CURRVAL 您不需要使用子查询,您可以直接访问该值。

尝试:

DELETE FROM table WHERE column = sequence.CURRVAL;

假设序列被用作主键(某处),这将导致删除最近插入的键记录,或者如果该列是外键,则只有那些被删除的记录与最近的记录相关。

哎呀...
我已经做了足够多的删除查询,所以我没有检查我的工作。虽然我从未想过在删除中使用序列值,但我从未想过您不能这样做。

但这会起作用:

declare del_key number;
begin
select  seq_so_test.currval into del_key from dual;
delete from t_so_test where id = del_key;
 end;

1) sequence.CURRVAL from DUAL is a single discrete value; the use of IN is inappropriate

2) because you are referencing sequence.CURRVAL you need not use a subquery, you may access this value directly.

try:

DELETE FROM table WHERE column = sequence.CURRVAL;

assuming the sequence is being used as a primary key (somewhere) this will result in the deletion of the most recently insert key record or if the column is a foreign key, then only those records that are related to the most recent record.

oops...
I've done enough delete queries that I didn't check my work. While I have never thought to use sequence values in a delete it never occurred to me that you couldn't do it.

this, however will work:

declare del_key number;
begin
select  seq_so_test.currval into del_key from dual;
delete from t_so_test where id = del_key;
 end;
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文