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)
 Union Queries problems

Author  Topic 

Trudye
Posting Yak Master

169 Posts

Posted - 2008-07-23 : 13:23:54
Hey Guys I have a question. I have seen this done before but a temp table was used (eg. #tblCSTR). But for some reason I can't get it to work with a perm table. Can anyone tell me what I'm missing.

Thanx much,
Trudye

SELECT '0 General Totals' AS Source, ztblCSR.Error_Desc,
Sum(ztblCSR.CountOfResultsPKey) AS Total_Count, ztblCSR.SUBID,@StartDT as Start, @EndDt as EndDate
FROM ztblCSR
GROUP BY ztblCSR.Error_Desc, ztblCSR.SUBID

UNION ALL
Select * from ztblCSR
ORDER BY Source ,Sum(ztblCSR.CountOfResultsPKey)Desc



Msg 205, Level 16, State 1, Procedure zzzSP_Top5, Line 43
All queries combined using a UNION, INTERSECT or EXCEPT operator must have an equal number of expressions in their target lists.

SwePeso
Patron Saint of Lost Yaks

30421 Posts

Posted - 2008-07-23 : 13:27:14
The error message means both queries must have equal number of columns.



E 12°55'05.25"
N 56°04'39.16"
Go to Top of Page

rohitkumar
Constraint Violating Yak Guru

472 Posts

Posted - 2008-07-23 : 13:27:51
All queries combined using a UNION, INTERSECT or EXCEPT operator must have an equal number of expressions in their target lists.
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2008-07-23 : 13:28:28
make sure the columnlist included in secong queries have same number of fields as in first part (6 columns) and also their datatypes are same as corresponding ones in first query.
Go to Top of Page

SwePeso
Patron Saint of Lost Yaks

30421 Posts

Posted - 2008-07-23 : 13:32:51
quote:
Originally posted by rohitkumar

All queries combined using a UNION, INTERSECT or EXCEPT operator must have an equal number of expressions in their target lists.
Wow, that was an exact replica of the original error message!



E 12°55'05.25"
N 56°04'39.16"
Go to Top of Page

rohitkumar
Constraint Violating Yak Guru

472 Posts

Posted - 2008-07-23 : 13:37:26
quote:
Originally posted by Peso

Wow, that was an exact replica of the original error message!



E 12°55'05.25"
N 56°04'39.16"




yes it is :), I wanted Trudye to read it.
Go to Top of Page

Trudye
Posting Yak Master

169 Posts

Posted - 2008-07-24 : 07:56:01
Thanks to everyone for responding. I recompiled it this morning without changing anything and it worked. I have NO IDEA what happend, but I knew I had seen simular code before, I searched our procs and found it. I matched the code, I dbl ck's my table to insure I had all of the necessary fields and I did. I recompiled and VOILA!

Sorry for bothering you,
Trudye
Go to Top of Page

SwePeso
Patron Saint of Lost Yaks

30421 Posts

Posted - 2008-07-24 : 07:58:08
Someone might have deleted (or added) a column in table ztblCSR ?
You should ALWAYS avoid using * in production code.



E 12°55'05.25"
N 56°04'39.16"
Go to Top of Page
   

- Advertisement -