SQL Clone
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


UNION Query help


UNION Query help

Author
Message
VSSGeorge
VSSGeorge
SSCrazy
SSCrazy (2.1K reputation)SSCrazy (2.1K reputation)SSCrazy (2.1K reputation)SSCrazy (2.1K reputation)SSCrazy (2.1K reputation)SSCrazy (2.1K reputation)SSCrazy (2.1K reputation)SSCrazy (2.1K reputation)

Group: General Forum Members
Points: 2123 Visits: 1578
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
Koen Verbeeck
Koen Verbeeck
SSC Guru
SSC Guru (61K reputation)SSC Guru (61K reputation)SSC Guru (61K reputation)SSC Guru (61K reputation)SSC Guru (61K reputation)SSC Guru (61K reputation)SSC Guru (61K reputation)SSC Guru (61K reputation)

Group: General Forum Members
Points: 61029 Visits: 13297
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? :-)


How to post forum questions.
Need an answer? No, you need a question.
What’s the deal with Excel & SSIS?
My blog at SQLKover.

MCSE Business Intelligence - Microsoft Data Platform MVP
T.Ashish
T.Ashish
Ten Centuries
Ten Centuries (1.3K reputation)Ten Centuries (1.3K reputation)Ten Centuries (1.3K reputation)Ten Centuries (1.3K reputation)Ten Centuries (1.3K reputation)Ten Centuries (1.3K reputation)Ten Centuries (1.3K reputation)Ten Centuries (1.3K reputation)

Group: General Forum Members
Points: 1341 Visits: 607
Change
ORDER BY CboValuesTranslations.cboValueTranslationName Asc
with
ORDER BY Name Asc

Pulivarthi Sasidhar
Pulivarthi Sasidhar
SSCrazy
SSCrazy (2.4K reputation)SSCrazy (2.4K reputation)SSCrazy (2.4K reputation)SSCrazy (2.4K reputation)SSCrazy (2.4K reputation)SSCrazy (2.4K reputation)SSCrazy (2.4K reputation)SSCrazy (2.4K reputation)

Group: General Forum Members
Points: 2408 Visits: 1026
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...
T.Ashish
T.Ashish
Ten Centuries
Ten Centuries (1.3K reputation)Ten Centuries (1.3K reputation)Ten Centuries (1.3K reputation)Ten Centuries (1.3K reputation)Ten Centuries (1.3K reputation)Ten Centuries (1.3K reputation)Ten Centuries (1.3K reputation)Ten Centuries (1.3K reputation)

Group: General Forum Members
Points: 1341 Visits: 607
Pulivarthi Sasidhar (10/16/2013)
use ORDER BY CboValuesTranslations.cboValueTranslationName Asc
for 1st Select statement and then use union with 2nd select statement.


You will get error "Incorrect syntax near the keyword 'union'."
Greg Edwards-268690
Greg Edwards-268690
SSCarpal Tunnel
SSCarpal Tunnel (4.1K reputation)SSCarpal Tunnel (4.1K reputation)SSCarpal Tunnel (4.1K reputation)SSCarpal Tunnel (4.1K reputation)SSCarpal Tunnel (4.1K reputation)SSCarpal Tunnel (4.1K reputation)SSCarpal Tunnel (4.1K reputation)SSCarpal Tunnel (4.1K reputation)

Group: General Forum Members
Points: 4132 Visits: 8593
It looks like you have 5 columns in 1 query and 4 in the other.
T.Ashish
T.Ashish
Ten Centuries
Ten Centuries (1.3K reputation)Ten Centuries (1.3K reputation)Ten Centuries (1.3K reputation)Ten Centuries (1.3K reputation)Ten Centuries (1.3K reputation)Ten Centuries (1.3K reputation)Ten Centuries (1.3K reputation)Ten Centuries (1.3K reputation)

Group: General Forum Members
Points: 1341 Visits: 607
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.
Greg Edwards-268690
Greg Edwards-268690
SSCarpal Tunnel
SSCarpal Tunnel (4.1K reputation)SSCarpal Tunnel (4.1K reputation)SSCarpal Tunnel (4.1K reputation)SSCarpal Tunnel (4.1K reputation)SSCarpal Tunnel (4.1K reputation)SSCarpal Tunnel (4.1K reputation)SSCarpal Tunnel (4.1K reputation)SSCarpal Tunnel (4.1K reputation)

Group: General Forum Members
Points: 4132 Visits: 8593
I see I missed (...(...))
Kurt W. Zimmerman
Kurt W. Zimmerman
SSCrazy
SSCrazy (2.6K reputation)SSCrazy (2.6K reputation)SSCrazy (2.6K reputation)SSCrazy (2.6K reputation)SSCrazy (2.6K reputation)SSCrazy (2.6K reputation)SSCrazy (2.6K reputation)SSCrazy (2.6K reputation)

Group: General Forum Members
Points: 2574 Visits: 1400
T.Ashish (10/16/2013)
Change
ORDER 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
Greg Edwards-268690
Greg Edwards-268690
SSCarpal Tunnel
SSCarpal Tunnel (4.1K reputation)SSCarpal Tunnel (4.1K reputation)SSCarpal Tunnel (4.1K reputation)SSCarpal Tunnel (4.1K reputation)SSCarpal Tunnel (4.1K reputation)SSCarpal Tunnel (4.1K reputation)SSCarpal Tunnel (4.1K reputation)SSCarpal Tunnel (4.1K reputation)

Group: General Forum Members
Points: 4132 Visits: 8593
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.
Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum

































































































































































SQLServerCentral


Search