Author |
Topic |
tocroi72
Yak Posting Veteran
89 Posts |
Posted - 2006-08-01 : 17:00:58
|
Hi, i need help with the query below. Please advice if you can. Create table #master(claim_num varchar(10),PROC_CODE varchar(2),SERVICE_LINE_NUM int)insert into #master (claim_num,PROC_CODE) values('0000','AA')insert into #master (claim_num,PROC_CODE) values('0000','BB')insert into #master (claim_num,PROC_CODE) values('1111','BB')insert into #master (claim_num,PROC_CODE) values('1111','AA')insert into #master (claim_num,PROC_CODE) values('1111','CC')i like to update the service_line_num in the #master table so the result would look like this:0000 AA 10000 BB 21111 BB 11111 AA 21111 CC 3Below is my query, it works perfectly . However, if i have more than 10000 records in #master table. The query would run forever. I wonder if there're a better way to handle this.THANK you for your helps.--------------------------------DECLARE @claimnumber varchar(12)DECLARE @RowCnt intDECLARE @i intDECLARE @proc_code varchar(12)SELECT * into #tmp2 from #master where 1=2select distinct Claim_num into #tmp from #master SELECT TOP 1 @claimnumber = Claim_num from #tmpSET @RowCnt = @@ROWCOUNTWHILE @RowCnt <> 0beginSET @i = 0insert into #tmp2 select * from #master where Claim_num = @claimnumberselect top 1 @claimnumber = Claim_num, @proc_code = proc_code from #tmp2set @rowcnt = @@rowcountwhile @rowcnt <> 0 BEGIN set @i = @i + 1 update #master set service_line_num = @i where Claim_num = @claimnumber and proc_code = @proc_code delete #tmp2 where Claim_num = @claimnumber and proc_code = @proc_code select top 1 @claimnumber = Claim_num, @proc_code = proc_code from #tmp2 set @rowcnt = @@rowcount end DELETE #tmp where Claim_num = @claimnumberSELECT TOP 1 @claimnumber = Claim_num from #tmpset @rowcnt = @@rowcountend |
|
timmy
Master Smack Fu Yak Hacker
1242 Posts |
Posted - 2006-08-01 : 18:00:59
|
How are you creating your #master table? There is a simple way around it using a sub-query, but it means that your SERVICE_LINE_NUM values will be numbered in the alphabetical order of PROC_CODE. Can you create your #master table with an identity field, so we can tell in which order the lines were written? |
 |
|
SwePeso
Patron Saint of Lost Yaks
30421 Posts |
Posted - 2006-08-01 : 18:20:54
|
[code]-- prepare test datadeclare @master table (claim_num varchar(10), proc_code varchar(2), service_line_num int)insert @master (claim_num, proc_code)select '0000', 'AA' union allselect '0000', 'BB' union allselect '1111', 'BB' union allselect '1111', 'AA' union allselect '1111', 'CC'-- do the workupdate mset m.service_line_num = (select count(*) from @master z where z.claim_num = m.claim_num and z.proc_code <= m.proc_code)from @master m-- show the dataselect * from @master[/code]Peter LarssonHelsingborg, Sweden |
 |
|
jsmith8858
Dr. Cross Join
7423 Posts |
Posted - 2006-08-01 : 18:45:17
|
Are you using SQL 2005? If so, then there is an easy way to do this.- Jeff |
 |
|
timmy
Master Smack Fu Yak Hacker
1242 Posts |
Posted - 2006-08-01 : 18:51:38
|
Peter, That's what I came up with before I realised that the sequences are out (i.e. the service_line_num is sequenced in the order of insert, not the order of the PROC_CODE column). This is why I asked the question.Tim |
 |
|
SwePeso
Patron Saint of Lost Yaks
30421 Posts |
Posted - 2006-08-02 : 01:22:17
|
quote: Originally posted by timmy Peter, That's what I came up with before I realised that the sequences are out (i.e. the service_line_num is sequenced in the order of insert, not the order of the PROC_CODE column). This is why I asked the question.Tim
Yes, I read your question. But I had to post it, if tocroi72 just made a typo of the ranking.Peter LarssonHelsingborg, Sweden |
 |
|
tocroi72
Yak Posting Veteran
89 Posts |
Posted - 2006-08-02 : 11:44:45
|
Thank Peso so much for the quick query - you have save me 10 hours of running my ugly codes. |
 |
|
SwePeso
Patron Saint of Lost Yaks
30421 Posts |
Posted - 2006-08-02 : 13:39:11
|
You're welcome.Thanks for the feedback.BTW, how long time ran the update query I posted?Peter LarssonHelsingborg, Sweden |
 |
|
tocroi72
Yak Posting Veteran
89 Posts |
Posted - 2006-08-02 : 16:04:35
|
it is very fast - i updated 34827 records and it only runs for 2 seconds.Thanks again. |
 |
|
|