Solved

How do I fix SQL Error Msg 8120, Level 16, State 1?

Posted on 2016-11-02
3
83 Views
Last Modified: 2016-11-02
Ok So I have been playing with stored procedures for a couple weeks. I have had great success with these. Until today.  I created a stored procedure with SSMS 2016 using this code.
IF ( OBJECT_ID('dbo.sp_frmCPS_GetCurECOStatus') IS NOT NULL )
DROP PROCEDURE dbo.sp_frmCPS_GetCurECOStatus
GO
CREATE PROCEDURE dbo.sp_frmCPS_GetCurECOStatus
@Partnum		VARCHAR(MAX)		=NULL
AS
BEGIN
	SET NOCOUNT ON
	SELECT MAX ([ECOno]), [Status]
	FROM  [dbo].[tblSqlECO]
	WHERE [PartNo]= @Partnum
	GROUP BY [ECOno]
	ORDER BY [ECOno] DESC
END

Open in new window

When I run it i get this error:

Msg 8120, Level 16, State 1, Procedure sp_frmCPS_GetCurECOStatus, Line 6 [Batch Start Line 8]
Column 'dbo.tblSqlECO.Status' is invalid in the select list because it is not contained in either an aggregate function or the GROUP BY clause.


What I'm trying to achieve is the get the last ECO Number from the table and be able to return  the "Status" column Value of that row for some Visual Basic.Net code I'm working on.
0
Comment
Question by:Steve Williams
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
3 Comments
 
LVL 51

Accepted Solution

by:
Vitor Montalvão earned 500 total points
ID: 41870205
You're grouping by [ECOno] but you should group by Status.
0
 
LVL 1

Assisted Solution

by:Steve Williams
Steve Williams earned 0 total points
ID: 41870218
@Vitor
Thanks, that got me past that error but then I got this error:

Msg 8127, Level 16, State 1, Procedure sp_frmCPS_GetCurECOStatus, Line 10 [Batch Start Line 8]
Column "dbo.tblSqlECO.ECOno" is invalid in the ORDER BY clause because it is not contained in either an aggregate function or the GROUP BY clause.


This is the final code that works I had the syntax wrong for the ORDER BY also, it should have had the MAX() encapsulating the [ECOno] column.

IF ( OBJECT_ID('dbo.sp_frmCPS_GetCurECOStatus') IS NOT NULL )
DROP PROCEDURE dbo.sp_frmCPS_GetCurECOStatus
GO
CREATE PROCEDURE dbo.sp_frmCPS_GetCurECOStatus
@Partnum		VARCHAR(MAX)		=NULL
AS
BEGIN
	SET NOCOUNT ON
	SELECT MAX ([ECOno]), [Status]
	FROM  [dbo].[tblSqlECO]
	WHERE [PartNo]= @Partnum
	GROUP BY [Status]
	ORDER BY MAX([ECOno]) DESC
END

Open in new window

0
 
LVL 1

Author Comment

by:Steve Williams
ID: 41870225
Thanks Vitor, Really appreciate you help!
0

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

For both online and offline retail, the cross-channel business is the most recent pattern in the B2C trade space.
In this article we will learn how to fix  “Cannot install SQL Server 2014 Service Pack 2: Unable to install windows installer msi file” error ?
Viewers will learn how the fundamental information of how to create a table.
Viewers will learn how to use the SELECT statement in SQL to return specific rows and columns, with various degrees of sorting and limits in place.

628 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question