문제

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?.

도움이 되었습니까?

해결책

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.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top