Sample Header Ad - 728x90

How to define variable in dynamic SQL in IBM Db2?

0 votes
1 answer
601 views
In Db2 v11.5 on Linux I would like to create variable and then use this variable in SELECT statement. I don't want to create procedure or function on database, I just want to do this in dynamic SQL from db2cmd or some database tool. Simplified sample:
BEGIN
     DECLARE name VARCHAR(10);
     SET name = 'John';
     SELECT * FROM SYSIBM.SYSDUMMY1 WHERE name = :name;
END;
--- **EDIT:** Why I don't want to create stored procedure/function? I perform audit every half a year for database privileges/authorizations on several databases and **I don't have create procedure privilege on some of the databases,** because of strict security settings. In most cases I only have access to system catalog tables which is perfectly enough I can create audit. I list database users and then for "suspicious" users I perform full permission/authority checks executing several SQL-s from system catalog. Up till now for every user I have used text editor and used find/replace, which is OK solution, but variable is better solution. Thanks!
Asked by folow (523 rep)
Dec 5, 2024, 09:54 AM
Last activity: Dec 5, 2024, 01:15 PM