开发者

Oracle 10g multiple DELETE statements

I'm building a dml file that first deletes records that may be in the table, then inserts records. Example:

DELETE from foo where field1='bar';
DELETE from foo where fiel开发者_运维百科ds1='bazz';

INSERT ALL
 INTO foo(field1, field2) values ('bar', 'x')
 INTO foo(field1, field2) values ('bazz', 'y')
SELECT * from DUAL;

When I run the insert statement by itself, it runs fine. When I run the deletes, only the last delete runs.

Also, it seems to be necessary to end the multiple insert with the select, is that so? If so, why is that necessary? In the past, when using MySQL, I could just list multiple delete and insert statements, all individually ending with a semicolon, and it would run fine.


You apparently have a typo, since you're calling it either field1 or fields1 in the DELETEs.

You're doing it the hard way, though.

DELETE FROM Foo WHERE Field1 in ('bar', 'bazz');

INSERT INTO Foo (Field1, Field2) 
  SELECT 'bar', 'x' FROM System.dual UNION 
  SELECT 'bazz', 'y' FROM System.dual;

Not sure if Oracle requires the FROM System.dual, although I seem to recall it did. SQL Server will allow just a SELECT 'bar', 'x' instead.


Any particular reason you don't want to:

insert into foo(field1, field2) values('bar', 'x');
insert into foo(field1, field2) values('bazz', 'y');
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜