简体   繁体   English

从Access向SQL Server中的变量传递值

[英]Passing values to variables in SQL server from Access

I have a large query in SQL Server that returns many rows and columns. 我在SQL Server中有一个大型查询,该查询返回许多行和列。 I need to create a user interface in MS Access for that query. 我需要在MS Access中为该查询创建一个用户界面。 In order to run the query I need to pass some variables to the query. 为了运行查询,我需要将一些变量传递给查询。 For example: 例如:

DECLARE @start_date date, @end_date date SELECT * FROM myTable WHERE Date BETWEEN @start_date AND @end_date DECLARE @start_date日期,@ end_date日期SELECT * FROM myTable WHERE日期@start_date和@end_date之间

I know that I can't have a DECLARE statement in the ACCESS query viewer. 我知道在ACCESS查询查看器中不能有DECLARE语句。 But is there another way of passing these parameters to the query? 但是还有其他方法可以将这些参数传递给查询吗? I've read that it can be done with VBA, but I just want to confirm that there is no other way of passing values to the variable. 我读过它可以用VBA完成,但是我只想确认没有其他方法可以将值传递给变量。

Ideally, what I will need is to interact with SQL Server via MS ACCESS 理想情况下,我需要通过MS ACCESS与SQL Server进行交互

Thanks 谢谢

You don't mention where/how/when that query is saved on SQL server. 您没有提到该查询在SQL Server上的保存位置/方式/时间。 Stating that you have large query but leaving out where this query resides is big detail. 声明您的查询量很大,但是忽略了该查询的位置。

However, you asking can you pass values to SQL server without any code? 但是,您问是否可以不使用任何代码将值传递给SQL Server? The answer is yes, but this means your best approach is to create + save the large query server side as a VIEW. 答案是肯定的,但这意味着您最好的方法是创建并保存大型查询服务器端为VIEW。 You will NOT need any parameters. 您将不需要任何参数。

You then when launching the form (or report) in access, simply attached a where clause: 然后,当您在Access中启动表单(或报表)时,只需附加一个where子句:

MyDate is Between Forms!ReportPrompt!startDate and forms!ReportPrompt!EndDate

The above means no VBA, no parameters and if the query has 1 million rows but only ONE row matches your criteria then ONLY the one row will come down the network pipe. 上面的意思是没有VBA,没有参数,并且如果查询有100万行,但只有1行符合您的条件,则只有1行将落入网络管道。 You can just type the above into the reports Filter and just set the report to filter on load property = Yes. 您可以在报告过滤器中键入以上内容,然后将报告设置为根据负载属性进行过滤=是。

However as noted you don't supply much information as to where or how your query is saved server side. 但是,如前所述,您没有提供太多有关服务器端在何处或如何保存查询的信息。 If you talking about a store procedure (a massive detail to leave out), then you can pass parmaters this way: 如果您在谈论存储过程(大量细节需要省略),那么可以通过以下方式传递参数:

Dim qdfPass       As DAO.QueryDef 
Dim rst           As DAO.Recordset 
Set qdfPass = CurrentDb.QueryDefs("MyPass") 
qdfPass.SQL = "exec sp_myProc " &  MyP1Var & "," & MyP2Var
qdfPass.Execute

The above assumes you saved a pass through query client side called MyPass. 上面假设您保存了一个名为MyPass的查询客户端传递。

In fact there is a dozen ways to pass values to SQL server, but without any more information as to where/how/when/when and under what context that query data is to be consumed then we are faced only with wild and speculative guessing. 实际上,有十多种方法可以将值传递给SQL Server,但是没有更多关于在何处/如何/何时/何时以及何时使用查询数据的更多信息,那么我们仅面临疯狂和推测性的猜测。

If the resulting data is to be used for a report, then as noted you don't really need (nor will benefit) from a store procedure and using a "where" clause as part of the open report command will suffice. 如果将结果数据用于报表,则如前所述,您实际上并不需要(也不会受益)存储过程,并且在打开报表命令中使用“ where”子句就足够了。 The same advice applies if you want this data returned to a form. 如果您希望将此数据返回到表单,则同样的建议适用。

If the query and store procedure is only going to do server side processing, then the above code to call a store procedure and pass paramaters will suffice. 如果查询和存储过程仅用于服务器端处理,那么上面的代码将调用存储过程并传递参数。

@Albert D. Kallal has given the best way of doing this which is to use a stored procedure and pass in the values as parameters. @Albert D. Kallal给出了执行此操作的最佳方法,即使用存储过程并将值作为参数传递。 There are a few other ways of doing this for example :- 还有其他一些方法可以做到这一点,例如:

  1. Via ADODB in VBA code :- 通过VDB代码中的ADODB:-

    Dim con As New ADODB.Connection Dim rs As New ADODB.Recordset 作为新的ADODB.Connection的昏暗状态作为新的ADODB.Recordset的昏暗状态

    con.Open "SQLSrv", "User", "Pass" rs.Open "DECLARE @start_date date, @end_date date; SELECT * FROM myTable WHERE Date BETWEEN @start_date AND @end_date", con con。打开“ SQLSrv”,“ User”,“ Pass” rs.Open“ DECLARE @start_date date,@end_date date; SELECT * FROM myTable WHERE Date介于@start_date AND @end_date之间”

  2. You can write a "Pass through" query that does the same thing. 您可以编写执行相同操作的“通过”查询。

Both these methods will work at the moment however Microsoft are trying to remove the support for passing in multiple SQL statements in a single execute and you may find that later versions of TDS ( Tabular Data Stream - the protocol MS OBDC drivers communicates with SQL Server ) do not support this. 这两种方法目前都可以使用,但是Microsoft试图删除对在单个执行中传递多个SQL语句的支持,并且您可能会发现TDS的更高版本(表格数据流-协议MS OBDC驱动程序与SQL Server通信)不支持这一点。

Yes, there are ways to pass parameters to a query via Access. 是的,有几种方法可以通过Access将参数传递给查询。 You can use controls on a form (textbox, etc.). 您可以在表单(文本框等)上使用控件。 You then reference the controls in the Criteria ( WHERE ) portion of your SQL. 然后,您可以在SQL的CriteriaWHERE )部分中引用控件。

So if I wanted to DELETE * records for an employee in a table between two dates that a user specifies, I would reference the control on the form, like this: 因此,如果我想在用户指定的两个日期之间的表中DELETE *某个雇员的记录,我将引用表单上的控件,如下所示:

Between Forms![StartDateTxt] And Forms![EndDateTxt]

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

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