2014-08-30 24 views
0

高手! 我在Access窗體中使用SQL Server鏈接表。在MainTable中,我需要更新和插入記錄,但Access不會讓它更新,它說「此記錄集不可更新」。我知道,這是DISTINCT,但它是必要的TableType記錄 - 我只需要TableTypes中的一個相關的name_ds(甚至首先由npr),結果只是thees 7 MainTable記錄不是16(沒有DISTINCT)。 任何解決方法? 結構簡單 -無法使用DISTINCT更新連接表的主表 - 訪問:此記錄集不可更新

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

數據 - 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:

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,RES ult -

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

回答

0

由於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子句。您可以使用多列嘗試在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)