The IDENTITY function cannot be used with a SELECT INTO statement containing a UNION, INTERSECT or EXCEPT operator.

Error Message:
Msg 1057, Level 15, State 5, Line 11
The IDENTITY function cannot be used with a SELECT INTO statement containing a UNION, INTERSECT or EXCEPT operator.

Severity level:
15.

Description:
This error message appears when in a SELECT INTO statement that involves a UNION, INTERSECT, or EXCEPT operator, the IDENTITY function shall be used.

Consequences:
The SQL statement cannot be parsed and further execution is stopped.

Resolution:
Errors of the Severity Level 15 are generated by the user and can be fixed by the SQL Server user. The statement cannot be executed this way. You cannot use the IDENTITY function in such a SELECT INTO statement.

Versions:
All versions of SQL Server.

Example(s):
IF OBJECT_ID (‘dbo.T’) IS NOT NULL
    DROP TABLE dbo.T
SELECT
    IDENTITY(int, 1,1) AS ID_Num
INTO
    dbo.T
FROM
    (SELECT 1) X
UNION ALL
SELECT
    2

Remarks:
In the above example we try to use the IDENTITY function in a SELECT INTO statement that contains a UNION operator. This raises the error.




Array

No comments yet... Be the first to leave a reply!