DB2 DYNAMIC SQL

SELECT CUSTNAM, CUSTCTY, CUSTST INTO :Name, :City, :State
FROM CUSTMAST WHERE Custno = :Cust

EXECUTE IMMEDIATE:

PREPARE and EXECUTE

Examples

SqlString= 'DELETE FROM EMPLOYEE WHERE EMPNAME = ''' + %Trim(EmployeeName)+'''';EXEC SQL PREPARE SqlSTMT FROM :SqlString;EXEC SQL EXECUTE SqlSTMT ;
SqlString= 'DELETE FROM EMPLOYEE WHERE EMPNAME = ? and EMPID = ? ; 
// "?" represents parameter marker

EXEC SQL PREPARE SqlSTMT FROM :SqlString;

EXEC SQL EXECUTE SqlSTMT using :EmployeeName , :EmployeeId
// Each host variable will replace respective "?"
SqlString= 'SELECT * FROM EMPLOYEE WHERE EMPNAME = ''' + %Trim(EmployeeName)+'''';EXEC SQL PREPARE SqlSTMT FROM :SqlString;EXEC SQL DECLARE @C1 cursor for SqlSTMT ;
// Now process @C1 cursor as normal cursor
SqlString= 'SELECT * FROM EMPLOYEE WHERE EMPNAME = ? and EMPID = ? ; 
// "?" represents parameter marker

EXEC SQL PREPARE SqlSTMT FROM :SqlString;

EXEC SQL DECLARE @C1 cursor for SqlSTMT;

EXEC SQL OPEN @C1 using :EmployeeName , :EmployeeId ;
// Each host variable will replace respective "?"

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store