Sql 无法使用DISTINCT-Access使用联接表更新主表:此记录集不可更新

Sql 无法使用DISTINCT-Access使用联接表更新主表:此记录集不可更新,sql,sql-server,ms-access-2010,Sql,Sql Server,Ms Access 2010,大师! 我正在Access窗体中使用SQL Server链接表。在MainTable中,我需要更新和插入记录,但Access不允许更新,因为更新时它会说此记录集不可更新。我知道,这可能是不同的,但对于TableType记录来说是必要的-我只需要TableType中的一个相关名称,即使是npr最先使用的名称,结果是只有7个主表记录,而不是16个没有不同名称的记录。 有解决办法吗? 结构简单- MainTable: id, npr, name, type, datasource_fk. TableD

大师! 我正在Access窗体中使用SQL Server链接表。在MainTable中,我需要更新和插入记录,但Access不允许更新,因为更新时它会说此记录集不可更新。我知道,这可能是不同的,但对于TableType记录来说是必要的-我只需要TableType中的一个相关名称,即使是npr最先使用的名称,结果是只有7个主表记录,而不是16个没有不同名称的记录。 有解决办法吗? 结构简单-

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
表格:

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

由于MainTable npr列与TableType npr列多次匹配,您的联接将获得16个匹配项

1;12;"Olie";"percentage";1
匹配

7;12;"City1";"t"
8;12;"City1";"n"
9;12;"State1";"r"
1;12;"City1";"w"
最好是对列TableType.somethingtype使用where子句。您可以尝试使用多列对Tables和TableType进行左联接,但实际上,您可能需要调整数据。换句话说,禁用某些行。以下查询将显示您面临的问题:

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)