Solved

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

Posted on 2016-11-02
3
21 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 45

Accepted Solution

by:
Vitor Montalvão earned 500 total points
Comment Utility
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
Comment Utility
@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
Comment Utility
Thanks Vitor, Really appreciate you help!
0

Featured Post

What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

Join & Write a Comment

Having an SQL database can be a big investment for a small company. Hardware, setup and of course, the price of software all add up to a big bill that some companies may not be able to absorb.  Luckily, there is a free version SQL Express, but does …
Slowly Changing Dimension Transformation component in data task flow is very useful for us to manage and control how data changes in SSIS.
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.
This video shows how to set up a shell script to accept a positional parameter when called, pass that to a SQL script, accept the output from the statement back and then manipulate it in the Shell.

771 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

11 Experts available now in Live!

Get 1:1 Help Now