devxlogo

Programming techniques: EXEC SQL and bind variables

Programming techniques: EXEC SQL and bind variables

Question:
I am having a problem with EXEC SQL. When I use the following code:

DIM var1 AS Integer
DIM var2 AS Integer
DIM var3 AS Integer
DIM sSQL AS String
sSQL = “SELECT 1, 2, 3 from :var1, :var2, :var3 from systable.dual”
EXEC SQL :sSQL

it does not work. If I use the string directly after the EXEC SQL, everything is fine. What gives?

Answer:
There isn’t any problem with EXEC SQL. It can take a string variable, but when it does, it does no further parsing on the content of the string. This means that it interprets the ‘:var1’ references as data rather than bind variable indicators.

It’s easy to handle this situation once you are aware of it. All you have to do is to change the code so that you build a literal string in your variable, like this:

sSQL = “SELECT 1, 2, 3 from ” & var1 & “, ” & var2 & “, ” & var3 & ” from systable.dual”
EXEC SQL :sSQL

This should work fine.

devxblackblue

About Our Editorial Process

At DevX, we’re dedicated to tech entrepreneurship. Our team closely follows industry shifts, new products, AI breakthroughs, technology trends, and funding announcements. Articles undergo thorough editing to ensure accuracy and clarity, reflecting DevX’s style and supporting entrepreneurs in the tech sphere.

See our full editorial policy.

About Our Journalist