Reputation: 1
Query:
USE [IEC]
GO
ALTER VIEW [dbo].[V_AllOA]
AS
SELECT ROW_NUMBER() OVER (ORDER BY OrderId) OAId,
*
FROM (
SELECT *
FROM BranchOrder
WHERE Flag IN (1, 2)
UNION
SELECT *
FROM BranchOrder12_13
WHERE flag IN (1, 2)
UNION
SELECT *
FROM BranchOrder11_12
WHERE flag IN (1, 2)
) a
GO
Upvotes: 0
Views: 173
Reputation: 40481
When using union, you must make sure all the columns, including their ORDER and TYPE are the same.
for example: table1
ID, NAME , LASTNAME
1 'john' 'smith'
table2 -
ID , NAME , MIDDLENAME , LASTNAME
4 'john' 'bing' 'smith'
On the example above, both table can't be union using select * since they have different number of columns.
In that case, you can specify the same columns that appears on both table in your select statement like this:
SELECT ID,NAME,LASTNAME from table1
UNION
SELECT ID,NAME,LASTNAME from table2
This is a correct query.
SELECT * FROM table1
UNION
SELECT * FROM TABLE2
This is not a correct query.
Upvotes: 1
Reputation: 397
Your tables BranchOrder, BranchOrder12_13, BranchOrder11_12 have different number of columns. They must all have exact number of columns and they must be in the same order - I mean if first column in first table is integer, the first column in other tables must be integer as well.
Upvotes: 2