views:

3462

answers:

2

Hi,

I'm currently generating SQL insert statements from more than one tables, and in the generated data I need to use a CASE statement, like this:

select 'INSERT INTO TABLE1 (f1, f2, f3, f4 ...) values ('
       ||t.f1||','
       ||CASE
             WHEN t.f2 > 0 THEN '1'
             ELSE '0'
         END CASE
  from table2 t , table3 t3

But at this point if I want to continue my statement with ... END CASE||','|| .... I can't run the query anymore, as TOAD complains about not finding the FROM keyword.

A quick solution was to separate the ouput into fields, then save it to text, and edit, but there must be a better way....

Thanks in advance!

+1  A: 

For some similar situations, the "decode" function works quite well.

You might be able to feed the expression (t.f2 > 0) into a decode, and then translate 'T' into '1' and 'F' into '0'.

I haven't tried this.

Walter Mitty
Decode is on its way out, so I wouldn't recommend using it.
Matthew Watson
+3  A: 

Use END instead of END CASE

select 'INSERT INTO TABLE1 (f1, f2, f3, f4 ...) values ('
       ||t.f1||','
       ||CASE
             WHEN t.f2 > 0 THEN '1'
             ELSE '0'
         END||','||t.f2
  from table2 t , table3 t3
pablo
Thank you it works for me!
Zsolt Botykai