?
Solved

All queries combined using a UNION, INTERSECT or EXCEPT operator must have an equal number of expressions in their target lists.

Posted on 2009-04-11
2
Medium Priority
?
2,191 Views
Last Modified: 2012-05-06
Why the following sql has the error:

All queries combined using a UNION, INTERSECT or EXCEPT operator must have an equal number of expressions in their target lists.


select a.id, shortcode, c.currency, a.type from account a inner join currency c on a.currencyid = c.id WHERE a.id > -1 union select -2 as id, '-- Please Select --' as shortcode from account a order by a.id

Open in new window

0
Comment
Question by:techques
2 Comments
 
LVL 57

Accepted Solution

by:
Raja Jegan R earned 150 total points
ID: 24122731
Try this query out:

Removed ORDER BY clause because UNION doesn't allows it.

SELECT a.id,shortcode,c.currency,a.type
FROM   account a
INNER JOIN currency c
  ON   a.currencyid = c.id
WHERE  a.id > -1 
UNION 
SELECT -2 AS id,'-- Please Select --' AS shortcode, NULL, null
FROM   account a

Open in new window

0
 
LVL 143

Expert Comment

by:Guy Hengel [angelIII / a3]
ID: 24122810
you can use order by (no points here) like this:

SELECT a.id,shortcode,c.currency,a.type
FROM   account a
INNER JOIN currency c
  ON   a.currencyid = c.id
WHERE  a.id > -1 
UNION 
SELECT -2 AS id,'-- Please Select --' AS shortcode, NULL, null
FROM   account a
ORDER BY 1

Open in new window

0

Featured Post

What is SQL Server and how does it work?

The purpose of this paper is to provide you background on SQL Server. It’s your self-study guide for learning fundamentals. It includes both the history of SQL and its technical basics. Concepts and definitions will form the solid foundation of your future DBA expertise.

Question has a verified solution.

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

I've encountered valid database schemas that do not have a primary key.  For example, I use LogParser from Microsoft to push IIS logs into a SQL database table for processing and analysis.  However, occasionally due to user error or a scheduled task…
Use this article to create a batch file to backup a Microsoft SQL Server database to a Windows folder.  The folder can be on the local hard drive or on a network share.  This batch file will query the SQL server to get the current date & time and wi…
Is your data getting by on basic protection measures? In today’s climate of debilitating malware and ransomware—like WannaCry—that may not be enough. You need to establish more than basics, like a recovery plan that protects both data and endpoints.…
Whether it be Exchange Server Crash Issues, Dirty Shutdown Errors or Failed to mount error, Stellar Phoenix Mailbox Exchange Recovery has always got your back. With the help of its easy to understand user interface and 3 simple steps recovery proced…

850 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