开发者

Update Multiple Tables with an Especifc Order between them

Ok, maybe is a tricky question, but I know it could happen to some of us. I will try to make it as shorter as possible.

I have 4 Tables, 1 parent table and 3 child tables. (for this example they have the same columns, in my real c开发者_如何学Goase the don't). Like this.

(All the columns in the 4 tables are integer)

PARENT                   
ID  POSITION             
1   1
2   1
3   1
4   1

CHILD1
IDPARENT, POSITION
1            2
1            3
1            4
1            6
2            3
2            4
3            2
3            5

CHILD2
IDPARENT, POSITION
1            5
1            7
2            2
3            3

CHILD3
IDPARENT, POSITION
4            2
4            3
3            6

All the tables have a column named position where this are the rules:

  1. The table PARENT always have position = 1
  2. The position number is a consecutive per parent who cannot be duplicated across all the tables. (i.e the parent 1, have children in child1, child2 and child3, but the position is consecutive 1,2,3,4,5,6,7 it doesnt get repeated across them)
  3. The consecutive could not be consecutive in the same table (i.e. parent 1 has in child 1 the position 2 3 4 6, but the 5 and 7 are in the child 2).
  4. A parent can and cannot have children in all the tables (It could have in child1 and child3 athe same time or just child2 or in no one).
  5. These tables are static, it is always the parent and 3 children.

I have written a query to get the idparent with their respective childrens and position.

SELECT C.IDPARENT,  '1' AS CHILD, c.POSITION
FROM  Child1 c
UNION ALL
SELECT C.IDPARENT,  '2' AS CHILD, c.POSITION
FROM Child2 c
UNION ALL
SELECT C.IDPARENT,  '3' AS CHILD, c.POSITION
FROM Child3 c
UNION ALL 
SELECT P.ID,  '0' AS CHILD,  P.POSITION
FROM PARENT P
ORDER BY IDPARENT, POSITION, CHILD

It gets this output with the information above.

IDPARENT CHILD POSITION
    1   0   1
    1   1   2
    1   1   3
    1   1   4
    1   2   5
    1   1   6
    1   2   7
    2   0   1
    2   2   2
    2   1   3
    2   1   4
    3   0   1
    3   1   2
    3   2   3
    3   1   5
    3   3   6
    4   0   1
    4   3   2
    4   3   3

As you can see, the information get correctly outputed and thats what i want. But there is Bad record here

3   2   3
3   1   5

It jumps from 3 to 5 instead to 4 because sometimes in the table those records get deleted by an outside web application.

So after all of this, this is my problem what refers to the subject of this question.

How could I make a massive update to all those tables, in a proper order, that records like

  3   2   3
  3   1   5
  3   3   6

get converted into

 3   2   3
 3   1   4
 3   3   5

Note: In my example i just did one bad record. In my real case, i got a lot of them.

I have written that query so far, and that's the data that I want to update, but I know you cannot update massive tables, just one by one, but I dont know how to update them with the consecutive order if I just can update one table at time. Because when the next query runs to update the next table, it will have now different data.

Thanks to all of you in advance for reading this long question and for the help provided.


;with cte as
(
  SELECT C.IDPARENT, c.POSITION
  FROM  Child1 c
  UNION ALL
  SELECT C.IDPARENT, c.POSITION
  FROM Child2 c
  UNION ALL
  SELECT C.IDPARENT, c.POSITION
  FROM Child3 c
  UNION ALL 
  SELECT P.ID, P.POSITION
  FROM PARENT P
)
select C.*, row_number() over(partition by IDPARENT order by POSITION) as rn
into #tmp
from cte as C

update C
  set Position = T.rn
from Child1 as C
  inner join #tmp as T
    on C.IDParent = T.IDParent and
       C.Position = T.Position

update C
  set Position = T.rn
from Child2 as C
  inner join #tmp as T
    on C.IDParent = T.IDParent and
       C.Position = T.Position

update C
  set Position = T.rn
from Child3 as C
  inner join #tmp as T
    on C.IDParent = T.IDParent and
       C.Position = T.Position
  
drop table #tmp

Above is the query for you. Here you can try with sample data in table variables. The syntax for the inserts is for SQL Server 2008.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜