Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2005 Forums
 Transact-SQL (2005)
 error: All queries combined using a UNION, INTERSE

Author  Topic 

mcupryk
Yak Posting Veteran

91 Posts

Posted - 2010-02-21 : 22:06:20
error: All queries combined using a UNION, INTERSECT or EXCEPT operator must have an equal number of expressions in their target lists.

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


SELECT




TOP 5 * FROM

(



SELECT *, 0 AS [PriorityOrder] FROM [dbo].[tbl_Images]

UNION ALL

SELECT



*, 1 AS [PriorityOrder] FROM [dbo].[tbl_NoPhotos]) A

ORDER



BY [PriorityOrder]

hi here is the things?

CREATE TABLE [dbo].[tbl_Images](
[img_id] [tinyint] IDENTITY(1,1) NOT NULL,
[img_caption] [nvarchar](50) NULL,
[img_title] [nvarchar](50) NULL,
[img_stream] [varbinary](max) NULL,
[img_type] [nvarchar](50) NULL,
[img_size] [bigint] NULL,
[img_is_primary] [bit] NULL,
[ProfileID] [int] NOT NULL,
CONSTRAINT [PK_tbl_Images] PRIMARY KEY CLUSTERED
(
[img_id] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY]


---------------------------------------------------------------------
CREATE TABLE [dbo].[tbl_NoPhotos](
[img_id] [tinyint] NOT NULL,
[img_caption] [nvarchar](50) NULL,
[img_title] [nvarchar](50) NULL,
[img_stream] [varbinary](max) NULL,
[img_type] [nvarchar](50) NULL,
[img_size] [bigint] NULL,
CONSTRAINT [PK_tbl_NoPhotos_1] PRIMARY KEY CLUSTERED
(
[img_id] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY]

sakets_2000
Master Smack Fu Yak Hacker

1472 Posts

Posted - 2010-02-21 : 22:33:23
your first table has 8 columns and second table has 6 columns. Unions are trying to combine 9 and 7 columns together, that why the error.
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2010-02-22 : 08:36:16
dont put * in selects. Also make sure you corresponding columns put in both queries separated by UNION ALL has same datatypes

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/

Go to Top of Page
   

- Advertisement -