如何使用给定SQL的Access创建查询

时间:2015-09-17 22:03:54

标签: sql ms-access access-vba ms-office sql-view

有人给了我以下SQL并希望我使用Access进行查询,以便它返回所需的信息。所有dbo _ ****都是从中获取信息的数据库。

我尝试使用设计视图执行以下SQL,但它不起作用,数据表(表)与信息不匹配。

我迷失了LEFT OUTER JOIN ......我不知道在那之后该怎么做。有谁可以帮助我吗 ? 或者,如果有人可以编辑SQL,那么它只能在Access中以SQL视图模式进行复制。

谢谢。

Declare @data datetime
set @data = '2013-12-31'

--Fundos
SELECT 
       RelCliSFN.CdCrt, 
       MC5.Nome 'NomeCarteira',
       MC5.CGC 'CNPJCarteira',       
       player_adm.NomeCurto 'Administrador',
       RelCliSFN.Cliente, 
       CE5.Nome 'NomeCliente',
       CE5.CPFCGC 'CPF/CNPJCliente',
       VigRelCliSFN.DtIni, 
       VigRelCliSFN.DtFim
 FROM 
 RelCliSFN
 Inner JOIN MC5 ON MC5.Carteira = RelCliSFN.CdCrt
 INNER JOIN MC5Auxiliar ON MC5.Carteira = MC5Auxiliar.Carteira
 Inner Join VigRelCliSFN ON RelCliSFN.IdRelCliSFN = VigRelCliSFN.IdRelCliSFN
 Inner Join CE5 ON CE5.Cliente=RelCliSFN.Cliente 
 LEFT OUTER JOIN PlayAdmxMC5 ON MC5.Carteira = PlayAdmxMC5.CdCrt AND     PlayAdmxMC5.DtIniVig =
(SELECT     MAX(DtIniVig) FROM          PlayAdmxMC5 WHERE      MC5.Carteira = CdCrt AND DtIniVig < MC5.DataAtual) 
 INNER JOIN Player player_adm ON PlayAdmxMC5.IdPlayAdm = player_adm.ID 
WHERE
--MC5Auxiliar.bNaoExpCCSFN is null
((VigRelCliSFN.DtIni <= @data)
and ((VigRelCliSFN.DtFim >= @data) or (VigRelCliSFN.DtFim is null)))
and MC5.CGC <> 0
and CE5.Assessor not in (17, 99)
and CE5.CPFCGC <>0
and MC5.TipoCarteira <> 4
--and RelCliSFN.CdCrt = 3`

2 个答案:

答案 0 :(得分:0)

正如其他人已经提到的那样,您的查询具有MS SQL-Server语法。所以有三种方法可以解决你的问题。

解决方案1: 在SQL-Server数据库中创建一个视图。

为此,请打开SQL-Server Management Studio。转到 YourServer &gt;数据库&gt; YourDatabase &gt;观点。右键单击&#34;观看&#34;然后选择&#34;新视图...&#34;。关闭&#34;添加表格&#34;窗口。将你的sql代码复制到sql窗口(那个说&#34; SELECT FROM&#34;)。您将收到一条错误消息,指出您的代码无法显示。忽略它。保存您的视图。您可以像连接表一样将它连接到Access数据库。

解决方案2: 在Access中使用SQL-Passthrough查询。

解释起来有点复杂,因为几乎每个Access版本的菜单都略有不同。我将为Access 2007解释它。

创建新查询。关闭&#34;添加表格&#34;窗口。选择&#34;传递&#34;从设计菜单(我不确切地知道英文版的菜单标签是&#34; design&#34;。在我的版本中它说&#34; Entwurf&#34;)。打开属性窗口。插入正确的ODBC字符串。获取ODBC字符串的最简单方法是使用现有ODBC表中的一个。转到VBA立即窗口(Ctrl + G)并写入?CurrentDb.TableDefs("YourTable").Connect(而不是YourTable,您必须插入ODBC表的名称)。按Return键。你应该得到像

这样的东西
ODBC;DSN=YourConnection;Description=IAmAnODBCConnection;APP=2007 Microsoft Office system;DATABASE=YourDatabase

将此字符串复制到查询的ODBC属性中。确保选项&#34;返回记录&#34;设置为&#34;是&#34;。将SQL复制到查询中并保存。

解决方案3: 将SQL Server语法转换为Access语法。在执行此操作之前,您必须连接查询中使用的每个表。我无法测试语法转换,但这应该可行:

PARAMETERS data DateTime = #2013-12-31#;

SELECT 
       RelCliSFN.CdCrt, 
       MC5.Nome "NomeCarteira",
       MC5.CGC "CNPJCarteira",       
       player_adm.NomeCurto "Administrador",
       RelCliSFN.Cliente, 
       CE5.Nome "NomeCliente",
       CE5.CPFCGC "CPF/CNPJCliente",
       VigRelCliSFN.DtIni, 
       VigRelCliSFN.DtFim
 FROM 
 RelCliSFN
 Inner JOIN MC5 ON MC5.Carteira = RelCliSFN.CdCrt
 INNER JOIN MC5Auxiliar ON MC5.Carteira = MC5Auxiliar.Carteira
 Inner Join VigRelCliSFN ON RelCliSFN.IdRelCliSFN = VigRelCliSFN.IdRelCliSFN
 Inner Join CE5 ON CE5.Cliente=RelCliSFN.Cliente 
 LEFT OUTER JOIN PlayAdmxMC5 ON MC5.Carteira = PlayAdmxMC5.CdCrt AND     PlayAdmxMC5.DtIniVig =
(SELECT     MAX(DtIniVig) FROM          PlayAdmxMC5 WHERE      MC5.Carteira = CdCrt AND DtIniVig < MC5.DataAtual) 
 INNER JOIN Player player_adm ON PlayAdmxMC5.IdPlayAdm = player_adm.ID 
WHERE
((VigRelCliSFN.DtIni <= [data])
and ((VigRelCliSFN.DtFim >= [data]) or (VigRelCliSFN.DtFim is null)))
and MC5.CGC <> 0
and CE5.Assessor not in (17, 99)
and CE5.CPFCGC <>0
and MC5.TipoCarteira <> 4

我不知道Access是否可以处理PlayAdmxMC5.DtIniVig =(子查询)的等式。如果它不起作用,您应该创建一个仅包含子查询的新查询,然后将其连接到此查询。

答案 1 :(得分:0)

我正在使用的主要代码

   SELECT dbo_RelCliSFN.CdCrt, dbo_MC5.Nome AS NomeCarteira, dbo_MC5.CGC AS CNPJCarteira, dbo_RelCliSFN.Cliente, dbo_CE5.Nome AS NomeCliente, dbo_CE5.CPFCGC AS [CPF/CNPJCliente], dbo_VigRelCliSFN.DtIni, dbo_VigRelCliSFN.DtFim
FROM ((((dbo_RelCliSFN INNER JOIN dbo_CE5 ON dbo_RelCliSFN.Cliente = dbo_CE5.Cliente) INNER JOIN dbo_MC5 ON dbo_RelCliSFN.CdCrt = dbo_MC5.Carteira) INNER JOIN dbo_MC5Auxiliar ON dbo_MC5.Carteira = dbo_MC5Auxiliar.Carteira) INNER JOIN dbo_VigRelCliSFN ON dbo_RelCliSFN.IdRelCliSFN = dbo_VigRelCliSFN.IdRelCliSFN) LEFT JOIN Query1 ON dbo_MC5.Carteira = Query1.CdCrt
GROUP BY dbo_RelCliSFN.CdCrt, dbo_MC5.Nome, dbo_MC5.CGC, dbo_RelCliSFN.Cliente, dbo_CE5.Nome, dbo_CE5.CPFCGC, dbo_VigRelCliSFN.DtIni, dbo_VigRelCliSFN.DtFim, dbo_MC5.TipoCarteira, dbo_CE5.Assessor
HAVING (((dbo_RelCliSFN.CdCrt)=802) AND ((dbo_MC5.CGC)<>0) AND ((dbo_CE5.CPFCGC)<>0) AND ((dbo_VigRelCliSFN.DtIni)<=#12/31/2013#) AND ((dbo_VigRelCliSFN.DtFim)>=#12/31/2013# Or (dbo_VigRelCliSFN.DtFim) Is Null) AND ((dbo_MC5.TipoCarteira)<>4) AND ((dbo_CE5.Assessor) Not In (17.19)));