vahid.arr (5/25/2013)
i write this query but it give me this error:All queries combined using a UNION, INTERSECT or EXCEPT operator must have an equal number of expressions in their target lists.
select SabtHazineID, SabtHazine.HazineID, EndUserNAme, Tedad, sabtHazineDate, Describtion from dbo.SabtHazine
left JOIN dbo.Hazine ON dbo.Hazine.HazineID = dbo.SabtHazine.HazineID
union all
select Hazine.HazineName,Hazine.Mablagh from AgencyDB.dbo.Hazine
where HazineID not in (select HazineID from dbo.Hazine)
The error message is telling you exactly what the problem is.
In the first query you have 6 columns. In the second query you only have 2 columns. Both queries must have the same number of columns (or literals) with matching data types when you use UNION, UNION ALL, INTERSECT or EXCEPT.
Code reformatted to make it easier to read:
select
SabtHazineID,
SabtHazine.HazineID,
EndUserNAme,
Tedad,
sabtHazineDate,
Describtion
from
dbo.SabtHazine
left JOIN dbo.Hazine
ON dbo.Hazine.HazineID = dbo.SabtHazine.HazineID
union all
select
Hazine.HazineName,
Hazine.Mablagh
from
AgencyDB.dbo.Hazine
where
HazineID not in (select HazineID from dbo.Hazine);