MS Access和SSMS从完全相同的查询返回不同的记录数

时间:2015-10-05 23:24:24

标签: sql sql-server ms-access ssms

在我疯了之前请帮助我。我正在使用SSMS 2008 R2和MS Access 2010。

我正在从某个构建的Access数据库中获取查询,并将它们放入SSMS,最终成为存储过程。在大多数情况下,这只是修复Access的语法特性而已完成的问题。但有些人总是给我不同的结果,我无法确定原因。这是一个这样的查询。这是SSMS版本。

SELECT DISTINCT
 a.ACCOUNT_NUMBER
,a.INVESTOR_CODE
,a.APPLICATION_PROCESS_STATUS
,i.DOCUMENT_INTAKE_START_DATE
,a.APPLICATION_TYPE_DESC
,p.DECISION_STATUS_DESC
,p.DECISION_STATUS_DATE
,p.DECISION_PROCESS_STATUS
,a.INVESTOR_NAME
FROM        V_Application_Process a
LEFT JOIN   V_Document_Intake_Process i ON a.APPLICATION_ID = i.APPLICATION_ID
LEFT JOIN   V_Decision_Process p ON a.APPLICATION_ID = p.APPLICATION_ID

当我在SSMS中运行时,我得到 107614 结果。当我在Access中运行它时,我得到 90848 。我将粘贴查询的Access版本:

SELECT DISTINCT
 dbo_V_Application_Process.ACCOUNT_NUMBER
,dbo_V_Application_Process.INVESTOR_CODE
,dbo_V_Application_Process.APPLICATION_PROCESS_STATUS
,dbo_V_Document_Intake_Process.DOCUMENT_INTAKE_START_DATE
,dbo_V_Application_Process.APPLICATION_TYPE_DESC
,dbo_V_Decision_Process.DECISION_STATUS_DESC
,dbo_V_Decision_Process.DECISION_STATUS_DATE
,dbo_V_Decision_Process.DECISION_PROCESS_STATUS
,dbo_V_Application_Process.INVESTOR_NAME

FROM
((dbo_V_Application_Process
LEFT JOIN dbo_V_Document_Intake_Process
ON dbo_V_Application_Process.APPLICATION_ID = dbo_V_Document_Intake_Process.APPLICATION_ID)
LEFT JOIN dbo_V_Decision_Process
ON dbo_V_Application_Process.APPLICATION_ID = dbo_V_Decision_Process.APPLICATION_ID)

我通过执行SELECT * FROM V_Application_Process etc.比较每个视图,并将其与Access查询所基于的直通(它们是那些非常相同的视图的直通连接)进行比较,并且它们总是返回完全相同的结果。因此,查询所基于的视图是相同的,但是以相同的方式将它们组合在一起并获得不同的结果。

有谁知道为什么?

修改

好的,所以我注意到,当所有这三个视图都在Access中连接时,一个不应该为NULL的字段为NULL(SSMS很好)。因此,在Access中,我使用应用程序ID加入的APplication Process和Document Intake Process视图进行了测试查询。我使用了一个测试帐户。它工作正常。然后,我在Application进程和Decision进程视图中加入了另一个应用程序ID。它工作正常。最后,我在应用程序ID上加入了所有三个表。在这个测试帐户上,当我这样做时,我得到零结果。所以视图上的连接工作得很好,直到你输入三个混合,我不知道为什么。

2 个答案:

答案 0 :(得分:1)

Adding to what Andre said, how about replacing all the columns in the SELECT clause with all the Primary key ID fields used in the joins. Will this still give different results?

If so identify the rows that are missing and see if they have anything in common.

I've just noticed all your tables have dbo_ at the start. For pass through queries I was expecting dbo. (See this webpage for more info )

This tells me your SQL Server query is NOT running on the SQL server, but I think it is using the tables you have linked to SQL Server from access that all begin with dbo_

This will bring into play other possible issues (ie the ODBC driver and which columns have been set up as the primary key for the linked tables - I think).

PART2:

Note that instead of writing an access query like your one below, you would be better to create the query below as a view on SQL Server and then "link to" the view in access. This way SQL Server will do more of the processing and less data is transferred to access.

Here's a reformatted version of your Access query (with aliases)

SELECT DISTINCT vAP.ACCOUNT_NUMBER
              , vAP.INVESTOR_CODE
              , vAP.APPLICATION_PROCESS_STATUS
              , vDIP.DOCUMENT_INTAKE_START_DATE
              , vAP.APPLICATION_TYPE_DESC
              , vDP.DECISION_STATUS_DESC
              , vDP.DECISION_STATUS_DATE
              , vDP.DECISION_PROCESS_STATUS
              , vAP.INVESTOR_NAME

SELECT DISTINCT vAP.APPLICATION_ID
              , vDIP.APPLICATION_ID
              , vDP.APPLICATION_ID

           FROM ((dbo_V_Application_Process AS vAP
                      LEFT JOIN dbo_V_Document_Intake_Process AS vDIP
                             ON vAP.APPLICATION_ID = vDIP.APPLICATION_ID
                 )
                 LEFT JOIN dbo_V_Decision_Process AS vDP
                        ON vAP.APPLICATION_ID = vDP.APPLICATION_ID)

答案 1 :(得分:0)

如果从两个查询中删除DISTINCT,您获得相同数量的结果吗?

如果是,我的猜测是某些SQL Server数据类型在Access中没有确切的等价物,因此可能在SQL Server中有些不同的值被映射到Access中的相同值,因此它们的记录变得相同并因此被删除DISTINCT。

我的第一个嫌疑人是日期字段。您在SQL Server中使用哪种数据类型?