Search code examples
sqlcdb2embedded-sql

DB2 embedded SQL in C use same host variable for both input and output allowed?


Is it safe to use same host variable for both input and output in an embedded SQL query ? I'm using C and DB2 static embedded SQL.

Example:

EXEC SQL 
     SELECT someCol 
     INTO :someHostVar 
     FROM SomeTable 
     WHERE :someHostVar = someOtherCol;

Solution

  • Yes, you can do that. The value of someHostVar will be overwirtten and contain whatever the value of someCol is for this particular predicate - unless the value of someCol happens to be NULL at which point it the host variable remains unchanged.

    Even though you can do this, I would suggest to you that this is not a good practice because someHostVar may end up containing values for different columns of the same table - too easy to screw up.