Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 Auto Increment for INSERT INTO

Author  Topic 

vendn5
Starting Member

1 Post

Posted - 2006-12-19 : 13:02:06
I'm populating a table (TMP_SQL) with data from several other tables in our database.

I need to create through SQL commands a counter variable to fill TMP_SQL's KEY1 field. The data I'm pulling in from other tables does not have a field that is always 100% gaurenteed unique, so I need to create a counter for a key field.

Pseudo code:

insert into TMP_SQL(tmp_key1, tmp_general1, tmp_general2, tmp_number1, ....)
select @counter, someColumn, someColumn2, (select column3 from other table returning 1 row),...
from table1 where things = things.


How can I implement @counter = @counter + 1? Is this possible? I tried:
DECLARE @COUNTER INT
SET @COUNTER = 0

and then had @COUNTER = @COUNTER + 1 in my select statement; of course, I got the error about not being able to set variables in the same select as data-retrieval

Computers never do what I tell them to...

SwePeso
Patron Saint of Lost Yaks

30421 Posts

Posted - 2006-12-19 : 13:04:26
Create a new column as IDENTITY(1, 1) column...


Peter Larsson
Helsingborg, Sweden
Go to Top of Page
   

- Advertisement -