2011-09-20 144 views
1

当编写的SQL Server查询,你可以声明和使用变量是这样的:SQL查询变量

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

是否有申报和的MS Access编写查询时使用的变量同样的方式?

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

+1

在Access中,至少有2个查询。 – Fionnuala

回答

4

在某种程度上

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

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

乔尔Coehoorn
Query MS Access database in VB 2008

您使用的类中的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) 
+0

我需要从查询中填充'@ test'。此外,查询将从.NET应用程序运行,而不是VBA。 – MarioVW

+0

如果您从网络应用程序运行,则可以添加参数。 – Fionnuala

+0

例如http://stackoverflow.com/questions/2930551/query-ms-access-database-in-vb-2008 – Fionnuala