Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Semicolon and Single Quote Issue in SQL Insert Statements

I am executing a SQL script which has spaces, semicolons, single-quotes, special characters etc. in of the column called prod_desc. I need to execute the script from SQLPLUS. I have the set the following in the SQLPLUS however, single quotes, semicolons are not taken into consideration.

set sqlblanklines on

See the below insert statement for instance, semicolon is after the line break. This is causing an error during execution. How can I resolve the issue?

SET DEFINE OFF;
Insert into PROD_DESC
   (PROD_NO, PROD_DESC
    )
Values
   ('XYZ', 'test
semicolon test;
'
);

Table Structure

CREATE TABLE prod_desc
(
   prod_no     VARCHAR2 (100),
   prod_desc   VARCHAR2 (1000)
);
like image 389
Jacob Avatar asked Mar 07 '17 07:03

Jacob


2 Answers

Espace the semicolon:

SET DEF OFF;
Insert into PROD_DESC(PROD_NO, PROD_DESC)
Values('XYZ', 'test semicolon test\;');
like image 183
Gnqz Avatar answered Nov 11 '22 03:11

Gnqz


I also like to use the q-quote syntax. It removes the need for escaping, and makes quoting quotes much easier.

The syntax is

q'[ <your string> ]'

<Your string> can contain quotes or any other characters the normally need escaping. The [ can actually be substituted for or "paired" characters, such as {} or ().

For example:

SQL> 
SQL> CREATE TABLE prod_desc
  2  (
  3     prod_no     VARCHAR2 (10),
  4     prod_desc   VARCHAR2 (100)
  5  );

Table created.

SQL> 
SQL> SET DEFINE OFF;
SQL> Insert into PROD_DESC
  2     (PROD_NO, PROD_DESC
  3      )
  4  Values
  5     ('XYZ', q'[test
  6  semicolon test
  7  ; ]'
  8  );

1 row created.

SQL> 
SQL> 
SQL> select * from prod_desc;

PROD_NO    PROD_DESC
---------- ----------------------------------------------------------------------------------------------------
XYZ        test
           semicolon test
           ;
like image 32
BobC Avatar answered Nov 11 '22 02:11

BobC