Henry
Henry

Reputation: 825

INSERT INTO using format

I want to INSERT to my database and use as shown below. I get error in EXECUTE line

SQL Error [42601]: ERROR: syntax error at or near "validation"

Is it something specific i need to change here? Keep in mind that's not all columns in that table because rest will be fulfilled automaticly by db.

sql := format('INSERT INTO %s.%s (col1, col2,col3,col4) VALUES (%s,%s,%s,%s)',              
    vSchemaName, vTableName, pcol1, pcol2, pcol3,pcol);         
EXECUTE sql ;

Upvotes: 0

Views: 385

Answers (1)

user330315
user330315

Reputation:

To avoid problems with the formatting of different data types, I would use placeholders:

sql := format('INSERT INTO %I.%I (col1,col2,col3,col4) VALUES ($1,$2,$3,$4)', vSchemaName, vTableName);         
EXECUTE sql 
   using pcol1, pcol2, pcol3, pcol;

This assumes that your variables (pcol1, ...) are declared with the correct data type.

Upvotes: 1

Related Questions