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.
| Author |
Topic |
|
jung1975
Aged Yak Warrior
503 Posts |
Posted - 2004-07-21 : 11:39:42
|
I have a table looks likeM_id servicedate c_id d_id 1001 03/24/04 34 561001 03/24/04 44 771001 04/30/04 56 891002 05/01/04 33 561003 05/23/04 11 121003 05/23/04 30 30 I would like to insert column called status ID ( @@identity) group by m_id and servicedate using a cursor.The output should looks like:M_id servicedate c_id d_id status_id1001 03/24/04 34 56 11001 03/24/04 44 77 11001 04/30/04 56 89 21002 05/01/04 33 56 31003 05/23/04 11 12 41003 05/23/04 30 30 4 |
|
|
rockmoose
SQL Natt Alfen
3279 Posts |
Posted - 2004-07-21 : 13:35:26
|
| I think that you are thinking wrongly about the @@Identity.( an identity column cannot contain duplicates )Anyway, I hope this might help:set nocount oncreate table #serv(M_id int,servicedate datetime,c_id int,d_id int)insert #serv(M_id,servicedate,c_id,d_id)select 1001,'03/24/04',34,56union all select 1001,'03/24/04',44,77union all select 1001,'04/30/04',56,89union all select 1002,'05/01/04',33,56union all select 1003,'05/23/04',11,12union all select 1003,'05/23/04',30,30print 'identity column is useless'alter table #serv add status_id int identityselect * from #servalter table #serv drop column status_idprint 'add ordinary int column and update... 'alter table #serv add status_id intgo-- set based update using temporary tablecreate table #sequence(id int identity,M_id int,servicedate datetime,primary key clustered(M_id,servicedate))insert #sequence(M_id,servicedate)select M_id,servicedate from #serv group by M_id,servicedate order by M_id,servicedateupdate #serv set status_id = idfrom #sequence where #serv.M_id = #sequence.M_id and #serv.servicedate = #sequence.servicedatedrop table #sequenceselect * from #servdrop table #serv--/rockmoose |
 |
|
|
rockmoose
SQL Natt Alfen
3279 Posts |
Posted - 2004-07-21 : 13:37:24
|
| Oh, Sorry about the Cursor..It is always better to work with sets. Cursors are muuucccchhhh slower./rockmoose |
 |
|
|
Kristen
Test
22859 Posts |
Posted - 2004-07-21 : 14:30:14
|
"an identity column cannot contain duplicates"Shouldn't happen, but can happen. Best to put a unique index on it to stop it happening.Kristencreate table MyTempTable( MyID int identity(1,1), MyStuff varchar(10))INSERT INTO MyTempTable(MyStuff)SELECT 'FOO1' UNION ALLSELECT 'BAR2'GOSELECT * FROM MyTempTableGOSET IDENTITY_INSERT MyTempTable ONGOINSERT INTO MyTempTable(MyID, MyStuff)SELECT 4, 'FOO4' UNION ALLSELECT 5, 'BAR5'SET IDENTITY_INSERT MyTempTable OFFGOSELECT * FROM MyTempTable ORDER BY MyIDGO-- 1,2,4,5DBCC CHECKIDENT (MyTempTable, RESEED, 3)GOINSERT INTO MyTempTable(MyStuff)SELECT 'FOO3' UNION ALLSELECT 'BAR4'GOSELECT * FROM MyTempTable ORDER BY MyIDGO-- 1,2,4,4,5,5 - Oh Dear!!DROP TABLE MyTempTableGO Kristen |
 |
|
|
rockmoose
SQL Natt Alfen
3279 Posts |
Posted - 2004-07-21 : 14:44:35
|
| Sorry my mistake, ignore erroneus info jung1975.Gosh Kristen !, You must be doing some very evil stuff to your databases.DBCC CHECKIDENT (MyTempTable, RESEED, 3) thats just wonderful, lets see.., used it last week I think ;-)Happy Sqling/rockmoose |
 |
|
|
Kristen
Test
22859 Posts |
Posted - 2004-07-21 : 15:33:04
|
| Indeeie, that WON'T happen - except in evil circumstances - but SQL can take a dive and create the same effect (well, that certainly used to be true - maybe MS have absolutely and for definitely fixed it in SQL2K SPnn!) IMO Its best to have a Unique Constraint on it and then if SQL does do a Tripple Toe Salco at least it will go Splat! rather than squirting an erroneous value into your table.Kristen |
 |
|
|
rockmoose
SQL Natt Alfen
3279 Posts |
Posted - 2004-07-21 : 15:45:18
|
| Thanks for the enlightnment.Actually 100% of the times I have used an Identity in a table it has also been the PK, and 95% of the times Clustered.I have a hard time figuring out why one would want an Identity that is not PK, (UIX)./rockmoose |
 |
|
|
Kristen
Test
22859 Posts |
Posted - 2004-07-21 : 16:28:41
|
| That's easy - afterthought! (as in "I wish I'd used an identity ...)Kristen |
 |
|
|
|
|
|
|
|