| Author |
Topic |
|
gurusamy.senthil
Yak Posting Veteran
65 Posts |
Posted - 2006-06-23 : 01:28:45
|
| Table StructuresClassid Invtid Seqnbr000001 000045 0000001 000048 0000001 000049 1000001 000051 3000001 000052 2000002 000053 0000002 000055 1....000002 000059 4......Primary key in this table is Invtid. We have to leave the seqnbr which has Zero's, appart from that for repeated classid the seqnbr have to change in order.For eg:-000001 000045 0000001 000048 0000001 000049 1000001 000051 2000001 000052 3The above result is for one group of classid, like that for next group we have to leavethe seqnbr which has 0 and the other seqnbr number have to be in order start from 1...nI have used Cursors to solve the problem and done that.My SP isIF EXISTS (SELECT * FROM SYSOBJECTS WHERE id = object_id('dbo.xttsinvent') and sysstat & 0xf = 4) DROP PROCEDURE xttsinventGOCREATE PROCEDURE xttsinventASDECLARE @Seqnbr int,@ClassID varchar(10)DECLARE @InvIDTmp varchar(30)--SET @SeqNbr = 1DECLARE xttsCurr_Outer CURSORFOR SELECT distinct ClassID FROM xttsinventorytmp WHERE seqnbr <> 0 ORDER BY ClassIDOPEN xttsCurrFETCH NEXT FROM xttsCurr_Outer INTO @ClassIDWHILE @@FETCH_STATUS = 0BEGIN SET @SeqNbr = 1 DECLARE xttsCurr_Inner CURSOR FOR SELECT InvtID FROM xttsinventorytmp WHERE SeqNbr <> 0 and classid = @Classid ORDER BY SeqNbr OPEN xttsCurr_Inner FETCH NEXT FROM xttsCurr_Inner INTO @InvIDTmp WHILE @@FETCH_STATUS = 0 BEGIN UPDATE xtts SET xtts.SeqNbr = @SeqNbr FROM xttsinventorytmp xtts WHERE xtts.Invtid = @InvIDTmp SET @SeqNbr = @SeqNbr + 1 FETCH NEXT FROM xttsCurr_Inner INTO @InvIDTmp END CLOSE xttsCurr_Inner DEALLOCATE xttsCurr_Inner FETCH NEXT FROM xttscurr_Outer INTO @ClassIDEND CLOSE xttscurr_OuterDEALLOCATE xttscurr_OuterGOCan any one tell me is there another method to do without using Cursors. |
|
|
madhivanan
Premature Yak Congratulator
22864 Posts |
|
|
gurusamy.senthil
Yak Posting Veteran
65 Posts |
Posted - 2006-06-23 : 02:28:58
|
| maddy in that update stmt id I use classid as distinct means will that work?.Thats what you would like to explain. Is it so?. |
 |
|
|
CSK
Constraint Violating Yak Guru
489 Posts |
Posted - 2006-06-23 : 02:50:25
|
| Do U Need the Distinct Sequence No for InvtID..? |
 |
|
|
CSK
Constraint Violating Yak Guru
489 Posts |
Posted - 2006-06-23 : 02:52:50
|
| First Take a backupfor that table then try this query and let be backDeclare @SeqNbr intSelect @SeqNbr = 1UPDATE xtts SET xtts.SeqNbr = @SeqNbr ,@SeqNbr = @SeqNbr + 1FROM xttsinventorytmp xtts WHERE xtts.SeqNbr <>0-- KK |
 |
|
|
gurusamy.senthil
Yak Posting Veteran
65 Posts |
Posted - 2006-06-23 : 04:09:56
|
| No distinct number for invtid.suppose the below is the case meansClassID InvtID SeqNbr000001 000045 0000001 000048 0000001 000049 1000001 000051 3000001 000052 2000002 000053 0000002 000055 1000002 000056 4The result has to be in form000001 000045 0000001 000048 0000001 000049 1000001 000051 2000001 000052 3000002 000053 0000002 000055 1000002 000056 2....like wise for each classid.I think now u will get me. |
 |
|
|
khtan
In (Som, Ni, Yak)
17689 Posts |
Posted - 2006-06-23 : 05:11:15
|
[code]declare @test table (ClassID varchar(30), InvtID varchar(10), SeqNbr int)insert @testselect '000001', '000045', 0 union allselect '000001', '000048', 0 union allselect '000001', '000049', 1 union allselect '000001', '000051', 3 union allselect '000001', '000052', 2 union allselect '000002', '000053', 0 union allselect '000002', '000055', 1 union allselect '000002', '000056', 4update t set SeqNbr = (select count(*) from @test x where x.ClassID = t.ClassID and x.SeqNbr <> 0 and x.InvtID <= t.InvtID)from @test twhere t.SeqNbr <> 0select * from @test/* RESULT :ClassID InvtID SeqNbr ------------------------------ ---------- ----------- 000001 000045 0000001 000048 0000001 000049 1000001 000051 2000001 000052 3000002 000053 0000002 000055 1000002 000056 2*/[/code] KH |
 |
|
|
gurusamy.senthil
Yak Posting Veteran
65 Posts |
Posted - 2006-06-23 : 05:36:39
|
| Thanks khtan,This is the second time you came up an nice solution for me.In my previous topic I asked you to explain the logic of the program, Ihave understood bit but not clear.If you don't please explain this programming logic of how it works.What is happenning inside the bracket i.e, (select count(*)..)Will the program check the everyline to update or it will update the entire in fetch.Hope you will help me in this... |
 |
|
|
khtan
In (Som, Ni, Yak)
17689 Posts |
Posted - 2006-06-23 : 05:45:13
|
Sure. Here it isupdate t set SeqNbr = (select count(*) from @test x -- Count the number of records where x.ClassID = t.ClassID -- for each group of ClassID and x.SeqNbr <> 0 -- ignoring those with SeqNbr = 0 and x.InvtID <= t.InvtID) -- for InvtID less or equalfrom @test twhere t.SeqNbr <> 0 -- your criteria of ignore those with SeqNbr = 0/* RESULT :ClassID InvtID SeqNbr ------------------------------ ---------- ----------- 000001 000045 0 ignored000001 000048 0 ignored000001 000049 1 counted 1 record (000049)000001 000051 2 counted 2 records (000049 & 000051)000001 000052 3 counted 3 records (000049 , 000051 & 000052)000002 000053 0 ignored000002 000055 1 counted 1 record (000055)000002 000056 2 counted 2 records (000055 & 000056)*/ KH |
 |
|
|
madhivanan
Premature Yak Congratulator
22864 Posts |
Posted - 2006-06-23 : 05:56:08
|
| The reason of why I want you to do this in front end application is that for each row count is calculated. If you run that query against a table having millions of rows it may take hours to number themMadhivananFailing to plan is Planning to fail |
 |
|
|
gurusamy.senthil
Yak Posting Veteran
65 Posts |
Posted - 2006-06-23 : 06:07:53
|
| Thanks khtan, no need to explain the logic I have understood by reading once again and what there in logic.Maddy you are right, but I cannot do that front end application. B'coz for entering values in front end app means what you are telling will definetly get solution. But the record was already created and if we enter there might some duplicates and we cannot know what exactly is going. so that I created sp to check and change the value.Thanks for all especially khtan |
 |
|
|
|