Solved

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

Posted on 2016-11-02
3
53 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
  • 2
3 Comments
 
LVL 48

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

Optimizing Cloud Backup for Low Bandwidth

With cloud storage prices going down a growing number of SMBs start to use it for backup storage. Unfortunately, business data volume rarely fits the average Internet speed. This article provides an overview of main Internet speed challenges and reveals backup best practices.

Question has a verified solution.

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

Why is this different from all of the other step by step guides?  Because I make a living as a DBA and not as a writer and I lived through this experience. Defining the name: When I talk to people they say different names on this subject stuff l…
For both online and offline retail, the cross-channel business is the most recent pattern in the B2C trade space.
This video shows, step by step, how to configure Oracle Heterogeneous Services via the Generic Gateway Agent in order to make a connection from an Oracle session and access a remote SQL Server database table.
Viewers will learn how to use the SELECT statement in SQL and will be exposed to the many uses the SELECT statement has.

856 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