Re: Dynamic WHERE IN statement in Cursor SQL statement -
02-22-2011
, 08:40 AM
You are missing some basic points of SQL. It is a compiled language,
not an interpreted on the fly. You can do this with dynamic SQL, but
this is a sign of bad programming. It says you don't now what you are
doing until run time AND any random user is better than you. Local
variables are also a sign of bad programming.
Next, we don't like cursors. SQL is a declarative set_oriented
language; Using SQL like a 1950's magnetic tape file system is a
sign of bad programming.
You also need to read the ISO_11179 rules for data element names. You
never put meta_data in a name, use the underscore (the dash was
COBOL).
Ignoring that this is bad SQL, a COBOL programmers writes:
IF var_b = 'abc'
THEN SET local_var_c = var_b || ',' || 'bcd';
ELSE SET local_var_c = var_b;
END IF;
But SQL programmers use expressions and not control flow:
SET local_var_c
= CASE WHEN var_b = 'abc'
THEN var_b || ',' || 'bcd
ELSE var_b END;
See the different mindset?
The SQL idiom for what you are doing is called a long parameter list.
It is just what it sounds like. Read my articles at:
http://www.simple-talk.com/sql/learn...rameter-lists/
It ws written for SQL Server but the code is portable. DB2 can handle
32K parameters, so do not worry about what you pass. |