| Author |
Topic |
|
pvccaz
Yak Posting Veteran
87 Posts |
Posted - 2007-04-25 : 20:03:14
|
| Hi,I have a table called ClientTbl. I need to update the parentid column in this table. The parent id is the value in the Id column where the Div = 00 for the specific client. For Div = 00 the parent should be NULL.Is it possible to wite an update statement to do this or should i cursor through each records and update the parent id.ClientTableId Client Div Parent1 00003 00 NULL2 00003 50 NULL3 00003 99 NULL4 00004 00 NULL5 00005 00 NULL6 00022 00 NULL7 00025 00 NULL8 00025 01 NULL9 00025 02 NULLOutput should be like this.Id Client Div Parent1 00003 00 NULL2 00003 50 13 00003 99 14 00004 00 NULL5 00005 00 NULL6 00022 00 NULL7 00025 00 NULL8 00025 01 79 00025 02 7Please advise..Thanks! |
|
|
Michael Valentine Jones
Yak DBA Kernel (pronounced Colonel)
7020 Posts |
Posted - 2007-04-25 : 20:18:15
|
| [code]update ClientTableset Parent = a.IDfrom ClientTable join ClientTable a on ClientTable.Client = a.Clientwhere a.Client = '00'[/code]CODO ERGO SUM |
 |
|
|
yumyum113
Starting Member
31 Posts |
Posted - 2007-04-25 : 20:33:27
|
Hi pvccaz,Hope this helps... declare @temptable table ( ID int, Client int, Div int, Parent int)insert into @temptableselect 1, 00003, 00, NULL union allselect 2, 00003, 50, NULL union allselect 3, 00003, 99, NULL union allselect 4, 00004, 00, NULL union allselect 5, 00005, 00, NULL union allselect 6, 00022, 00, NULL union allselect 7, 00025, 00, NULL union allselect 8, 00025, 01, NULL union allselect 9, 00025, 02, NULL update @temptableset parent = t2.idfrom @temptable t1left join (select client,min(id) 'id'from @temptablegroup by clienthaving count(client)>1) t2 on t1.client = t2.client and div<>0 select * from @temptable |
 |
|
|
pvccaz
Yak Posting Veteran
87 Posts |
Posted - 2007-04-25 : 20:36:22
|
| I tried but no rows are getting updated.(0 row(s) affected) |
 |
|
|
pvccaz
Yak Posting Veteran
87 Posts |
Posted - 2007-04-25 : 20:41:28
|
| Hi yumyum113It works.. Thank you...!! |
 |
|
|
pbguy
Constraint Violating Yak Guru
319 Posts |
Posted - 2007-04-26 : 00:11:27
|
| Try this is simpledeclare @tt table (ID int,Client int,Div int,Parent int)insert into @ttselect 1, 00003, 00, NULL union allselect 2, 00003, 50, NULL union allselect 3, 00003, 99, NULL union allselect 4, 00004, 00, NULL union allselect 5, 00005, 00, NULL union allselect 6, 00022, 00, NULL union allselect 7, 00025, 00, NULL union allselect 8, 00025, 01, NULL union allselect 9, 00025, 02, NULL Update tSet parent = a.idfrom @tt t, @tt a where t.client = a.client and t.div<>0 |
 |
|
|
pvccaz
Yak Posting Veteran
87 Posts |
Posted - 2007-04-26 : 14:21:08
|
| Hi pbguy,Thanks so much.. It works fine.. |
 |
|
|
|
|
|