Mar 23, 2014

SELECT INTO must be the first query in a statement containing a UNION, INTERSECT or EXCEPT operator sql server


Suppose we have created a table One and two by using following query:

CREATE TABLE One(
    Id INT,
    Data VARCHAR(100)
)

CREATE TABLE Two(
    Id INT,
    Data VARCHAR(100)
)

Now if we execute following sql queries:

SELECT * FROM One

UNION

SELECT * INTO #Temp FROM  Two

Or

SELECT * FROM One

EXCEPT

SELECT * INTO #Temp FROM  Two

We will get error message like :

SELECT INTO must be the first query in a statement containing a UNION, INTERSECT or EXCEPT operator Sql server.

Cause: "INTO #Temp" have to use in first select statement.

Solution:

SELECT * INTO #Temp FROM One

UNION

SELECT * FROM  Two

Or

SELECT * INTO #Temp FROM One

EXCEPT

SELECT * FROM  Two

No comments:

Post a Comment