【问题标题】:How to update the table records which are dependent on another table如何更新依赖于另一个表的表记录
【发布时间】:2012-12-13 14:30:51
【问题描述】:

我只是坚持更新下表列。考虑下面的脚本。

declare @Table1 Table  ( ID int Identity(1,1), UserCount int )
insert into @Table1 (UserCount) values (2),(3),(5)
declare @Table2 Table ( ID int Identity(1,1), Name varchar(10), IDRef int null)
insert into @Table2 (Name) values ('p1'),('p2'),('p3'),('p4'),('p5'),('p6'),('p7'),('p8'),('p9'),('p10')

结果是

从@Table1 中选择 *

ID          UserCount
----------- -----------
1           2
2           3
3           5

从@Table2 中选择 *

ID          Name       IDRef
----------- ---------- -----------
1           p1         NULL
2           p2         NULL
3           p3         NULL
4           p4         NULL
5           p5         NULL
6           p6         NULL
7           p7         NULL
8           p8         NULL
9           p9         NULL
10          p10        NULL

基于@Table1.UserCount 值,我需要将Table2.IDRef 值更新为@Table1.ID

预期结果是,

ID          Name       IDRef
----------- ---------- -----------
1           p1         1
2           p2         1
3           p3         2
4           p4         2
5           p5         2
6           p6         3
7           p7         3
8           p8         3
9           p9         3
10          p10        3

【问题讨论】:

  • @Swani 你和那些表有关系吗?或者您正在尝试将 IDREF 作为外键?
  • 没有没有关系,逻辑是 1. sum(table1.UserCount) = table2 row count 2. 从表1中选择第一条记录,获取第一条记录UserCount,用first '更新table2带有 table1.id 的 UserCount'rows 3. 从表 1 中选择第二条记录并获取第二条记录 UserCount 并使用带有 table1.id 的下一个 'UserCount' 行更新 table2

标签: sql


【解决方案1】:

SQL Fiddle

查询:

declare @Table1 Table  ( ID int Identity(1,1), UserCount int )
insert into @Table1 (UserCount) values (2),(3),(5)
declare @Table2 Table ( ID int Identity(1,1), Name varchar(10), IDRef int null)
insert into @Table2 (Name) values ('p1'),('p2'),('p3'),('p4'),('p5'),('p6'),('p7'),('p8'),('p9'),('p10')

update t2
set t2.idref = t1.id
from (
    select *, rn=row_number() over (order by id)
      from @table1 t1
      join master..spt_values v on v.type='p'
                               and v.number between 1 and t1.UserCount
) t1
join (select *, rn=row_number() over (order by id)
        from @table2
) t2 on t1.rn=t2.rn

select * from @Table2
order by id

Results

| ID | NAME | IDREF |
---------------------
|  1 |   p1 |     1 |
|  2 |   p2 |     1 |
|  3 |   p3 |     2 |
|  4 |   p4 |     2 |
|  5 |   p5 |     2 |
|  6 |   p6 |     3 |
|  7 |   p7 |     3 |
|  8 |   p8 |     3 |
|  9 |   p9 |     3 |
| 10 |  p10 |     3 |

【讨论】:

    【解决方案2】:

    你可以这样做:

    ;WITH Digits
    AS
    (
      SELECT n
      FROM (VALUES(1), (2), (3), (4), (5), (6), (7), (8), (9), (10)) d(n)
    ), TableRefs
    AS
    (
      SELECT 
        ID, 
        RefId = d.n,
        UserCount,
        ROW_NUMBER() OVER(ORDER BY(SELECT 1)) rownum
      FROM @table1 
      INNER JOIN Digits d ON n <= usercount
    )
    UPDATE t2
    SET IDRef = tref.ID
    FROM @table2 t2
    INNER JOIN TableRefs tref ON t2.Id = tref.rownum;
    

    SQL Fiddle Demo

    这将使@table2 像这样:

    | ID | NAME | IDREF |
    ---------------------
    |  1 |   p1 |     1 |
    |  2 |   p2 |     1 |
    |  3 |   p3 |     2 |
    |  4 |   p4 |     2 |
    |  5 |   p5 |     2 |
    |  6 |   p6 |     3 |
    |  7 |   p7 |     3 |
    |  8 |   p8 |     3 |
    |  9 |   p9 |     3 |
    | 10 |  p10 |     3 |
    

    【讨论】:

      猜你喜欢
      • 2022-01-10
      • 1970-01-01
      • 2018-08-09
      • 1970-01-01
      • 2018-06-30
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多