是否可以仅在 SQL 中测试空记录?
我正在尝试帮助同事解决一个特殊问题,而她仅限于 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可以将
unions
放入with
中,然后包含另一个仅在大联合为空时返回null
的联合:You could put the
unions
in awith
, then include another union that returns anull
only when the big union is empty: