Aditya Acharya
Aditya Acharya

Reputation: 395

How to update two tables in a single query in MS SQL

Is it be possible to update two tables writing a single query?

So that i do not have to execute two queries and to track whether both are successful?

Upvotes: 4

Views: 1160

Answers (5)

NET Experts
NET Experts

Reputation: 1536

Ofcourse you can by using triggers

Upvotes: 0

hlovdal
hlovdal

Reputation: 28180

You could use transactions, however you are still required to update the tables separately and check the results before committing or rolling back.

Upvotes: 0

Robin Day
Robin Day

Reputation: 102468

You can write a stored procedure that updates the two tables and returns whatever you need it to in order to determine success. This stored proc can then be called from a single command. However, it will still have to contain two queries.

Upvotes: 1

lhahne
lhahne

Reputation: 6029

You can't do it in a query but you can do it as a transaction when all queries within the transaction will either succeed or fail.

Upvotes: 3

shahkalpesh
shahkalpesh

Reputation: 33476

No, that is not possible AFAIK.

EDIT: What is the reason for you to achieve this in a single query?

Upvotes: 0

Related Questions