Search code examples
sqlteradatateradata-sql-assistant

Teradata Dynamic SQL with Bind Variables?


Is it possible to use bind variables in a Teradata dynamic SQL statement? I can't find an example anywhere. I'm looking to do something like this:

DECLARE SQLString VARCHAR(1000)
SET SQLString = 'SELECT * FROM MyTable WHERE ColumnA = :variable1';

EXECUTE IMMEDIATE SQLString; --Can I pass something here for variable1 or do I have to concatenate and not use a bind variable?

Thanks


Solution

  • DML statements with bind variables are usually executed multiple times, thus there are no bind variables when you use EXECUTE IMMEDIATE, you must use EXECUTE/PREPARE:

    DECLARE SQLstring VARCHAR(1000);
    SET SQLstring = 'update MyTable SET ColumnA = ? where columnB = ?'; -- ? identify bind variables
    PREPARE S1 FROM SQLstring;
    EXECUTE S1 USING variable1, variable2;
    

    See the Stored Procedure manual