繁体   English   中英

无法使用DISTINCT使用连接的表更新主表-访问:此记录集不可更新

[英]Can't update main table with joined tables using DISTINCT - Access: This recordset is not updatable

大师! 我在Access Forms中使用SQL Server链接表。 在MainTable中,我需要更新和插入记录,但是Access不允许它进行更新,因为更新内容为“此Recordset不可更新”。 我知道,这是DISTINCT的原因,但是TableType记录是必需的-我只需要一个来自TableTypes的相关name_ds(甚至首先是npr),结果就是7个MainTable记录而不是16个(没有DISTINCT)。 任何解决方法? 结构简单-

MainTable: id, npr, name, type, datasource_fk.
TableDS: id, name_ds, something. 
TableType: id, npr, name_type, something_type.

数据-主表:

1;12;"Olie";"percentage";1
2;15;"Tol";"count";2
3;13;"Opp";"percentage";1
4;12;"Hypq";"count";3
5;14;"Gete";"count";1
6;;"Mour";"count";2
7;;"Ellt";"percentage";3

表格DS:

1;"City1";"q"
2;"City2";"a"
3;"State1";"z"
4;"State2";"x"

表格类型:

1;12;"City1";"w"
2;15;"City1";"s"
3;13;"City1";"x"
4;14;"City2";"w"
5;14;"City1";"s"
6;13;"City3";"p"
7;12;"City1";"t"
8;12;"City1";"n"
9;12;"State1";"r"
10;15;"State1";"r"

SQL,结果-

SELECT DISTINCT t3.npr AS npr_type, t1.npr, t1.id, t1.name, t2.name_ds, t1.datasource_fk, t1.types
FROM (MainTable AS t1 LEFT JOIN TableDS AS t2 ON t1.datasource_fk = t2.id) LEFT JOIN TableType AS t3 ON t1.npr = t3.npr;

---------------------------------------------------------------------------------------------------------------------------------------------
|     npr_type      |        npr        |        id         |       name        |      name_ds      |   datasource_fk   |       types       |
---------------------------------------------------------------------------------------------------------------------------------------------
|                   |                   |                 6 | Mour              | City2             |                 2 | count             |
---------------------------------------------------------------------------------------------------------------------------------------------
|                   |                   |                 7 | Ellt              | State1            |                 3 | percentage        |
---------------------------------------------------------------------------------------------------------------------------------------------
|                12 |                12 |                 1 | Olie              | City1             |                 1 | percentage        |
---------------------------------------------------------------------------------------------------------------------------------------------
|                12 |                12 |                 4 | Hypq              | State1            |                 3 | count             |
---------------------------------------------------------------------------------------------------------------------------------------------
|                13 |                13 |                 3 | Opp               | City1             |                 1 | percentage        |
---------------------------------------------------------------------------------------------------------------------------------------------
|                14 |                14 |                 5 | Gete              | City1             |                 1 | count             |
---------------------------------------------------------------------------------------------------------------------------------------------
|                15 |                15 |                 2 | Tol               | City2             |                 2 | count             |
---------------------------------------------------------------------------------------------------------------------------------------------

您在联接上获得16个匹配项,因为MainTable npr列与TableType npr列多次匹配。

1;12;"Olie";"percentage";1

符合

7;12;"City1";"t"
8;12;"City1";"n"
9;12;"State1";"r"
1;12;"City1";"w"

最好的选择是对TableType.somethingtype列使用where子句。 您可以使用多个列在TableDS和TableType上尝试LEFT JOIN,但实际上,您可能需要调整数据。 换句话说,停用某些行。 以下查询将向您显示您要面对的问题:

SELECT t3.npr AS npr_type, 
        t1.npr, 
        t1.id, 
        t1.name, 
        t2.name_ds, 
        t1.datasource_fk, 
        t1.type,
        t3.something_type
FROM #MainTable t1 
      LEFT JOIN #TableDS AS t2 
      ON t1.datasource_fk = t2.id
      LEFT JOIN #TableType AS t3 
      ON t1.npr = t3.npr
ORDER BY t3.npr, 
        t1.npr, 
        t1.id, 
        t1.name, 
        t2.name_ds, 
        t1.datasource_fk, 
        t1.type,
        t3.something_type

因此,找出数据后。 然后,您可以执行以下操作:

SELECT t3.npr AS npr_type, 
        t1.npr, 
        t1.id, 
        t1.name, 
        t2.name_ds, 
        t1.datasource_fk, 
        t1.type,
        t3.something_type
FROM #MainTable t1 
      LEFT JOIN #TableDS AS t2 
      ON t1.datasource_fk = t2.id
      LEFT JOIN #TableType AS t3 
      ON t1.npr = t3.npr
WHERE
    (t1.npr = 12 AND t3.something_type = 'n')   
    OR
    (t1.npr = 14 AND t3.something_type = 's')
    OR
    (t1.npr = 13 AND t3.something_type = 'p')
    OR
    (t1.npr = 15 AND t3.something_type = 's')
    OR
    (t1.npr IS NULL)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM