I’m curios if this can be achieved as I’m currently facing a bug and would like to see if putting a SELECT and an UPDATE in a transaction would fix it (if you’re wondering why I’m not posting the code that causes the bug it’s because it’s a complex environment and I can’t post all the influencing factors).
Something that I’m also interested in, related to this, is if you have ever experienced code that had and UPDATE query written after a SELECT query, yet the UPDATE gets executed before the SELECT (with the possibility that the script might run twice ruled out).
It depends on what do you mean by a transaction.
There are two types of transactions:
INSERT,UPDATE,SELECT,DELETEstatements, and in such statements there is no explicit transaction commands, and the database engine will rollback the whole statement if an error happens.COMMITthe whole transaction orROLLBACK.So you can’t have both
SELECTandUPDATEinside one query, but you can but them inside a transaction like:Then Put them in a stored procedure or a UDF.
Note that:
SELECTstatements do not modify data, so you might not need to enclose it in a transaction, so in your case you will have onlyUPDATEstatement you can just use a stored procedure without a transaction.