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
1,643 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 50 total points
Comment Utility
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 142

Expert Comment

by:Guy Hengel [angelIII / a3]
Comment Utility
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

Comprehensive Backup Solutions for Microsoft

Acronis protects the complete Microsoft technology stack: Windows Server, Windows PC, laptop and Surface data; Microsoft business applications; Microsoft Hyper-V; Azure VMs; Microsoft Windows Server 2016; Microsoft Exchange 2016 and SQL Server 2016.

Join & Write a Comment

Data architecture is an important aspect in Software as a Service (SaaS) delivery model. This article is a study on the database of a single-tenant application that could be extended to support multiple tenants. The application is web-based develope…
This article explains how to reset the password of the sa account on a Microsoft SQL Server.  The steps in this article work in SQL 2005, 2008, 2008 R2, 2012, 2014 and 2016.
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…
When you create an app prototype with Adobe XD, you can insert system screens -- sharing or Control Center, for example -- with just a few clicks. This video shows you how. You can take the full course on Experts Exchange at http://bit.ly/XDcourse.

763 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

Need Help in Real-Time?

Connect with top rated Experts

7 Experts available now in Live!

Get 1:1 Help Now