简体   繁体   中英

Select And Update within same query in MS Access & C#

I would like to Select & Update the same record in access in same query from c# application. Something like-

int personId=command.ExecuteScalar(
  "Select Top 1 PersonId From Persons where IsLocked=false 
  AND UPDATE THE SELECTED ROW WITH IsLocked=true");

I must require the PersonId value which is being selected & updated in my code.

I saw some questions in SO, but those are not what I actually want.

Any help?

I'm not clear why you are only grabbing the personID and then want to change a different column, but to change only the first row returned then try:

UPDATE P
SET IsLocked = True
FROM Persons P
WHERE PersonId = (SELECT top 1 PersonId FROM Persons WHERE IsLocked = False ORDER BY PersonId)

The query will update the first PersonId with IsLocked = False (Assuming that person is not listed more than once in your table)

Access SQL does not allow you to both SELECT and UPDATE in a single query.

If you were doing this within an Access application session, you could use a custom VBA function which returns the PersonId and performs the UPDATE ... then call that function from a SELECT query. Unfortunately, you can't use a custom VBA function in a query run from outside an Access application session.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM