简体   繁体   English

更改 SQL Server 直通查询变量

[英]Change SQL Server Pass Through Query Variable

So I have a SQL Server pass through query in MS-Access that I created with the query design.因此,我在使用查询设计创建的 MS-Access 中有一个 SQL Server 直通查询。 I just double click it and it runs and opens up in datasheet view.我只需双击它,它就会运行并在数据表视图中打开。 Then I can export it.然后我可以导出它。 The query looks like so:查询如下所示:

DECLARE @acyr AS varchar(4);
 SELECT @acyr = '2018'

SELECT *
FROM Table
WHERE year = @acyr

I also have a form with a listBox labeled acyrList where the user picks from 2017, 2018, 2019. I would like for acyrList.Value to be passed to the @acyr in the pass through query and return the records for that year.我还有一个带有标记为 acyrList 的 listBox 的表单,用户从 2017、2018、2019 年选择。我希望 acyrList.Value 在传递查询中传递给 @acyr 并返回该年的记录。

How can I achieve this ?我怎样才能做到这一点?

You can rebuild the SQL in the Click event for the listbox and then assign that SQL to the pass through query.您可以在列表框的 Click 事件中重建 SQL,然后将该 SQL 分配给传递查询。 Something like:就像是:

Private Sub acyrList_Click()
  Dim strSQL As String
  strSQL = "DECLARE @acyr AS varchar(4); " & _
           "SELECT @acyr = '" & acyrList.value & "'; " & _
           "SELECT * From Table WHERE year = @acyr;"

  'MsgBox strSQL
  CurrentDb.QueryDefs("passthrough_query_name").sql = strSQL
End Sub

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

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