|
sahu74
Posting Yak Master
100 Posts |
Posted - 2004-09-17 : 09:53:41
|
| [code]I have 4 tables with the following structure:Tbl1( Tbl1_ID, DeptID, StartYear, EndYear)Tbl2( Tbl2_ID, Tbl1_ID, StartYear, EndYear)Tbl3( Tbl3_ID, Tbl2_ID, StartYear, EndYear)Tbl3_Term (Tbl3_ID, TermID, Value)[/code]I am adding a new column to Tbl1, Tbl2 and Tbl3. The modified structure is:Tbl1( Tbl1_ID, DeptID, StartYear, EndYear, OrderNo) **Tbl1_ID is the primary key; DeptId is the foreign keyTbl2( Tbl2_ID, Tbl1_ID, StartYear, EndYear, OrderNo) **Tbl2_ID is the primary key; Tbl1_ID is the foreign keyTbl3( Tbl3_ID, Tbl2_ID, StartYear, EndYear, OrderNo) **Tbl3_ID is the primary key; Tbl2_ID is the foreign keyTbl3_Term (Tbl3_ID, TermID, Value) **Tbl1_ID and TermID together are the primary key; Tbl3_ID is the foreign keyI would like to group everything first based on the termID. Then I want to update the new column in Tbl1, Tbl2 and Tbl3 based on (group by) the foreign key in each table (i.e., DeptID for Tbl1, Tbl1_ID for Tbl2 and Tbl2_ID for Tbl3). For all the records associated with DeptID 'x' in Tbl1, I want the OrderNo to be updated as 1, 2, 3, 4,...For the next DeptID (say 'z') in Tbl1 the OrderNo will start with 1, 2, 3 ... again.For Tbl2, the OrderNo for all the records associated with Tbl1_ID should be 1, 2, 3, ....and the same way goes on for each Tbl1_ID in Tbl2.I donot need to update the OrderNo in Tbl1, Tbl2 or Tbl3 if the EndYear value corresponding to that record is < Current year (2004)Here is some sample data.************************[code]Tbl1 Tbl1_ID DeptID StartYear EndYear OrderNo1 z 2001 20052 x 2002 20053 x 2002 20054 z 2002 20055 z 2002 20056 x 2002 2005--------------------------------------------------------Tbl2Tbl2_ID Tbl1_ID StartYear EndYear OrderNo1 1 2001 20052 1 2001 20053 2 2002 20054 3 2002 20055 4 2002 20056 4 2002 20057 3 2002 2003--------------------------------------------------------Tbl3Tbl3_ID Tbl2_ID StartYear EndYear OrderNo1 1 2002 2005 2 3 2002 2005 3 3 2002 2005 4 2 2002 2005 5 4 2002 2005 6 3 2002 2003 7 4 2002 2005 8 4 2002 2005 9 6 2002 2005 10 5 2002 2005 11 6 2002 2002 12 6 2002 2005 ------------------------------------------------------Tbl3_TermTbl3_ID TermID Value------- ------ -----1 1 121 2 142 1 102 3 173 2 22******************************************************Tables after update******************************************************Tbl1 Tbl1_ID DeptID StartYear EndYear OrderNo1 z 2001 2005 12 x 2002 2005 13 x 2002 2005 24 x 2002 2005 35 z 2002 2005 26 z 2002 2005 3------------------------------------------------------------------------Tbl2Tbl2_ID Tbl1_ID StartYear EndYear OrderNo1 1 2001 2005 12 1 2001 2005 23 2 2002 2005 14 3 2002 2005 15 4 2002 2005 16 4 2002 2005 27 3 2002 2003 -------------------------------------------------------------------------Tbl3Tbl3_ID Tbl2_ID StartYear EndYear OrderNo1 1 2002 2005 12 3 2002 2005 13 3 2002 2005 24 2 2002 2005 15 4 2002 2005 16 3 2002 2003 -7 4 2002 2005 28 4 2002 2005 39 6 2002 2005 110 5 2002 2005 111 6 2002 2002 -12 6 2002 2005 2------------------------------------------------------Tbl3_TermTbl3_ID TermID Value------- ------ -----1 1 121 2 142 1 10k2 3 173 2 22Any help will be greatly appreciated.Thank you.PKS.[/code] |
|
|
spirit1
Cybernetic Yak Master
11752 Posts |
Posted - 2004-09-17 : 18:26:04
|
this is for increment updateing declare @counter intset @counter = 1UPDATE Tbl1_ID SET @counter = OrderNo = @counter + 1Where deptid='z'you will need to play with this to suit your needs. if nothing else, write multiple updates for each table.Cursors are a big NO, NO.Go with the flow & have fun! Else fight the flow |
 |
|