41

I have a SQL script that will insert a long string into a table. The string contains a new line (and this new line is absolutely necessary), so when it is written in a text file, the query is split to multiple lines. Something like:

insert into table(id, string) values (1, 'Line1goesHere 

Line2GoesHere 
blablablabla
');

This runs ok in Toad, but when I save this as a .sql file and run it using sqlplus, it considers each line a separate query, meaning that each line will fail (beacuse insert into table(id, string) values (1, 'Line1goesHere, Line2GoesHere aren't well-formated scripts.

SP2-0734: unknown command beginning "Line2GoesHere" - rest of line ignored.

Is there a way to fix this?

marcospereira
  • 12,045
  • 3
  • 46
  • 52
Louis Rhys
  • 34,517
  • 56
  • 153
  • 221

4 Answers4

62

Enable SQLBLANKLINES to allow blank lines in SQL statements. For example:

SET SQLBLANKLINES ON
insert into table(id, string) values (1, 'Line1goesHere 
Line2GoesHere 

blablablabla
');

The premise of this question is slightly wrong. SQL*Plus does allow multi-line strings by default. It is only blank lines that cause problems.

Jon Heller
  • 34,999
  • 6
  • 74
  • 132
jim mcnamara
  • 16,005
  • 2
  • 34
  • 51
23

You can also use not-well-known feature of Oracle's SQL: Perl style quoted strings.

SQL> select q'[f dfgdfklgdfkjgd
  2  sdffdslkdflkgj dglk
  3  glfdglkjdgkldj ]'
  4  from dual;

Q'[FDFGDFKLGDFKJGDSDFFDSLKDFLKGJDGLKGLFDGLKJDGKLDJ]'
----------------------------------------------------
f dfgdfklgdfkjgd
sdffdslkdflkgj dglk
glfdglkjdgkldj
Jérôme MEVEL
  • 7,031
  • 6
  • 46
  • 78
ibre5041
  • 4,903
  • 1
  • 20
  • 35
4

SQL*Plus Manual

You can end a SQL command in one of three ways:

  1. with a semicolon (;)
  2. with a slash (/) on a line by itself
  3. with a blank line

A blank line in a SQL statement or script tells SQL*Plus that you have finished entering the command, but do not want to run it yet. Press Return at the end of the last line of the command.

Turning SQLBLANKLINES on in this situation may be the answer, but even with it you still have to worry about the following SQL*Plus commands.

@  ("at" sign)        (Start of line) 
@@ (double "at" sign) (Start of line) 
#   SQLPREFIX         (Start of line)
.   BLOCKTERMINATOR   (Start of line and by itself)
/  (slash)            (Start of line and by itself)
;   SQLT[ERMINATOR]   (Start of line and by itself, or at the end)

SQLPREFIX is something that you cannot turn off; it's a feature of SQL*Plus. BLOCKTERMINATOR can be activated or disabled. Slash on the other hand if it appears at the start of a new line will cause it to execute the contents in the buffer. SQL[TERMINATOR] has a similar behavior.

Chad
  • 2,938
  • 3
  • 27
  • 38
  • So how do you insert a multiline string with empty lines and lines starting with # ? – FORTRAN Mar 29 '18 at 06:47
  • Just solved it, I use set sqlprefix * before the query and then set it back to normal afterwards set sqlprefix # – FORTRAN Mar 29 '18 at 07:01
3

Another way of inserting newlines to a string is concatenating:

chr(13)||chr(10)

(on windows)

or just:

chr(10)

(otherwise)

A.B.Cade
  • 16,735
  • 1
  • 37
  • 53