Can anyone give any advice on using variable within DECODE?
If I use:
SUM(DECODE(saq2a, 1, 1, 0)) "1"
...with saq2a being the column name, it works. If I use:
v_use := 'saq2a';
SUM(DECODE(v_use, 1, 1, 0)) "1"
...it doesn't work because it gives a comparison error. If I then wrap the 1 in quotes ('1'), it works, but gives the wrong answer because it compares it to the word saq2a as opposed to the column name (and the value in the table).
This is really getting on my nerves. Does anyone know how I can use a variable for the column name.
(Just for background info, I have a cursor that uses these commands. It would save me thousands of lines of code if I can use a variable for the column name, and change the variable before I call the curosr, hence reusing it.)
I asked this in the SQL forum, and they suggested I try here, as DBMS_SQL was probably my best option. But how do I use that?!
Cheers, ben
If I use:
SUM(DECODE(saq2a, 1, 1, 0)) "1"
...with saq2a being the column name, it works. If I use:
v_use := 'saq2a';
SUM(DECODE(v_use, 1, 1, 0)) "1"
...it doesn't work because it gives a comparison error. If I then wrap the 1 in quotes ('1'), it works, but gives the wrong answer because it compares it to the word saq2a as opposed to the column name (and the value in the table).
This is really getting on my nerves. Does anyone know how I can use a variable for the column name.
(Just for background info, I have a cursor that uses these commands. It would save me thousands of lines of code if I can use a variable for the column name, and change the variable before I call the curosr, hence reusing it.)
I asked this in the SQL forum, and they suggested I try here, as DBMS_SQL was probably my best option. But how do I use that?!
Cheers, ben