Solved

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

Posted on 2016-11-02
3
63 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 50

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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
VB Catch Statement in Event Viewer. 2 20
mssql 7 32
VM SQL server license. 1 65
Automatically add row to table by job in SqlServer 2012 3 14
Slowly Changing Dimension Transformation component in data task flow is very useful for us to manage and control how data changes in SSIS.
A Stored Procedure in Microsoft SQL Server is a powerful feature that it can be used to execute the Data Manipulation Language (DML) or Data Definition Language (DDL). Depending on business requirements, a single Stored Procedure can return differe…
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.
Using examples as well as descriptions, and references to Books Online, show the documentation available for datatypes, explain the available data types and show how data can be passed into and out of variables.

734 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