Frage

I want to generate .sql file with the sql query output . I am doing this with concat statement in sql . I am using case statement in some queries this will be the problem for me.

select concat('insert into x values(',CASE a when B then 'Book' else 'NONE' end , ') on duplicate key update B = values(B)') from author;

select 'insert into x values('+CASE a when B then 'Book' else 'NONE' end +') on duplicate key update B = values(B)' from author;

It also not works because in mysql + used for adding only numbers not for strings .

Is there any way for doing this?.

War es hilfreich?

Lösung

The problem with the first version is the quotes of things within the string. For instance, you want your string to contain "'Book'"

select concat('insert into x values(',
               (CASE a when 'B' then '''Book''' else '''NONE''' end) ,
              ') on duplicate key update B = values(''B'')'
             )
from author;

I think this quotes al the strings as they should be. I'm guess column A is a character that should be compared to 'B' and not to column B.

Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top