?
Solved

Tempdb IDENTITY as Variable

Posted on 2004-09-15
5
Medium Priority
?
193 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:Greg Rowland
[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
5 Comments
 
LVL 17

Accepted Solution

by:
BillAn1 earned 300 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 43

Assisted Solution

by:Eugene Z
Eugene Z earned 300 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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Why is this different from all of the other step by step guides?  Because I make a living as a DBA and not as a writer and I lived through this experience. Defining the name: When I talk to people they say different names on this subject stuff l…
Recently we ran in to an issue while running some SQL jobs where we were trying to process the cubes.  We got an error saying failure stating 'NT SERVICE\SQLSERVERAGENT does not have access to Analysis Services. So this is a way to automate that wit…
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.
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.

719 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