SQL存储过程在选择列表中抛出无效错误

发布于 2024-12-20 08:21:06 字数 652 浏览 2 评论 0原文

我正在从 mysql 转换为 sql server。

转换选择存储过程时,

CREATE PROCEDURE selPropertyByAcntID
(
@in_acntID INT
)
AS
SELECT *
     , SUM(CASE
                        WHEN u.prop_id IS NOT NULL THEN 1
                        ELSE 0
                 END) AS UnitCount
FROM 
    prop_details d
INNER JOIN 
    acnt_property a
ON 
    d.prop_id = a.prop_id
LEFT JOIN 
    unit_details u
ON 
    d.prop_id = u.prop_id
WHERE 
    a.group_id = @in_acntID
GROUP BY 
    d.prop_id;

抛出以下错误:

错误:8120 严重性 16 列“prop_details.prop_title”在选择列表中无效,因为它不是聚合函数或 GROUP BY 子句中的容器。

奇怪——完全相同的存储过程在 mysql 环境中工作。

非常感谢对此的任何帮助。

I am converting from mysql to sql server.

While converting a select stored proc,

CREATE PROCEDURE selPropertyByAcntID
(
@in_acntID INT
)
AS
SELECT *
     , SUM(CASE
                        WHEN u.prop_id IS NOT NULL THEN 1
                        ELSE 0
                 END) AS UnitCount
FROM 
    prop_details d
INNER JOIN 
    acnt_property a
ON 
    d.prop_id = a.prop_id
LEFT JOIN 
    unit_details u
ON 
    d.prop_id = u.prop_id
WHERE 
    a.group_id = @in_acntID
GROUP BY 
    d.prop_id;

It is throwing the following error:

Error: 8120 Severity 16
Column 'prop_details.prop_title' is invalid in the select list because it is not container in either an aggregate function or the GROUP BY clause.

strange -- the exact same stored proc isworking in a mysql environment.

Any help on this is much appreciated.

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

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

发布评论

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

评论(4

温折酒 2024-12-27 08:21:06

在 SQL Server 和几乎所有 RDBMS(除了 MySQL)中,您只能在 SELECT 列表中包含 GROUP BY 列表中包含的项目或那些包裹在一个聚合体中。

因此,要解决此问题,请删除 *,只需从表 da 中选择所需的列,并将此列表也添加到 <代码>分组依据

In SQL Server and just about every RDBMS except MySQL you can only include in the SELECT list items that are included in the GROUP BY list or those wrapped in an aggregate.

So to resolve the issue get rid of *, select only the columns you need from tables d and a and add this list also to the GROUP BY

听不够的曲调 2024-12-27 08:21:06

只是一个注释,与你的问题无关。这部分:

 , SUM(CASE
              WHEN u.prop_id IS NOT NULL THEN 1
              ELSE 0
       END) AS UnitCount

可以简化为:

 , COUNT(u.prop_id) AS UnitCount

Just a note, not related to your question. This part:

 , SUM(CASE
              WHEN u.prop_id IS NOT NULL THEN 1
              ELSE 0
       END) AS UnitCount

can be simplified to:

 , COUNT(u.prop_id) AS UnitCount
入怼 2024-12-27 08:21:06

对于 SQL Server(我认为事实上大多数 RDBMS...),带有 GROUP BY 的语句只能包含分组列和聚合函数。例如,这可以工作:

SELECT ColumnA, SUM(ColumnB) TotalCount
FROM Table
GROUP BY ColumnA;

但这不行:

SELECT ColumnA, ColumnC, ColumnD, SUM(ColumnB) TotalCount
FROM Table
GROUP BY ColumnA;

因为 ColumnCColumnD 没有被分组。

With SQL Server (I thought most RDBMS in fact...) statements with GROUP BY can only include the grouping columns along with aggregating functions. For example, this would work:

SELECT ColumnA, SUM(ColumnB) TotalCount
FROM Table
GROUP BY ColumnA;

But this would not:

SELECT ColumnA, ColumnC, ColumnD, SUM(ColumnB) TotalCount
FROM Table
GROUP BY ColumnA;

Because ColumnC and ColumnD aren't being grouped.

随遇而安 2024-12-27 08:21:06

在您选择的内容中,您有 *,但仅按 prop_id 分组。如果你将 * 更改为 d.prop_id 你应该没问题。如果您需要任何其他列,请在选择和分组依据中指定它们。

In you select you have *, but only grouping by the prop_id. If you change you * to just be d.prop_id you should be good. If you need any other columns specify them in the select and the group by.

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