T-SQL 联合的数字结果为空

发布于 2024-11-09 09:49:59 字数 369 浏览 0 评论 0原文

我正在编写一个 Transact-SQL 查询,其中涉及多个 UNION,其中第二列的数据类型是 int。第一列的数据类型是 varchar。

这是我遇到的问题的一个例子。在下面的 SQL 中,查询不会运行,因为它不允许我将最后一个 SELECT 语句的第二列留空。我在那里放置了一个空白 varchar (' '),但它需要一个 int,所以我是否被迫输入一个像 0 这样的虚拟值?

SELECT product, price
  FROM tableA    
UNION ALL    
SELECT '', SUM(price)
  FROM tableB    
UNION ALL    
SELECT '', ''
  FROM tableC

I'm writing a Transact-SQL query which involves several UNIONS where, where the datatype of the second column is an int. The datatype of the first column is a varchar.

Here's an example of the issue I'm having. In the SQL below, the query won't run because it won't let me leave the second column blank for that last SELECT statement. I put a blank varchar there (' ') but it requires an int, so am I forced to put in a dummy value like 0?

SELECT product, price
  FROM tableA    
UNION ALL    
SELECT '', SUM(price)
  FROM tableB    
UNION ALL    
SELECT '', ''
  FROM tableC

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

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

发布评论

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

评论(3

奢望 2024-11-16 09:49:59

为什么不在真正需要时选择 null 并将其转换为数字?

SELECT product, price
FROM tableA

UNION ALL

SELECT '', SUM(price)
FROM tableB

UNION ALL

SELECT 'space', NULL
FROM tableC

Why not select null and cast it to a number when really necessary?

SELECT product, price
FROM tableA

UNION ALL

SELECT '', SUM(price)
FROM tableB

UNION ALL

SELECT 'space', NULL
FROM tableC
可可 2024-11-16 09:49:59

尝试 null 怎么样:
选择“空格”,空

How about trying null :
SELECT 'space', null

慵挽 2024-11-16 09:49:59
SELECT product, price
FROM tableA
UNION ALL
SELECT '', SUM(price)
FROM tableB
UNION ALL
SELECT 'space', NULL
FROM tableC

SELECT product, price
FROM tableA
UNION ALL
SELECT '', SUM(price)
FROM tableB
UNION ALL
SELECT 'space', 0 -- These are just two hardcoded values, so why are we selecting from a table?
FROM tableC

SELECT product, price
FROM tableA
UNION ALL
SELECT '', SUM(price)
FROM tableB
UNION ALL
SELECT 'space', 0
SELECT product, price
FROM tableA
UNION ALL
SELECT '', SUM(price)
FROM tableB
UNION ALL
SELECT 'space', NULL
FROM tableC

or

SELECT product, price
FROM tableA
UNION ALL
SELECT '', SUM(price)
FROM tableB
UNION ALL
SELECT 'space', 0 -- These are just two hardcoded values, so why are we selecting from a table?
FROM tableC

or

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