Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Help using @variable while creating a new table

Status
Not open for further replies.

bmacbmac

IS-IT--Management
Jan 26, 2006
392
US
Hi,

I would like to create a new table with an identity. I'm hoping to run this script unattended.

I would like the beginning counter to be the max(id) + 1 from a different table.

So far I have tried
Code:
declare @maxirangeid int
set @maxirangeid = (select MAX(irangeid) + 1 from tempblockbase)

create table tempnewblock (irangeid int identity(@maxirangeid, 1), ilandid int, minint int)

But getting a syntax error.

Is there a way for me to do this?

Thanks!
 
Maybe try:
Code:
declare @maxirangeid int
DECLARE @DynamicSQL nvarchar(1000);
set @maxirangeid = 5
SET @DynamicSQL = 'create table tempnewblock (irangeid int identity(' + convert(varchar,@maxirangeid)
 +  ', 1), ilandid int, minint int)'
print @dynamicsql

EXEC sp_executesql @DynamicSQL;
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top