| Author |
Topic |
|
Delinda
Constraint Violating Yak Guru
315 Posts |
Posted - 2010-01-10 : 10:07:25
|
| My AutoNo table as follow,tAutoNoPref | cYear | cMonth | No-----------------------------------_R | 2010 | 01 | 1*No is inttRouteTrnxID | RID-------------------1 | _r000102 | _r000063 | _r000244 | _r000305 | _r000446 | _r00047*TrnxID is int and a primary key*RID is varchar(7) and unique1. I want RID value in table tRoute will be replaced with New RID value2. The new RID value taken from tAutoNo3. The new RID value is a 8 character, combination of Pref+00000+No 4. Every No is taken from tAutoNo, No is increase by 15. Example of new RID value is _R000001, _R000002, _R000003, ...., _R000009, _R000010, _R000011, and so onSo, the result as follow,BeforetRouteTrnxID | RID-------------------1 | _r000102 | _r000063 | _r000244 | _r000305 | _r000446 | _r00047AftertRouteTrnxID | RID-------------------1 | _R0000012 | _R0000023 | _R0000034 | _R0000045 | _R0000056 | _R000006tAutoNoPref | cYear | cMonth | No-----------------------------------_R | 2010 | 01 | 6I believe T-SQL have while and update.Anyone can show me how T-SQL looks like? |
|
|
sodeep
Master Smack Fu Yak Hacker
7174 Posts |
Posted - 2010-01-10 : 10:36:53
|
| [code]Update TRSet TR.RID = TA.Pref + '00000' + Cast(No as Varchar(4))from TRoute TRinner join tAutoNo TA On TA.No = TR.TrnxID[/code] |
 |
|
|
Delinda
Constraint Violating Yak Guru
315 Posts |
Posted - 2010-01-10 : 11:47:00
|
quote: Originally posted by sodeep
Update TRSet TR.RID = TA.Pref + '00000' + Cast(No as Varchar(4))from TRoute TRinner join tAutoNo TA On TA.No = TR.TrnxID
The result, there's no looping. Also No in tAutoNo not increase by 1 after each updated |
 |
|
|
tkizer
Almighty SQL Goddess
38200 Posts |
|
|
Delinda
Constraint Violating Yak Guru
315 Posts |
|
|
tkizer
Almighty SQL Goddess
38200 Posts |
|
|
Delinda
Constraint Violating Yak Guru
315 Posts |
|
|
sodeep
Master Smack Fu Yak Hacker
7174 Posts |
Posted - 2010-01-10 : 19:24:38
|
| [code]Declare @T table(TrnxID int, RID Varchar(40))Insert @TSelect 1 ,'_r00010' union allSelect 2 ,'_r00006' union allSelect 3 ,'_r00024' union allSelect 4 ,'_r00030' union allSelect 5 ,'_r00044' union allSelect 6 ,'_r00047'Update TSet T.RID = '_R' + '00000' + Cast(T.TrnXID+44 as Varchar(10))from @T Tinner join @T M on M.TrnXID = T.TrnXIDSelect * from @T[/code] |
 |
|
|
Delinda
Constraint Violating Yak Guru
315 Posts |
Posted - 2010-01-10 : 21:17:51
|
| mr tkizer and mr sodeep, both of you are great. |
 |
|
|
tkizer
Almighty SQL Goddess
38200 Posts |
|
|
|