Solved

Tempdb IDENTITY as Variable

Posted on 2004-09-15
5
186 Views
Last Modified: 2006-11-17
--Why does this not work?

declare @myNumber integer

set @myNumber = 501

DROP TABLE  [#myTEMP]

CREATE TABLE [#myTEMP]
      (
      [myFIELD] [int] IDENTITY( @myNumber,  1) NOT NULL
      )
0
Comment
Question by:SurferJoe
5 Comments
 
LVL 17

Accepted Solution

by:
BillAn1 earned 75 total points
ID: 12070413
The CREATE TABLE doesn't allow for a variable, however you can CREATE with value 1 then immediately RESEED to something else :

declare @myNumber integer

set @myNumber = 501

DROP TABLE  [#myTEMP]

CREATE TABLE [#myTEMP]
     (
     [myFIELD] [int] IDENTITY( 1,  1) NOT NULL
     )

DBCC CHECKIDENT ('#myTEMP', RESEED, @MyNumber)
0
 
LVL 42

Assisted Solution

by:EugeneZ
EugeneZ earned 75 total points
ID: 12070765
or  you can try global temp table (or regular table):

declare @myNumber integer
declare @str nvarchar(1000)
set @myNumber = 501

DROP TABLE  ##myTEMP

set @str ='CREATE TABLE ##myTEMP
     (
     [myFIELD] [int] IDENTITY(' + cast(@myNumber as varchar(10)) + ',  1) NOT NULL
     )'
--print @str
exec(@str)

select * from ##myTEMP
0

Featured Post

Microsoft Certification Exam 74-409

Veeam® is happy to provide the Microsoft community with a study guide prepared by MVP and MCT, Orin Thomas. This guide will take you through each of the exam objectives, helping you to prepare for and pass the examination.

Question has a verified solution.

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

I wrote this interesting script that really help me find jobs or procedures when working in a huge environment. I could I have written it as a Procedure but then I would have to have it on each machine or have a link to a server-related search that …
The Delta outage: 650 cancelled flights, more than 1200 delayed flights, thousands of frustrated customers, tens of millions of dollars in damages – plus untold reputational damage to one of the world’s most trusted airlines. All due to a catastroph…
Via a live example, show how to set up a backup for SQL Server using a Maintenance Plan and how to schedule the job into SQL Server Agent.
Using examples as well as descriptions, and references to Books Online, show the documentation available for datatypes, explain the available data types and show how data can be passed into and out of variables.

831 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