我想在动态查询中转换一个简单的查询,直接传递一些变量。查询可能是这样的:
SELECT * FROM mySchema.THETABLE a
WHERE a.THECOLUMN = 'someVariable';
所以,我尝试了这样的事情:
DECLARE v_schema varchar(7);
DECLARE v_param1 varchar(200);
DECLARE v_sqlstr varchar(2000);
SET v_schema = 'mySchema';
SET v_param1 = 'someVariable';
SET v_sqlstr = 'SELECT * FROM '
|| v_schema
|| '.THETABLE a WHERE a.THECOLUMN = '''
|| v_param1
|| ''';
execute sql v_sqlstr;
如何才能做到这一点?当我在 dbvisualizer 命令窗口中尝试这种类型的代码时,我得到了错误:
[DECLARE - 0 row(s), 0.000 secs]
[Error Code: -199, SQL State: 42601]
DB2 SQL Error: SQLCODE=-199, SQLSTATE=42601, SQLERRMC=VARCHAR;
TABLE STATEMENT , . SCROLL INSENSITIVE SENSITIVE ASENSITIVE NO, DRIVER=4.15.82