Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SQL query variables in MS Access

Tags:

sql

ms-access

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

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?

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.

like image 583
MarioVW Avatar asked Jan 19 '26 14:01

MarioVW


1 Answers

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.

Joel Coehoorn
In Query MS Access database in VB 2008

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

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

Query 1

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

Query 2

update MyTable3 set (...) where ID < (select max(test) from table1)
like image 158
Fionnuala Avatar answered Jan 21 '26 05:01

Fionnuala



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!