Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

USING Common Table Expression and perform multiple update commands

is it possible to us a CTE to perform multiple update commands?

With Query AS
(
    SELECT
        Table_One.FOO AS FOO,
        Table_Two.BAR AS BAR
    FROM FOO
    JOIN BAR ON FOO.ID = BAR.ID
)
UPDATE
    Query.FOO = 1;
UPDATE
    Query.BAR = 2;

In the example Query isn't available anymore on the second UPDATE command.

EDIT:

My working code looks like this:

With Query AS
(
    SELECT
        Table_One.FOO AS FOO,
        Table_Two.BAR AS BAR
    FROM FOO
    JOIN BAR ON FOO.ID = BAR.ID
)
UPDATE
    Query.FOO = 1

With Query AS
(
    SELECT
        Table_One.FOO AS FOO,
        Table_Two.BAR AS BAR
    FROM FOO
    JOIN BAR ON FOO.ID = BAR.ID
)
UPDATE
    Query.BAR = 2;

Because you can't Update two Tables with one UPDATE command I need two Update commands. The problem right know is, that if I need to change the Select in the CTE I have to do it on two locations in the code.

like image 746
kami Avatar asked Mar 22 '16 11:03

kami


People also ask

Can we use CTE multiple times?

Unlike a derived table, a CTE behaves more like an in-line view and can be referenced multiple times in the same query. Using a CTE makes complex queries easier to read and maintain. Because a CTE can be referred to multiple times in a query, syntax can be simpler.

Can I use CTE in an update statement?

Updates with SQL CTEsWe can use common table expressions to update data in a table and this becomes very intuitive when we do updates with JOINs.

Can you run multiple update statements in SQL?

The UPDATE statement in SQL is used to update the data of an existing table in database. We can update single columns as well as multiple columns using UPDATE statement as per our requirement.

Can we update multiple tables using update command?

It's not possible to update multiple tables in one statement, however, you can use the transaction to make sure that two UPDATE statements must be treated atomically. You can also batch them to avoid a round trip like this. and T1.id = '011008';


2 Answers

A SQL Server UPDATE only allows you to update a single table. As buried in the documentation:

The following example updates rows in a table by specifying a view as the target object. The view definition references multiple tables, however, the UPDATE statement succeeds because it references columns from only one of the underlying tables. The UPDATE statement would fail if columns from both tables were specified.

Although views and CTEs are not exactly the same thing, they often follow similar rules. So, this is also explained in the section on updatable views:

Any modifications, including UPDATE, INSERT, and DELETE statements, must reference columns from only one base table.

You can effectively do what you want by issuing two updates and wrapping them in a single transaction.

like image 164
Gordon Linoff Avatar answered Sep 26 '22 08:09

Gordon Linoff


You can insert your CTE result to a @Table variable and use this Table wherever required in the code block. (You can join this Table with actual table to perform the UPDATE/INSERT/DELETE etc). You can't use the same CTE in multiple statement, because CTE is part of the subsequent statement only.

like image 36
Abdul Rasheed Avatar answered Sep 23 '22 08:09

Abdul Rasheed