November 14, 2005 at 8:46 am
Can I create a temporary table (not using dynamic SQL - due to visibility outside of execution of SQL) where I can set the ident seed to a parameter driven value?
November 14, 2005 at 9:16 am
No, not directly - identity doesn't take a variable for it's seed.
This doesn't work...
declare @i int
set @i = 100
create table #x( id int not null identity(@i,1), myVal char(1) not null)
However, there is a workaround.
This actually works..
declare @i int
set @i = 100
create table #x( id int not null identity(1,1), myVal char(1) not null)
set identity_insert #x on
insert #x (id, myVal) select @i, 'X'
set identity_insert #x off
insert #x (myVal) select 'a'
select * from #x
drop table #x
..would that fix your problem? The net effect (though a bit convoluted) would be that you can use a variable to start where new identities will be generated. Note though, that if you truncate the table, the identity_insert on/off process has to be repeated, since truncate always resets the identity.
/Kenneth
November 15, 2005 at 3:12 am
thanks but I have a insert from a select statement.... what can I do in this case?
November 15, 2005 at 3:52 am
SELECT IDENTITY(INT,1,1) AS SNO, Name,address into #tmp from <table name>
select * from #tmp
November 15, 2005 at 8:16 am
Geoff,
You could you use Kenneth's idea with a slight modification. Assuming the starting ID is greater than 1 (you could check for this), use the following code:
declare @i int
set @i = 100 -- desired starting ID
create table #x
(
id int not null identity(1,1)
, myVal char(1) not null
)
set identity_insert #x on
insert #x (id, myVal) select @i - 1, 'X'
set identity_insert #x off
INSERT #x (myVal) select 'a' -- replace with your insert.
DELETE #x WHERE id = @i - 1 -- remove the initial row
select * from #x
drop table #x
---------------------------------------------------------------------
Alternatively, you could do this:
declare @i int
set @i = 100 -- desired starting ID
create table #x
(
id int not null identity(1,1)
, myVal char(1) not null
)
DBCC CHECKIDENT (#x, RESEED, @i) WITH NO_INFOMSGS
insert #x (myVal) select 'a' -- replace with your INSERT statement
select * from #x
drop table #x
November 15, 2005 at 8:21 am
thanks the reseed is a v.good idea!
November 15, 2005 at 8:23 am
Let me warn you that dbcc checkident requires elevated priviliges
* Noel
November 15, 2005 at 8:30 am
Noel is correct, but permission to run DBCC CHECKIDENT extends to the table owner, which should be the user who creates the temp table.
November 15, 2005 at 8:39 am
Sorry but I differ:
FROM BOL:
DBCC CHECKIDENT permissions default to the table owner, members of the sysadmin fixed server role, and the db_owner and db_ddladmin fixed database role, and are not transferable.
* Noel
November 15, 2005 at 8:52 am
Noel is once again correct.
DBCC CHECKIDENT permissions default to the table owner, members of the sysadmin fixed server role, and the db_owner and db_ddladmin fixed database role, and are not transferable.
However, the owner of a temp table (in tempdb) is dbo, so unless the user is a member of the groups listed above, the DBCC CHECKIDENT method won't work. (I never use it myself).
November 15, 2005 at 8:57 am
I don't quite see why that should be a problem... Could you elaborate on your particular situation?
/Kenneth
Viewing 11 posts - 1 through 10 (of 10 total)
You must be logged in to reply to this topic. Login to reply