Microsoft
Software
Hardware
Network
Question : How to resolve the SQL error: "ORDER BY items must appear in the select list if the statement contains a UNION, INTERSECT or EXCEPT operator"?
Do you know how I can resolve the following error in the stored procedure that follows:
I created an Access application with Access as the front end and SQL Server 2005 as the back end database.
Msg 104, Level 16, State 1, Procedure procSpLN, Line 17
ORDER BY items must appear in the select list if the statement contains a UNION, INTERSECT or EXCEPT operator.
Msg 104, Level 16, State 1, Procedure procSpLN, Line 18
ORDER BY items must appear in the select list if the statement contains a UNION, INTERSECT or EXCEPT operator.
--------------------------
----------
---
set ANSI_NULLS ON
set QUOTED_IDENTIFIER ON
go
ALTER PROCEDURE [dbo].[procSpLN]
@RptYear int
AS
If Exists(SELECT * FROM dbo.SYSOBJECTS WHERE NAME = 'tblSpLN' AND TYPE = 'U')
DROP TABLE dbo.tblSpLN
Declare @FallCycle Bit
Declare @MutualFS int
Declare @BondsFS int
Declare @CashFS int
Declare @StocksFS int
Declare @IRAFS int
-- Assign the DE Values to variables
SELECT @FallCycle = FallCycle, @MutualFS = MutualFS, @BondsFS = BondsFS, @CashFS = CashFS, @StocksFS = StocksFS, @IRAFS = IRAFS
FROM dbo.tblStatesAll
WHERE StateFS = 'DE'
SELECT DISTINCT
CASE WHEN C.TaxIDInd = '1' THEN ' ' ELSE UPPER(C.FirstName) END AS [1st First Name],
UPPER (C.MiddleInitial) AS [1st Mid],
CASE WHEN C.TaxIDInd = '1' THEN C.FirstName ELSE UPPER(C.LastName) END AS [1st Last Name],
UPPER(C.SecondNameFirst) As [2nd First Name], UPPER(C.SecondNameMid) AS [2nd Mid], UPPER(C.SecondNameLast) AS [2nd Last Name],
UPPER(C.StreetAddr1) AS [Address 1], UPPER (C.StreetAddr2) AS [Address 2],
UPPER(C.City) AS City,
UPPER(C.ResStateCode) AS [State], C.Zip AS Zip, CASE WHEN LEN(C.SSN) = 0 THEN C.TaxID ELSE C.SSN END AS [SSN/Tax ID],
Right(C.OfficeNumber,3) + ' ' + C.CustomerNumber AS [Account Number], C.AcctExec AS [FA Number], C.DateOfBirth AS [Date Of Birth]
INTO dbo.tblSpLN
FROM dbo.tblStatesAll AS S INNER JOIN dbo.tblCustomersNew AS C ON S.StateFS = C.ResStateCode
INNER JOIN dbo.tblProductsNew AS P ON C.CustomerNumber = P.CustomerNumber AND C.OfficeNumber = P.OfficeNumber
WHERE (S.FallCycle= 0) AND S.StateFS <> 'NY' AND C.RedFlag = 'N' AND LEN(P.InsuranceContract) = 0 AND
((P .PropertyType='MUTUAL FUND' AND C.DateLost <= CAST((@RptYear - S.MutualFS) AS VARCHAR) + '-12-31' AND LEN(P.IRACode) = 0 AND (P.Quantity > 0 OR P.CashBalance > 0)) OR
(P .PropertyType='MUTUAL FUND' AND C.DateLost <= CAST((@RptYear - S.MutualFS) AS VARCHAR) + '-12-31' AND LEN(P.IRACode) = 0 AND (P.Quantity = 0 AND P.CashBalance = 0)
AND ((LEN(PlanNumber) = 0) OR (PlanNumber = 'NETWORKED') OR (ISNUMERIC (PLANNUMBER) = 1))) OR
-- Other is Included for Lexis Nexis as well as Insurance Contracts
(P .PropertyType='CASH' AND C.DateLost <= CAST((@RptYear - S.CashFS) AS VARCHAR) + '-12-31' AND LEN(P.IRACode) = 0 AND (P.Quantity > 0 OR P.CashBalance > 0)) OR
(P .PropertyType='EQUITY' AND C.DateLost <= CAST((@RptYear - S.StocksFS) AS VARCHAR) + '-12-31' AND LEN(P.IRACode) = 0 AND (P.Quantity > 0 OR P.CashBalance > 0)) OR
(P .PropertyType='FIXED INCOME' AND C.DateLost <= CAST((@RptYear - S.BondsFS) AS VARCHAR) + '-12-31' AND LEN(P.IRACode) = 0 AND (P.Quantity > 0 OR P.CashBalance > 0)) OR
--IRA WITH VALID DATE OF BIRTH
(P .PropertyType='MUTUAL FUND' AND C.DateLost <= CAST((@RptYear - S.IRAFS) AS VARCHAR) + '-12-31' AND LEN(P.IRACode) > 0 AND (P.Quantity > 0 OR P.CashBalance > 0) AND
(C.DateOfBirth <= CAST(((@RptYear - S.IRAFS)-71) AS VARCHAR) + '-06-30')) OR
(P .PropertyType='MUTUAL FUND' AND C.DateLost <= CAST((@RptYear - S.IRAFS) AS VARCHAR) + '-12-31' AND LEN(P.IRACode) > 0 AND (P.Quantity = 0 AND P.CashBalance = 0)
AND ((LEN(PlanNumber) = 0) OR (PlanNumber = 'NETWORKED') OR (ISNUMERIC (PLANNUMBER) = 1)) AND
(C.DateOfBirth <= CAST(((@RptYear - S.IRAFS)-71) AS VARCHAR) + '-06-30')) OR
(P .PropertyType='CASH' AND C.DateLost <= CAST((@RptYear - S.IRAFS) AS VARCHAR) + '-12-31' AND LEN(P.IRACode) > 0 AND (P.Quantity > 0 OR P.CashBalance > 0) AND
(C.DateOfBirth <= CAST(((@RptYear - S.IRAFS)-71) AS VARCHAR) + '-06-30')) OR
(P .PropertyType='EQUITY' AND C.DateLost <= CAST((@RptYear - S.IRAFS) AS VARCHAR) + '-12-31' AND LEN(P.IRACode) > 0 AND (P.Quantity > 0 OR P.CashBalance > 0) AND
(C.DateOfBirth <= CAST(((@RptYear - S.IRAFS)-71) AS VARCHAR) + '-06-30')) OR
(P .PropertyType='FIXED INCOME' AND C.DateLost <= CAST((@RptYear - S.IRAFS) AS VARCHAR) + '-12-31' AND LEN(P.IRACode) > 0 AND (P.Quantity > 0 OR P.CashBalance > 0) AND
(C.DateOfBirth <= CAST(((@RptYear - S.IRAFS)-71) AS VARCHAR) + '-06-30')))
UNION ALL
SELECT DISTINCT
CASE WHEN C.TaxIDInd = '1' THEN ' ' ELSE UPPER(C.FirstName) END AS [1st First Name],
UPPER (C.MiddleInitial) AS [1st Mid],
CASE WHEN C.TaxIDInd = '1' THEN C.FirstName ELSE UPPER(C.LastName) END AS [1st Last Name],
UPPER(C.SecondNameFirst) As [2nd First Name], UPPER(C.SecondNameMid) AS [2nd Mid], UPPER(C.SecondNameLast) AS [2nd Last Name],
UPPER(C.StreetAddr1) AS [Address 1], UPPER (C.StreetAddr2) AS [Address 2],
UPPER(C.City) AS City,
UPPER(C.ResStateCode) AS [State], C.Zip AS Zip, CASE WHEN LEN(C.SSN) = 0 THEN C.TaxID ELSE C.SSN END AS [SSN/Tax ID],
Right(C.OfficeNumber,3) + ' ' + C.CustomerNumber AS [Account Number], C.AcctExec AS [FA Number], C.DateOfBirth AS [Date Of Birth]
FROM dbo.tblStatesAll AS S INNER JOIN dbo.tblCustomersNew AS C ON S.StateFS = C.ResStateCode
INNER JOIN dbo.tblProductsNew AS P ON C.CustomerNumber = P.CustomerNumber AND C.OfficeNumber = P.OfficeNumber
WHERE (S.FallCycle= 0) AND S.StateFS = 'NY' AND C.RedFlag = 'N' AND LEN(P.InsuranceContract) = 0 AND
((P .PropertyType='MUTUAL FUND' AND C.DateLost <= CAST((@RptYear - S.MutualFS) AS VARCHAR) + '-12-31' AND LEN(P.IRACode) = 0 AND (P.Quantity > 0 OR P.CashBalance > 0)) OR
(P .PropertyType='MUTUAL FUND' AND C.DateLost <= CAST((@RptYear - S.MutualFS) AS VARCHAR) + '-12-31' AND LEN(P.IRACode) = 0 AND (P.Quantity = 0 AND P.CashBalance = 0)
AND ((LEN(PlanNumber) = 0) OR (PlanNumber = 'NETWORKED') OR (ISNUMERIC (PLANNUMBER) = 1))) OR
-- Other is Included for Lexis Nexis as well as Insurance Contracts
(P .PropertyType='CASH' AND C.DateLost <= CAST((@RptYear - S.CashFS) AS VARCHAR) + '-12-31' AND LEN(P.IRACode) = 0 AND (P.Quantity > 0 OR P.CashBalance > 0)) OR
(P .PropertyType='EQUITY' AND C.DateLost <= CAST((@RptYear - S.StocksFS) AS VARCHAR) + '-12-31' AND LEN(P.IRACode) = 0 AND (P.Quantity > 0 OR P.CashBalance > 0)) OR
(P .PropertyType='FIXED INCOME' AND C.DateLost <= CAST((@RptYear - S.BondsFS) AS VARCHAR) + '-12-31' AND LEN(P.IRACode) = 0 AND (P.Quantity > 0 OR P.CashBalance > 0)) OR
--IRA WITH VALID DATE OF BIRTH
(P .PropertyType='MUTUAL FUND' AND C.DateLost <= CAST((@RptYear - 3) AS VARCHAR) + '-12-31' AND LEN(P.IRACode) > 0 AND (P.Quantity > 0 OR P.CashBalance > 0) AND
(C.DateOfBirth <= CAST(((@RptYear - S.IRAFS)-71) AS VARCHAR) + '-06-30')) OR
(P .PropertyType='MUTUAL FUND' AND C.DateLost <= CAST((@RptYear - 3) AS VARCHAR) + '-12-31' AND LEN(P.IRACode) > 0 AND (P.Quantity = 0 AND P.CashBalance = 0)
AND ((LEN(PlanNumber) = 0) OR (PlanNumber = 'NETWORKED') OR (ISNUMERIC (PLANNUMBER) = 1)) AND
(C.DateOfBirth <= CAST(((@RptYear - S.IRAFS)-71) AS VARCHAR) + '-06-30')) OR
(P .PropertyType='CASH' AND C.DateLost <= CAST((@RptYear - 3) AS VARCHAR) + '-12-31' AND LEN(P.IRACode) > 0 AND (P.Quantity > 0 OR P.CashBalance > 0) AND
(C.DateOfBirth <= CAST(((@RptYear - S.IRAFS)-71) AS VARCHAR) + '-06-30')) OR
(P .PropertyType='EQUITY' AND C.DateLost <= CAST((@RptYear - 3) AS VARCHAR) + '-12-31' AND LEN(P.IRACode) > 0 AND (P.Quantity > 0 OR P.CashBalance > 0) AND
(C.DateOfBirth <= CAST(((@RptYear - S.IRAFS)-71) AS VARCHAR) + '-06-30')) OR
(P .PropertyType='FIXED INCOME' AND C.DateLost <= CAST((@RptYear - 3) AS VARCHAR) + '-12-31' AND LEN(P.IRACode) > 0 AND (P.Quantity > 0 OR P.CashBalance > 0) AND
(C.DateOfBirth <= CAST(((@RptYear - S.IRAFS)-71) AS VARCHAR) + '-06-30')))
UNION ALL
SELECT DISTINCT
CASE WHEN C.TaxIDInd = '1' THEN ' ' ELSE UPPER(C.FirstName) END AS [1st First Name],
UPPER (C.MiddleInitial) AS [1st Mid],
--SELECT DISTINCT UPPER (C.FirstName) AS [1st First Name], UPPER (C.MiddleInitial) AS [1st Mid],
CASE WHEN C.TaxIDInd = '1' THEN C.FirstName ELSE UPPER(C.LastName) END AS [1st Last Name],
UPPER(C.SecondNameFirst) As [2nd First Name], UPPER(C.SecondNameMid) AS [2nd Mid], UPPER(C.SecondNameLast) AS [2nd Last Name],
UPPER(C.StreetAddr1) AS [Address 1], UPPER (C.StreetAddr2) AS [Address 2],
UPPER(C.City) AS City,
'DE' AS [State], C.Zip AS Zip, CASE WHEN LEN(C.SSN) = 0 THEN C.TaxID ELSE C.SSN END AS [SSN/Tax ID],
Right(C.OfficeNumber,3) + ' ' + C.CustomerNumber AS [Account Number], C.AcctExec AS [FA Number], C.DateOfBirth AS [Date Of Birth]
FROM dbo.tblCustomersNew AS C
LEFT JOIN dbo.tblStatesAll AS S ON S.StateFS = C.ResStateCode -- Use LEFT JOIN and check for NULL in StateFS for non-matching values.
INNER JOIN dbo.tblProductsNew AS P ON C.CustomerNumber = P.CustomerNumber AND C.OfficeNumber = P.OfficeNumber
WHERE (@FallCycle= 0) AND S.StateFS IS NULL AND C.RedFlag = 'N' AND LEN(P.InsuranceContract) = 0 AND
((P .PropertyType='MUTUAL FUND' AND C.DateLost <= CAST((@RptYear - @MutualFS) AS VARCHAR) + '-12-31' AND LEN(P.IRACode) = 0 AND (P.Quantity > 0 OR P.CashBalance > 0)) OR
(P .PropertyType='MUTUAL FUND' AND C.DateLost <= CAST((@RptYear - @MutualFS) AS VARCHAR) + '-12-31' AND LEN(P.IRACode) = 0 AND (P.Quantity = 0 AND P.CashBalance = 0)
AND ((LEN(PlanNumber) = 0) OR (PlanNumber = 'NETWORKED') OR (ISNUMERIC (PLANNUMBER) = 1))) OR
-- Other is Included for Lexis Nexis as well as Insurance Contracts
(P .PropertyType='CASH' AND C.DateLost <= CAST((@RptYear - @CashFS) AS VARCHAR) + '-12-31' AND LEN(P.IRACode) = 0 AND (P.Quantity > 0 OR P.CashBalance > 0)) OR
(P .PropertyType='EQUITY' AND C.DateLost <= CAST((@RptYear - @StocksFS) AS VARCHAR) + '-12-31' AND LEN(P.IRACode) = 0 AND (P.Quantity > 0 OR P.CashBalance > 0)) OR
(P .PropertyType='FIXED INCOME' AND C.DateLost <= CAST((@RptYear - @BondsFS) AS VARCHAR) + '-12-31' AND LEN(P.IRACode) = 0 AND (P.Quantity > 0 OR P.CashBalance > 0)) OR
--IRA WITH VALID DATE OF BIRTH
(P .PropertyType='MUTUAL FUND' AND C.DateLost <= CAST((@RptYear - @IRAFS) AS VARCHAR) + '-12-31' AND LEN(P.IRACode) > 0 AND (P.Quantity > 0 OR P.CashBalance > 0) AND
(C.DateOfBirth <= CAST(((@RptYear - @IRAFS)-71) AS VARCHAR) + '-06-30')) OR
(P .PropertyType='MUTUAL FUND' AND C.DateLost <= CAST((@RptYear - @IRAFS) AS VARCHAR) + '-12-31' AND LEN(P.IRACode) > 0 AND (P.Quantity = 0 AND P.CashBalance = 0)
AND ((LEN(PlanNumber) = 0) OR (PlanNumber = 'NETWORKED') OR (ISNUMERIC (PLANNUMBER) = 1)) AND
(C.DateOfBirth <= CAST(((@RptYear - @IRAFS)-71) AS VARCHAR) + '-06-30')) OR
(P .PropertyType='CASH' AND C.DateLost <= CAST((@RptYear - @IRAFS) AS VARCHAR) + '-12-31' AND LEN(P.IRACode) > 0 AND (P.Quantity > 0 OR P.CashBalance > 0) AND
(C.DateOfBirth <= CAST(((@RptYear - @IRAFS)-71) AS VARCHAR) + '-06-30')) OR
(P .PropertyType='EQUITY' AND C.DateLost <= CAST((@RptYear - @IRAFS) AS VARCHAR) + '-12-31' AND LEN(P.IRACode) > 0 AND (P.Quantity > 0 OR P.CashBalance > 0) AND
(C.DateOfBirth <= CAST(((@RptYear - @IRAFS)-71) AS VARCHAR) + '-06-30')) OR
(P .PropertyType='FIXED INCOME' AND C.DateLost <= CAST((@RptYear - @IRAFS) AS VARCHAR) + '-12-31' AND LEN(P.IRACode) > 0 AND (P.Quantity > 0 OR P.CashBalance > 0) AND
(C.DateOfBirth <= CAST(((@RptYear - @IRAFS)-71) AS VARCHAR) + '-06-30')))
ORDER BY C.[State]
Answer : How to resolve the SQL error: "ORDER BY items must appear in the select list if the statement contains a UNION, INTERSECT or EXCEPT operator"?
change your order by from
ORDER BY C.[State]
to
ORDER BY [State]
Random Solutions
Using Range(Cells(r1,c1),Cells(r<wbr />2,c2)) instead of Range("A1:B2")
Changing from Split Access DB in 2007 to SQL Server
Excel VBA Code Problem
CSocket object in thread
What is the equivalent to SQL Servers @@ROWCOUNT?
Importing and appending Excel worksheets into Access Project temporary table
Email through VBA, using sendkeys
How Can I Trigger a Select Max Value Statement from an Access Form
Sending a message "on behalf of" without Exchange
Dragging and MouseMove