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 TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

INVAID - SELECT firstname, identity(int,1,@ID) INTO newtable FROM oldt 1

Status
Not open for further replies.

spangeman

Programmer
Oct 16, 2004
96
EU
Is there a way of doing this?
The @ID in the identity column is invalid at the moment

DECLARE @ID AS INT
SET @ID = (SELECT MAX(@ID) FROM IDENTS)
SELECT firstname, identity(int,1,@ID) INTO newtable FROM oldtable
 
Two problems:

- Identity() "column" in SELECT INTO must have a name.
- variables are not allowed - even @ID + identity(int, 1, 1) won't work

SELECT INTO works only if target table doesn't exist, so you can try this:
Code:
DECLARE @ID AS INT
SET @ID = (SELECT MAX(@ID) FROM IDENTS)
SELECT firstname, identity(int, 1, 1) [b]AS some_name[/b] INTO newtable FROM oldtable
[b]UPDATE newtable SET some_name = some_name + @ID[/b]
 
Works great thanks!

Simple really, populate your table then add the same amount (your current max @ID value) to all IDs.

Just goes to show how thinking outside the problems domain helps, I was too concerned with getting the result within the identity(int, 1, 1) statement.

Thanks again.
 
Spangeman, it won't be long before you realize that vongrunt not only thinks outside the box, he thinks outside of the outside of the box.
BTW, thanks for the kind words about my crosstab FAQ.
-Karl

[red] Cursors, triggers, user-defined functions and dynamic SQL are an axis of evil![/red]
[green]Life's uncertain...eat dessert first...www.deerfieldbakery.com[/green]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top