2011-09-20 146 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