Solved

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

Posted on 2016-11-02
3
41 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 46

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

[Webinar] Disaster Recovery and Cloud Management

Learn from Unigma and CloudBerry industry veterans which providers are best for certain use cases and how to lower cloud costs, how to grow your Managed Services practice in IaaS clouds, and how to utilize public cloud for Disaster Recovery

Question has a verified solution.

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

Introduction SQL Server Integration Services can read XML files, that’s known by every BI developer.  (If you didn’t, don’t worry, I’m aiming this article at newcomers as well.) But how far can you go?  When does the XML Source component become …
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
Via a live example, show how to extract information from SQL Server on Database, Connection and Server properties
Viewers will learn how to use the UPDATE and DELETE statements to change or remove existing data from their tables. Make a table: Update a specific column given a specific row using the UPDATE statement: Remove a set of values using the DELETE s…

920 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

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now