October 16, 2013 at 2:47 am
I am getting a error message while executing the query below:
Msg 104, Level 16, State 1, Line 18
ORDER BY items must appear in the select list if the statement contains a UNION, INTERSECT or EXCEPT operator.
SELECT ISNULL(CboValuesTranslations.cboValueTranslationName,ISNULL(CboValues.cboValueName,'')) as Name,
cboValues.cboValueSqlId as SqlId, CboValues.cboValueIncId as IncId,
LTRIM(STR(cboValues.cboValueSqlId))+'-'+LTRIM(STR(CboValues.cboValueIncId)) AS KeyId
FROM ListsOfCboValues INNER JOIN CboValues ON ListsOfCboValues.listOfCboValueSqlId = CboValues.listOfCboValueSqlId AND ListsOfCboValues.listOfCboValueIncId = CboValues.listOfCboValueIncId AND CboValues.isDeleted=0x0 LEFT OUTER JOIN CboValuesTranslations ON cboValuesTranslations.cboValueSqlId = cboValues.cboValueSqlId AND cboValuesTranslations.cboValueIncId = cboValues.cboValueIncId AND cboValuesTranslations.isDeleted=0x0 AND cboValuesTranslations.LanguageSqlId=2 AND cboValuesTranslations.LanguageIncId=2
WHERE ListsOfCboValues.isDeleted = 0x0 AND (ListsOfCboValues.listOfCboValueCode = 'EAS-00008')
UNION
SELECT 'NULL' as Name,
null as SqlId, null as IncId,
'NULL' AS KeyId
ORDER BY CboValuesTranslations.cboValueTranslationName Asc
October 16, 2013 at 2:58 am
I guess CboValuesTranslations.cboValueTranslationName is not used in certain rows as it is inside an ISNULL function. If CboValuesTranslations.cboValueTranslationName is NULL, another column is used.
I would simply select CboValuesTranslations.cboValueTranslationName and CboValues.cboValueName as two seperate columns and use an outer query to apply the ISNULL logic.
ps: ever heard of aliases? 🙂
Need an answer? No, you need a question
My blog at https://sqlkover.com.
MCSE Business Intelligence - Microsoft Data Platform MVP
October 16, 2013 at 3:10 am
Change
ORDER BY CboValuesTranslations.cboValueTranslationName Asc
with
ORDER BY Name Asc
October 16, 2013 at 3:26 am
use ORDER BY CboValuesTranslations.cboValueTranslationName Asc
for 1st Select statement and then use union with 2nd select statement.
or
Write a Select on top of Statements and then sort...
October 16, 2013 at 3:35 am
Pulivarthi Sasidhar (10/16/2013)
use ORDER BY CboValuesTranslations.cboValueTranslationName Ascfor 1st Select statement and then use union with 2nd select statement.
You will get error "Incorrect syntax near the keyword 'union'."
October 17, 2013 at 5:44 am
It looks like you have 5 columns in 1 query and 4 in the other.
October 17, 2013 at 7:00 am
Greg Edwards-268690 (10/17/2013)
It looks like you have 5 columns in 1 query and 4 in the other.
No, Both has 4 columns.
October 17, 2013 at 7:07 am
I see I missed (...(...))
October 17, 2013 at 7:47 am
T.Ashish (10/16/2013)
ChangeORDER BY CboValuesTranslations.cboValueTranslationName Asc
with
ORDER BY Name Asc
+1 on this. CboValuesTranslations.cboValueTranslationName is not in your second SELECT....
Kurt
Kurt W. Zimmerman
SR DBA
Lefrak Organization
New York, NY
http://www.linkedin.com/in/kurtwzimmerman
October 17, 2013 at 1:33 pm
If you use aliases, it becomes a bit clearer where another issue might lie.
It looks like you are mixing joins with select criteria.
AND CboValues.isDeleted=0x0 is the first one.
Maybe you copied and pasted and things got mixed up.
I would have this, and some similar, below the WHERE clause.
October 17, 2013 at 4:11 pm
Or just:
ORDER BY 1
I often avoid using the column names at all in UNIONed queries, as the names somehow seem to
change a lot 🙂 :
SQL DBA,SQL Server MVP(07, 08, 09) "It's a dog-eat-dog world, and I'm wearing Milk-Bone underwear." "Norm", on "Cheers". Also from "Cheers", from "Carla": "You need to know 3 things about Tortelli men: Tortelli men draw women like flies; Tortelli men treat women like flies; Tortelli men's brains are in their flies".
October 17, 2013 at 9:18 pm
Parentheses
(SELECT ISNULL(CboValuesTranslations.cboValueTranslationName,ISNULL(CboValues.cboValueName,'')) as Name,
cboValues.cboValueSqlId as SqlId, CboValues.cboValueIncId as IncId,
LTRIM(STR(cboValues.cboValueSqlId))+'-'+LTRIM(STR(CboValues.cboValueIncId)) AS KeyId
FROM ListsOfCboValues INNER JOIN CboValues ON ListsOfCboValues.listOfCboValueSqlId = CboValues.listOfCboValueSqlId AND ListsOfCboValues.listOfCboValueIncId = CboValues.listOfCboValueIncId AND CboValues.isDeleted=0x0 LEFT OUTER JOIN CboValuesTranslations ON cboValuesTranslations.cboValueSqlId = cboValues.cboValueSqlId AND cboValuesTranslations.cboValueIncId = cboValues.cboValueIncId AND cboValuesTranslations.isDeleted=0x0 AND cboValuesTranslations.LanguageSqlId=2 AND cboValuesTranslations.LanguageIncId=2
WHERE ListsOfCboValues.isDeleted = 0x0 AND (ListsOfCboValues.listOfCboValueCode = 'EAS-00008')
UNION
SELECT 'NULL' as Name,
null as SqlId, null as IncId,
'NULL' AS KeyId)
ORDER BY 1 Asc
October 17, 2013 at 10:50 pm
Good. Now OP has many ways to order his UNION query.
October 17, 2013 at 11:02 pm
ScottPletcher (10/17/2013)
Or just:ORDER BY 1
I often avoid using the column names at all in UNIONed queries, as the names somehow seem to
change a lot 🙂 :
Thank goodness that method has finally been deprecated.
--Jeff Moden
Change is inevitable... Change for the better is not.
October 17, 2013 at 11:06 pm
Jeff Moden (10/17/2013)
ScottPletcher (10/17/2013)
Or just:ORDER BY 1
I often avoid using the column names at all in UNIONed queries, as the names somehow seem to
change a lot 🙂 :
Thank goodness that method has finally been deprecated.
+1 for Jeff Moden
Viewing 15 posts - 1 through 15 (of 17 total)
You must be logged in to reply to this topic. Login to reply