Quantcast
Channel: SQLServerCentral » SQL Server 2014 » Development - SQL Server 2014 » Latest topics
Viewing all articles
Browse latest Browse all 3145

IF Statement with Insert

$
0
0
Hi AllSQL 2014...DDL:[code="sql"]CREATE TABLE [dbo].[Table1]( [Col1] [int] IDENTITY(1,1) NOT NULL, [Col2] [varchar](255) NULL, [Col3] [varchar](1024) NULL, [Col4] [varchar](1024) NULL, [Col5] [char](20) NULL) ON [PRIMARY][/code]DML:[code="sql"]IF @@SERVERNAME = 'CORRECT_SERVER_NAME'BEGIN INSERT INTO Table1 VALUES ('some_value', 'some_value', 'some_value', 'some_value')ENDELSEBEGIN INSERT INTO Table1 VALUES (5, 'some_value', 'some_value', 'some_value', 'some_value') END[/code]Running the DML statement produces this error:[code="plain"]An explicit value for the identity column in table 'Table1' can only be specified when a column list is used and IDENTITY_INSERT is ON.[/code]This doesn't make any sense because the IF statement should valuate to TRUE and proceed with the first statement, the one that doesn't include an explicit value for the first column. I would understand this error appearing if I ran the 2nd insert statement on its own. What's funny is that when I comment out the 2nd insert statement and replace it with a PRINT 'TEST' command, then it works as it should. Any ideas?Thanks

Viewing all articles
Browse latest Browse all 3145

Trending Articles