Msg 205 Level 16 State 1 Line 1 – SQLSERVERLEARNER https://sqlserverlearner.com LEARN SQL SERVER ONLINE Fri, 27 Apr 2012 09:45:17 +0000 en-US hourly 1 https://wordpress.org/?v=6.5.2 All queries combined using a UNION, INTERSECT or EXCEPT operator must have an equal number of expressions in their target lists. https://sqlserverlearner.com/2012/04/27/all-queries-combined-using-a-union-intersect-or-except-operator-must-have-an-equal-number-of-expressions-in-their-target-lists/ https://sqlserverlearner.com/2012/04/27/all-queries-combined-using-a-union-intersect-or-except-operator-must-have-an-equal-number-of-expressions-in-their-target-lists/#respond Fri, 27 Apr 2012 09:45:17 +0000 https://sqlserverlearner.azurewebsites.net/2012/04/27/all-queries-combined-using-a-union-intersect-or-except-operator-must-have-an-equal-number-of-expressions-in-their-target-lists/ Error:

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

Reason:
The select statements in the clause do not have the same number of columns.

Fix:
Make sure that all the starements using UNION, INTERSECT or EXCEPT operator have same number of expressions(columns).
Use column names instead of using * in the select list.

Sample example code

Before:
[sql]
SELECT 1
UNION
SELECT 2,3
[/sql]

After:
[sql]
SELECT 1,NULL
UNION
SELECT 2,3
[/sql]

]]>
https://sqlserverlearner.com/2012/04/27/all-queries-combined-using-a-union-intersect-or-except-operator-must-have-an-equal-number-of-expressions-in-their-target-lists/feed/ 0