简体   繁体   English

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

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

gurus! 大师! I'm using SQL Server linked tables in Access Forms. 我在Access Forms中使用SQL Server链接表。 In MainTable I need to update and insert records, but Access won't let it, for update it says "This Recordset is not updateable". 在MainTable中,我需要更新和插入记录,但是Access不允许它进行更新,因为更新内容为“此Recordset不可更新”。 I know, it's couse DISTINCT, but it's necessary for TableType records - I need only one related name_ds from TableTypes (even first by npr) and in result just thees 7 MainTable records not 16 (without DISTINCT). 我知道,这是DISTINCT的原因,但是TableType记录是必需的-我只需要一个来自TableTypes的相关name_ds(甚至首先是npr),结果就是7个MainTable记录而不是16个(没有DISTINCT)。 Any workarounds? 任何解决方法? Simple structure - 结构简单-

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

Data - MainTable: 数据-主表:

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

TableDS: 表格DS:

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

TableType: 表格类型:

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, result - 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             |
---------------------------------------------------------------------------------------------------------------------------------------------

You are getting 16 matches on your joins because MainTable npr column matches multiple times with TableType npr column. 您在联接上获得16个匹配项,因为MainTable npr列与TableType npr列多次匹配。

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

Matches to 符合

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

Your best bet is to use a where clause for column TableType.somethingtype. 最好的选择是对TableType.somethingtype列使用where子句。 You can try LEFT JOIN on TableDS and TableType using multiple columns but really, you may need to adjust your data. 您可以使用多个列在TableDS和TableType上尝试LEFT JOIN,但实际上,您可能需要调整数据。 In other words, inactivate some rows. 换句话说,停用某些行。 The following query will show you what you're up against: 以下查询将向您显示您要面对的问题:

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

So, after you figure out your data. 因此,找出数据后。 Then you may be able to do something like: 然后,您可以执行以下操作:

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