SQL stored procedure throwing invalid in the select list error
NickName:Rick Ask DateTime:2011-12-08T21:29:30

SQL stored procedure throwing invalid in the select list error

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.

Copyright Notice:Content Author:「Rick」,Reproduced under the CC 4.0 BY-SA copyright license with a link to the original source and this disclaimer.
Link to original article:https://stackoverflow.com/questions/8431727/sql-stored-procedure-throwing-invalid-in-the-select-list-error

Answers
Yuck 2011-12-08T13:33:44

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:\n\nSELECT ColumnA, SUM(ColumnB) TotalCount\nFROM Table\nGROUP BY ColumnA;\n\n\nBut this would not:\n\nSELECT ColumnA, ColumnC, ColumnD, SUM(ColumnB) TotalCount\nFROM Table\nGROUP BY ColumnA;\n\n\nBecause ColumnC and ColumnD aren't being grouped.",


Martin Smith 2011-12-08T13:32:26

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.\n\nSo 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 ",


ypercubeᵀᴹ 2011-12-08T13:35:13

Just a note, not related to your question. This part:\n\n , SUM(CASE\n WHEN u.prop_id IS NOT NULL THEN 1\n ELSE 0\n END) AS UnitCount\n\n\ncan be simplified to:\n\n , COUNT(u.prop_id) AS UnitCount\n",


More about “SQL stored procedure throwing invalid in the select list error” related questions

SQL stored procedure throwing invalid in the select list error

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.

Show Detail

SQL Stored Procedure Throwing Error

I'm trying to run the follow in CREATE a stored procedure in mySQL, but getting errors: "You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version f...

Show Detail

Select a column returned by a stored procedure

I have a stored procedure which is returning me about 50 columns. I want to write a query, where I will be able to select a particular column from the list of column returned by the SP. I tried wr...

Show Detail

Invalid Column name error during execution of stored procedure

I am not able to execute the stored procedure. It is throwing an error Invalid Column name 'DW201401' Command used to execute the stored procedure: exec RM_UTIL_MODE server,'DW201401' Stored

Show Detail

Error during stored procedure select but no error in SQL select

From the title, when I run my stored procedure, an error shows. But when I manually run the select statement that produces the error, there are no problems in SQL. This part of the stored procedure

Show Detail

Invalid length parameter error for stored procedure

I'm passing a list of names from SSRS to a SQL Server stored procedure but I'm getting an error: Invalid length parameter passed to the LEFT or SUBSTRING function This is my code: Select subst...

Show Detail

Stored procedure weird error in SQL Server

In SQL Server 2016, a stored procedure is throwing an error when it's executed from my .Net application. And when we execute the same stored procedure in the database with the same input parameters,

Show Detail

SQL Error - Invalid Object Name Stored Procedure

I implement and support a software applicatoin that allows me to create macros within the application. One of the macro steps is to execute a SQL procedure. When I attempt to run a macro that is

Show Detail

Calling Stored procedure with cross apply from another stored procedure gives error SQL server

I have been trying to call stored procedure from another stored procedure. Now issue is that under lying nested stored procedure contains CROSS APPLY with temp table and it runs fine when i execute...

Show Detail

I am not able to execute the stored procedure. It is throwing an error

I am not able to execute the stored procedure. It is throwing an error Invalid column name ' UNION ALL '. Any one can help USE [M511Symaster] GO /****** Object: StoredProcedure [dbo].[

Show Detail