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 |
|
monfu
Yak Posting Veteran
81 Posts |
Posted - 2008-05-20 : 06:55:08
|
| Dear AllI have a stored proc that is taking way too long to process, over 50 minutes.What I need to do is go through 180K rows, and if the projId and langid is the same, increment 1 to a value, if not, reset the incrementerI am doing the following at the momentDECLARE @Id int, @ProjectId int, @LangCode char(6)DECLARE @CurProjectId int, @CurLangCode char(6) DECLARE @incrementer intDECLARE Order_cursor CURSOR FORSELECT Id, langCode, projectid FROM #tempTable OPEN Order_cursorSET @incrementer = 1FETCH NEXT FROM Order_cursor INTO @Id, @LangCode, @ProjectIdWHILE @@FETCH_STATUS = 0BEGIN IF (@CurProjectId = @ProjectId) AND (@CurLangCode = @LangCode) BEGIN SET @incrementer = @incrementer + 1 END ELSE BEGIN SET @incrementer = 1 END UPDATE #tempTable SET edbOrder = @incrementer WHERE Id = @id SET @CurProjectId = @ProjectId SET @CurLangCode = @LangCode FETCH NEXT FROM Order_cursor INTO @Id, @LangCode, @ProjectIdENDCLOSE Order_cursorDEALLOCATE Order_cursorIs there a better way?ThanksJohann |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2008-05-20 : 07:30:53
|
It seems like you want only this:-UPDATE tSET t.edbOrder = t.RowNoFROM(SELECT ROW_NUMBER() OVER(PARTITION BY langCode, projectid ORDER BY langCode) AS RowNo,*FROM #tempTable)t |
 |
|
|
SwePeso
Patron Saint of Lost Yaks
30421 Posts |
Posted - 2008-05-20 : 07:53:19
|
TryORDER BY IDinstead. It seems like ID is identity column.This suggestion only works if ALL LangCodes are in sequence. Otherwise it will fail.If LangCode is not sequenced, you will have to use the UPDATE @variable = column approach.Post table layout with existing indexes and some 10-20 sample data that really reflects real world scenario. E 12°55'05.25"N 56°04'39.16" |
 |
|
|
|
|
|
|
|