Oracle sql 查询,将文件与 CASE 部分连接起来

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/148136/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-18 17:17:27  来源:igfitidea点击:

Oracle sql query, concatenate fileds with CASE section

sqloraclesyntaxcasetoad

提问by Zsolt Botykai

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:

我目前正在从多个表生成 SQL 插入语句,在生成的数据中我需要使用 CASE 语句,如下所示:

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.

但此时,如果我想继续我的语句,... END CASE||','|| ....我将无法再运行查询,因为 TOAD 抱怨找不到 FROM 关键字。

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

一个快速的解决方案是将输出分成字段,然后将其保存为文本并进行编辑,但必须有更好的方法。

回答by pablo

Use END instead of END CASE

使用 END 而不是 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

回答by Walter Mitty

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

您也许可以将表达式 (t.f2 > 0) 输入解码,然后将“T”转换为“1”,将“F”转换为“0”。

I haven't tried this.

我没试过这个。