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
 SQL Server 2000 Forums
 Transact-SQL (2000)
 Computed counter column

Author  Topic 

DEfusion
Starting Member

2 Posts

Posted - 2005-03-08 : 13:02:39
I'm doing some insert selects - I need to insert an order counter into the target table that doesn't exist in the source table.

I've tried the following :


DECLARE @counter int
SET @counter = 1

SELECT TOP 12 colA, colB, @counter = @counter + 1, @counter AS orderCounter


But I obviously get errors as you can't set data and get data within a SELECT at the same time.

Anyone have any ideas as to how I could achieve this simply?

Thanks in advance,

-D

spirit1
Cybernetic Yak Master

11752 Posts

Posted - 2005-03-08 : 13:09:48
insert the data normally and then update the counter column:

DECLARE @counter int
SET @counter = 1
UPDATE MyTable
SET @counter = orderCounter = @counter + 1


Go with the flow & have fun! Else fight the flow
Go to Top of Page

DEfusion
Starting Member

2 Posts

Posted - 2005-03-09 : 03:43:07
That won't work as there is unique index on the counter column and another column - e.g. target table colB & counterCol have a joined unique index. I'm not sure of the proper way to say that.
Go to Top of Page

spirit1
Cybernetic Yak Master

11752 Posts

Posted - 2005-03-09 : 05:53:51
disable the index, update the table and reenable the index.

Go with the flow & have fun! Else fight the flow
Go to Top of Page
   

- Advertisement -