简体   繁体   English

在 MS Access 中处理过滤的直通查询

[英]Dealing with filtered Pass Through Query in MS Access

I have a relatively complex SQL query (complex to run in Access) and want to run it in MS Access.我有一个相对复杂的 SQL 查询(在 Access 中运行很复杂)并且想在 MS Access 中运行它。 It works with the pass-through query well but going forward I will face an issue that is related to a filter I apply in the query.它适用于直通查询,但接下来我将面临与我在查询中应用的过滤器相关的问题。 I select the current report date within the where function.我 select 当前报告日期function 内。 Below is a part of my query I try to handle;以下是我尝试处理的查询的一部分;

select  LS.PID_FACILITY, LS.ASOF_DTE, LS.DATA_CYCLE_FLG,  LS.CUST_ACC, LS.CUST_SMUN, LS.CUST_NME, LS.CUST_CTY, 

WHERE LS.ASOF_DTE='19-SEP-22'

I do not want to change asof_dte filter manually everyday.我不想每天手动更改asof_dte过滤器。 If this was a normal access query I could join another table that includes only the current report date.如果这是一个正常的访问查询,我可以加入另一个只包含当前报告日期的表。 But I cannot do it in a pass-through query.但我不能在直通查询中做到这一点。 What is the alternative way to do it?什么是替代方法? I read something about creating variables or strings, but I could not relate them to my problem, since I am a beginner at creating such solutions.我读过一些关于创建变量或字符串的内容,但我无法将它们与我的问题联系起来,因为我是创建此类解决方案的初学者。
Thank you all.谢谢你们。

Well, two VERY intresting things here.好吧,这里有两件非常有趣的事情。

First, YES a great idea to include the date in the PT query.首先,是的,在 PT 查询中包含日期是个好主意。 But, you don't want to change that date each time.但是,您不想每次都更改该日期。

Soluton: Add a paramter to the query, and then from Access code add that paramter.解决方案:向查询中添加一个参数,然后从 Access 代码中添加该参数。 It is VERY easy to do this (one line of code.!! - don't adopt the zillion examples out there that has a boatload of ADO code - NOT required!.做到这一点非常容易(一行代码。!! - 不要采用那里有大量 ADO 代码的无数示例 - 不需要!。

However, BEFORE we start dealing with above?但是,在我们开始处理上述内容之前?

A MUCH better and simple, less work way to approach this?一种更好、更简单、更少工作的方法来解决这个问题?

in place of stored procedure?代替存储过程?

if possbile, create a view.如果可能,创建一个视图。 and use that for the report.并将其用于报告。

Why?为什么? Because you then get TWO VERY valuable bonus.因为你会得到两个非常有价值的奖金。

First, you can freely use the reports "where" clause, and it respects the where clause and STILL runs server side!!!首先,您可以自由使用reports "where"子句,它尊重where子句,仍然运行服务器端!!!

In other words, create a view for that existing query, but WITHOUT the date set in that view.换句话说,为该现有查询创建一个视图,但没有在该视图中设置日期。

You then link to the view from access client side.然后,您从访问客户端链接到视图。

Now, to open (filter) the report, you can do this:现在,要打开(过滤)报告,您可以这样做:

docmd.OpenReport "MyReport",acViewPreview,,"LS.ASOF_DTE='19-SEP-22'"

Now, of couse the above "where" clause can be a varible (string).现在,当然,上面的“where”子句可以是一个变量(字符串)。

NOTE SUPER but SUPER careful here:注意这里超级但超级小心:

If you base the reprot on a pass-though query (that then uses the stored procedure), then the filter occures CLIENT SIDE.!!!如果您将报告基于传递查询(然后使用存储过程),则过滤器出现在客户端。!!! (all rows will be returned and THEN filtered if you report is based on that stored procedure. (如果您的报告基于该存储过程,则所有行都将被返回并过滤。

But, if you use a view?但是,如果你使用视图呢? The the filter makes it to the server side!!!!过滤器使其到达服务器端!!!!

While both the pass-through query or the "view" can be filtered with the above "open report" and the where clause we have above?虽然传递查询或“视图”都可以使用上面的“打开报告”和我们上面的 where 子句进行过滤?

The view will still filter server side - the pass-though query will NOT!!!视图仍将过滤服务器端 - 传递查询不会!!!

Now, the 3rd way, is of course to build the stored procedure to accept a date parmater.现在,第三种方法当然是构建存储过程以接受日期参数。

You then could do this:然后你可以这样做:

with Currentdb.QueryDefs("MyPassThoughQueryGoesHere")
    .SQL = "EXEC MyStoreProc " + "19-SEP-22"
END WITH

docmd.OpenReport "MyReport",acViewPreview

So, you CAN add and have a PT query and add a paramter as per above.因此,您可以添加并进行 PT 查询并按照上述添加参数。

However, unless that stored procedure has some speical code, you are MUCH better off to create a view server side, base the reprot on that view, and simple pass + use the traditional "where" clause of the open report command.但是,除非该存储过程有一些特殊代码,否则最好创建一个视图服务器端,基于该视图进行报告,然后简单地传递 + 使用打开报告命令的传统“where”子句。 Even if that view has no filter, returns all rows in the table?即使该视图没有过滤器,是否返回表中的所有行?

With the "where" clause of the open report command, ONLY those rows meeting that critera will be pulled down the network pipe.使用打开报告命令的“where”子句,只有符合该条件的行才会被拉下网络 pipe。

So, say a invoice table with 1 million rows.因此,假设有 100 万行的发票表。

Create a view, link the view in access.创建一个视图,在访问中链接该视图。

base report on that view.基于该观点的报告。

Now, do this:现在,这样做:

docmd.OpenReport "rptInvoice",,,"InvoiceNum = 134343"

The above will ONLY PULL down 1 row from the server.以上只会从服务器拉下 1 行。 Even if the view has no filter and would return 1 million rows.即使视图没有过滤器并且会返回 100 万行。

So, using a view is less work then creating the stored procedure.因此,使用视图比创建存储过程的工作量少。

But, you can modify the stored procedure to accept a paramter, and then as noted use the above example to modify the PT query you have, and THEN open the report.但是,您可以修改存储过程以接受参数,然后按照说明使用上面的示例修改您拥有的 PT 查询,然后打开报告。

I think overall, it is less work to use view.我认为总体而言,使用视图的工作量较少。 Furthermore, if you have a slow running report now?此外,如果您现在有一份运行缓慢的报告?

Replace the query (move it) to sql server side.将查询(移动它)替换为 sql 服务器端。 Get it working.让它工作。 Now link to that view (give it same name as what the client side query was in Access).现在链接到该视图(将其命名为与 Access 中客户端查询相同的名称)。

Now, EVEN if you had some fancy filter code in VBA, and used openReport with the "where" clause?现在,即使您在 VBA 中有一些花哨的过滤器代码,并且使用带有“where”子句的 openReport? It will now work, only pull the records down the network pipe, you get stored procedure performance without the hassles.它现在可以工作了,只需将记录拉到网络 pipe 上,您就可以轻松获得存储过程性能。 and the date format and "where" clause for open report is access/VBA style - not sql server style SQL.打开报告的日期格式和“where”子句是访问/VBA 样式 - 不是 sql 服务器样式 SQL。

So, high recommend you try and dump the stored procedure and use a view (and EVEN better is any where clause works - not just one based on pre-defined parameters for the stored procedure - so you not limited to parameters)因此,强烈建议您尝试转储存储过程并使用视图(甚至更好的是任何 where 子句都可以工作 - 不仅仅是一个基于存储过程的预定义参数的 - 所以你不限于参数)

. . However, no big deal - the above "EXEC dbo.MyStoreProce " & strDate example would also work fine if you have a date parameter you wish to supply to the pass-though query.但是,没什么大不了的 - 如果您希望提供给传递查询的日期参数,上述“EXEC dbo.MyStoreProce”和 strDate 示例也可以正常工作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM