是否可以仅在 SQL 中测试空记录?

发布于 2024-10-11 19:10:54 字数 627 浏览 0 评论 0原文

我正在尝试帮助同事解决一个特殊问题,而她仅限于 MS SQL QUERY 代码。目的是如果查询没有返回记录,则插入虚拟记录(到周围的联合中)。

我在从 PL/SQL 到 MS SQL 之间来回切换时遇到了困难,我正在寻求帮助(我不是特别有吸引力,但我对 StackOverflow 观众很有吸引力)。

基本上,我们需要来自目标 Select ... 语句的单个可测试值。

理论上,它会这样做:

(other records from unions)
Union
   Select "These" as fld1, "are" as fld2, "Dummy" as fld3, "Fields" as fld4 
   where NOT (Matching Logic)
Union
   Select fld1, fld2, fld3, fld4  // Regular records exist
   From tested_table
   Where (Matching Logic)

无条件地强制单个虚拟记录是有效的。

有没有办法从 Select 中获得单一的、可测试的结果?

无法用代码实现(不允许),但可以提供 SQL

Anybody 吗?有人吗?贝勒?

I am trying to help a co-worker with a peculiar problem, and she's limited to MS SQL QUERY code only. The object is to insert a dummy record (into a surrounding union) IF no records are returned from a query.

I am having a hard time going back and forth from PL/SQL to MS SQL, and I am appealing for help (I'm not particularly appealing, but I am appealing to the StackOverflow audiance).

Basically, we need a single, testable value from the target Select ... statement.

In theory, it would do this:

(other records from unions)
Union
   Select "These" as fld1, "are" as fld2, "Dummy" as fld3, "Fields" as fld4 
   where NOT (Matching Logic)
Union
   Select fld1, fld2, fld3, fld4  // Regular records exist
   From tested_table
   Where (Matching Logic)

Forcing an individual dummy record, with no conditions, works.

IS there a way to get a single, testable result from a Select?

Can't do it in code (not allowed), but can feed SQL

Anybody? Anybody? Bbeller?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

我三岁 2024-10-18 19:10:54

您可以将 unions 放入 with 中,然后包含另一个仅在大联合为空时返回 null 的联合:

; with BigUnion as
         (
         select  *
         from    table1
         union all
         select  *
         from    table2
         )
select   *
from     BigUnion
union all
select   null
where    not exists (select * from BigUnion)

You could put the unions in a with, then include another union that returns a null only when the big union is empty:

; with BigUnion as
         (
         select  *
         from    table1
         union all
         select  *
         from    table2
         )
select   *
from     BigUnion
union all
select   null
where    not exists (select * from BigUnion)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文