小编典典

MS Access中的SQL查询变量

sql

为SQL Server编写查询时,可以声明和使用如下变量:

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

在编写用于MS Access的查询时,是否有一种方法可以类似地声明和使用变量?

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


阅读 371

收藏
2021-03-08

共1个答案

小编典典

在某种方式

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

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

您可以使用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

进一步的说明重新编辑为OP

查询1

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

查询2

update MyTable3 set (...) where ID < (select max(test) from table1)
2021-03-08