Oracle sql 查询,将字段与 CASE 部分连接起来
我目前正在从多个表生成 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
但此时如果我想使用 ... END CASE 继续我的语句||','|| ....
我无法再运行查询,因为 TOAD 抱怨找不到 FROM 关键字。
一个快速的解决方案是将输出分成字段,然后将其保存为文本并进行编辑,但必须有更好的方法。
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
使用 END 代替 END CASE
Use END instead of END CASE
对于一些类似的情况,“解码”功能效果很好。
您也许可以将表达式 (t.f2 > 0) 输入解码,然后进行翻译
“T”变为“1”,“F”变为“0”。
我没试过这个。
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.