MS Access 中的 SQL 查询变量

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/7491662/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-01 12:12:20  来源:igfitidea点击:

SQL query variables in MS Access

sqlms-access

提问by MarioVW

When writing a query for SQL Server, you can declare and use variables like this:

在为 SQL Server 编写查询时,您可以像这样声明和使用变量:

declare @test int
select @test = max(ID) from MyTable1
update MyTable2 set (...) where ID > @test
update MyTable3 set (...) where ID < @test

Is there a way to declare and use variables similarly when writing a query for MS Access?

在为 MS Access 编写查询时,是否可以类似地声明和使用变量?

I need to populate the variable with the result of another query and then use that value to perform insert/update operations. The query will be run from a .NET app.

我需要用另一个查询的结果填充变量,然后使用该值来执行插入/更新操作。查询将从 .NET 应用程序运行。

回答by Fionnuala

In a way

在某种方式

parameters @test int;
select * from MyTable where ID = @test

However, you cannot use set @test = 1234, the parameter can be manually entered when the query is run or set in VBA.

但是,您不能使用set @test = 1234,该参数可以在查询运行或在 VBA 中设置时手动输入。

Joel Coehoorn
In Query MS Access database in VB 2008

Joel Coehoorn
VB 2008 中查询 MS Access 数据库

You use the classes in the System.Data.OleDb namespace to query access databases:

您可以使用 System.Data.OleDb 命名空间中的类来查询访问数据库:

Using cn As New OleDbConnection("connection string here"), _
      cmd As New OleDbCommand("SELECT query with ? parameter here", cn)

    cmd.Parameters.Add("?", OleDbType.Int).Value = 1234

    MyCombobox.DataSource = cmd.ExecuteReader()
End Using

Further Notes re Edit to OP

进一步的注释重新编辑到 OP

Query 1

查询 1

update MyTable2 set (...) where ID > (select max(test) from table1)

Query 2

查询 2

update MyTable3 set (...) where ID < (select max(test) from table1)