3

I'm trying to run a script using sqlplus. My script is a simple delete statement. I execute it by putting the following in my ksh terminal:

sqlplus username/'password' @../sql/delete_societes.sql

../sql/delete_societes.sql is

DELETE FROM f2020.SOCIETES;
/

For some reason, it runs twice, causing the output "0 lines deteleted" to be printed twice and causing errors when I try to do an insert instead of a delete.

2 Answers 2

12

Make your script do either;

DELETE FROM f2020.SOCIETES
/

or

DELETE FROM f2020.SOCIETES;

without the slash.

From the documentation:

/(slash)

Executes the most recently executed SQL command or PL/SQL block which is stored in the SQL buffer.

and in the example further down:

Enter a slash (/) to re-execute the command in the buffer

... which is exactly what you are seeing.

Elsewhere in those docs:

The semicolon (;) means that this is the end of the command. Press Return or click Execute. SQL*Plus processes the command and displays the results

Like many clients SQL*Plus treats the semicolon at the end of your SQL statement as a statement separator - it is not part of the statement itself (which causes some confusion for e.g. dynamic SQL and JDBC calls) - and when it sees it it executes the command. The executed statement stays in the command buffer; and if you list to see the current command buffer, it will not show that semicolon. When you issue a slash it executes the buffer again.


Things are slightly different for PL/SQL; there the PL/SQL block has to be terminated with a semicolon, which is part of the block, and appears in the buffer. You have to use a slash to execute a PL/SQL block.

2
  • Which then begs the question: why doesn't SQL Developer do this? (I found this question when confused why a script that worked fine in SQL Developer failed in SQL*Plus.) Commented Oct 5, 2021 at 17:13
  • @MarkRaymond - good question, and oddly I thought it did. I imagine it's because it doesn't have the same concept of a command buffer that SQL*Plus has. There the slash runs whatever is in the buffer, implicitly terminating the current input if that's active; if you've already terminated (and executed) the statement with a semicolon, or just ended the input with a blank line (depending on sqlblanklines setting) then it just re-executes the buffer. That doesn't map to how SQL Developer works though. It doesn't make much sense when running SQL*Plus non-interacitvely either, I suppose.
    – Alex Poole
    Commented Oct 5, 2021 at 17:31
0

An example where you can see the sqlplus buffer content for SQL and PLSQL.

me@XEPDB1> help run

 RUN
 ---

 Lists and executes the most recently executed SQL command or
 PL/SQL block which is stored in the SQL buffer. The buffer has
 no command history list and does not record SQL*Plus commands.

 R[UN]


me@XEPDB1> help /

 / (slash)
 ---------

 Executes the most recently executed SQL command or PL/SQL block
 which is stored in the SQL buffer. Use slash (/) at the command
 prompt or line number prompt in SQL*Plus command line. The buffer
 has no command history and does not record SQL*Plus commands.

 /

me@XEPDB1> clear buffer                                                                                                                                           [1/651]
buffer cleared
me@XEPDB1> l
SP2-0223: No lines in SQL buffer.
me@XEPDB1> select * from dual
  2  /

D
-
X                          

me@XEPDB1> l
  1* select * from dual                 
me@XEPDB1> select * from dual;

D                    
-           
X

me@XEPDB1> l
  1* select * from dual
me@XEPDB1> /         

D                                       
-
X          

me@XEPDB1> r
  1* select * from dual

D
-
X

me@XEPDB1> begin null; end;
  2  /

PL/SQL procedure successfully completed.

me@XEPDB1> l
  1* begin null; end;
me@XEPDB1> /

PL/SQL procedure successfully completed.

me@XEPDB1> r
  1* begin null; end;

PL/SQL procedure successfully completed.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.