Question

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.

Was it helpful?

Solution

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

OTHER TIPS

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.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top