user1036645
user1036645

Reputation: 63

How to use reserved words in SQL query?

I have tried several ideas using prepared statement, but none of them worked. These are:

String insertSQL = "INSERT INTO APP.TEST (Name, 'role') VALUES (?, ?)";

String insertSQL = "INSERT INTO APP.TEST (Name, [role]) VALUES (?, ?)";

String insertSQL = "INSERT INTO APP.TEST (Name, role) VALUES (?, ?)";

Upvotes: 0

Views: 555

Answers (1)

Michael Berkowski
Michael Berkowski

Reputation: 270607

Apache Derby's documentation specifies that delimited SQL identifiers may be double-quoted. So your SQL string would eventually look like:

INSERT INTO APP.TEST (Name, "role") VALUES (?, ?)

Since that is being placed inside a string already double-quoted, backslash-escape the inner quotes:

String insertSQL = "INSERT INTO APP.TEST (Name, \"role\") VALUES (?, ?)";

Derby's list of reserved words does not currently specify role as reserved, but if in your implementation it does appear to be reserved, you must quote it accordingly.

Upvotes: 2

Related Questions