Oracle sql 查询,将字段与 CASE 部分连接起来

发布于 2024-07-06 12:18:18 字数 432 浏览 7 评论 0原文

我目前正在从多个表生成 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

玩物 2024-07-13 12:18:18

使用 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

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
柒七 2024-07-13 12:18:18

对于一些类似的情况,“解码”功能效果很好。

您也许可以将表达式 (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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文