Solved

Table Variable / Temporary Table (SET IDENTITY_INSERT ON) - SQL Server

Posted on 2012-04-09
9
2,065 Views
Last Modified: 2012-04-09
Hi,

I have the following code. My first question is whether the SET IDENTITY_INSERT ON works with Table Variable / Temporary Table or not !!

CODE SEGMENT: 1

declare @TableVar table (PK int identity(1,1), CustomerName varchar(5))

insert Into @TableVar VALUES ('XXXXX')
insert Into @TableVar VALUES ('YYYYY')
insert Into @TableVar VALUES ('AAAAAA')  -- Error here
insert Into @TableVar VALUES ('ZZZZZ')

select * from @TableVar

set identity_insert on
insert into @TableVar VALUES (3, 'AAAAA')
set identity_insert off

select * from @TableVar

CODE SEGMENT: 2

create table #TempTable (PK int identity(1,1), CustomerName varchar(5))

insert Into #TempTable VALUES ('XXXXX')
insert Into #TempTable VALUES ('YYYYY')
insert Into #TempTable VALUES ('AAAAAA')  -- Error here
insert Into #TempTable VALUES ('ZZZZZ')

select * from #TempTable

set identity_insert on
insert into #TempTable VALUES (3, 'AAAAA')
set identity_insert off

select * from #TempTable

Thanks
0
Comment
Question by:milani_lucie
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 6
  • 2
9 Comments
 
LVL 69

Expert Comment

by:Scott Pletcher
ID: 37823865
insert Into @TableVar VALUES ('AAAAAA')  -- Error here

That's six bytes trying to go into a varchar(5), which probably accounts for an error there.
0
 

Author Comment

by:milani_lucie
ID: 37823876
That is the reason i want to set the identity of value "3" (which was never used) using:

set identity_insert on
insert into @TableVar VALUES (3, 'AAAAA')
set identity_insert off

Thanks
0
 
LVL 69

Assisted Solution

by:Scott Pletcher
Scott Pletcher earned 250 total points
ID: 37823910
Yes, SET IDENTITY_INSERT should work with temp tables as well.

Ooh, just remembered, I think when you want to insert identity values you *must* specify a column list; I think SQL requires it.

So please try this:

insert into #TempTable (PK CustomerName) VALUES (3, 'AAAAA')
0
NFR key for Veeam Backup for Microsoft Office 365

Veeam is happy to provide a free NFR license (for 1 year, up to 10 users). This license allows for the non‑production use of Veeam Backup for Microsoft Office 365 in your home lab without any feature limitations.

 

Author Comment

by:milani_lucie
ID: 37823926
Great !! It works fine with "Temporary Table". Does it works with "Table Variable" ?

Thanks
0
 

Author Comment

by:milani_lucie
ID: 37823943
Here is the complete code:

CODE SEGMENT: 1

declare @TableVar table (PK int identity(1,1), CustomerName varchar(5))

insert Into @TableVar VALUES ('XXXXX')
insert Into @TableVar VALUES ('YYYYY')
insert Into @TableVar VALUES ('AAAAAA')  -- Error here
insert Into @TableVar VALUES ('ZZZZZ')

select * from @TableVar

set identity_insert @TableVar on
insert into @TableVar (PK, CustomerName) VALUES (3, 'AAAAA')
set identity_insert @TableVar off

select * from @TableVar

CODE SEGMENT: 2

create table #TempTable (PK int identity(1,1), CustomerName varchar(5))

insert Into #TempTable VALUES ('XXXXX')
insert Into #TempTable VALUES ('YYYYY')
insert Into #TempTable VALUES ('AAAAAA')  -- Error here
insert Into #TempTable VALUES ('ZZZZZ')

select * from #TempTable

set identity_insert #TempTable on
insert into #TempTable (PK, CustomerName) VALUES (3, 'AAAAA')
set identity_insert #TempTable off

select * from #TempTable

Thanks
0
 

Author Comment

by:milani_lucie
ID: 37824035
Getting error with "Table Variable": Incorrect syntax near '@TableVar'

Just want to make sure that it will NOT work with "Table Variable" - Thanks
0
 

Author Comment

by:milani_lucie
ID: 37824041
Error here: set identity_insert @TableVar on

Thanks
0
 
LVL 23

Accepted Solution

by:
wdosanjos earned 250 total points
ID: 37824114
INSERT into an identity column not allowed on table variables.
0
 

Author Comment

by:milani_lucie
ID: 37824165
Got the answer:

http://sqlwithmanoj.wordpress.com/tag/table-variables/

19. You cannot insert explicit values into an identity column (the table variable does not support the SET IDENTITY_INSERT ON).

Thanks
0

Featured Post

NFR key for Veeam Backup for Microsoft Office 365

Veeam is happy to provide a free NFR license (for 1 year, up to 10 users). This license allows for the non‑production use of Veeam Backup for Microsoft Office 365 in your home lab without any feature limitations.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

This article explains how to reset the password of the sa account on a Microsoft SQL Server.  The steps in this article work in SQL 2005, 2008, 2008 R2, 2012, 2014 and 2016.
I have a large data set and a SSIS package. How can I load this file in multi threading?
This video shows, step by step, how to configure Oracle Heterogeneous Services via the Generic Gateway Agent in order to make a connection from an Oracle session and access a remote SQL Server database table.
Viewers will learn how to use the UPDATE and DELETE statements to change or remove existing data from their tables. Make a table: Update a specific column given a specific row using the UPDATE statement: Remove a set of values using the DELETE s…

730 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question