UPDATE a
SET col2 = b.col2
FROM table1 a
INNER JOIN table2 b
ON b.col1 = a.col1;
ASKER
ASKER
table1 table2
col1 col2 col1 col2
1 2 1 5
1 3 1 6
1 4 2 8
2 1 3 0
3 6 4 7
Using this query:UPDATE t1
SET col2 = t2.col2
FROM table1 t1
INNER JOIN table2 t2
ON t1.col1 = t2.col1
I understand that which value is chosen from table2 to update table1 (for col1 value = 1) and that it isn't repeatable (e.g. if it updates with 5 on one run, it might update with 6 the next run). My primary question is, if it selects to update table1.col2 with 6 for the first row, will it use 6 for the 2nd and 3rd rows, or is is possible to have it update row 1 and 3 with 5 and row 2 with 6?USE [ExpertsExchange]
GO
DROP TABLE IF EXISTS T1;
DROP TABLE IF EXISTS T2;
GO
CREATE TABLE T1 ( C1 INT NOT NULL, C2 INT NOT NULL ) ;
CREATE CLUSTERED INDEX CIX_T1 ON T1 ( C2 ASC );
CREATE TABLE T2 ( C1 INT NOT NULL, C2 INT NOT NULL ) ;
CREATE CLUSTERED INDEX CIX_T2 ON T2 ( C1 ASC );
GO
INSERT INTO T1
VALUES ( 1, 1 ),
( 2, 1 ),
( 3, 1 ),
( 4, 1 ),
( 1, 2 ),
( 2, 2 ),
( 3, 2 ),
( 4, 2 ),
( 1, 3 ),
( 2, 3 ),
( 3, 3 ),
( 4, 3 ),
( 1, 4 ),
( 2, 4 ),
( 3, 4 ),
( 4, 4 );
INSERT INTO T2
VALUES ( 1, 10 ),
( 1, 20 ),
( 1, 30 ),
( 1, 40 ),
( 2, 2 ),
( 3, 3 ),
( 4, 4 );
GO
UPDATE T1
SET T1.C2 = T2.C2
FROM T1
INNER JOIN T2 ON T1.C1 = T2.C1;
GO
SELECT *
FROM T1;
GO
I said that you should see either a stream aggregate operation or a spool operation in the plan, and, IF you do, then the plan will be consistent.I don't see the necessity that parallel streams use the same spool value in the case e.g. of a large partitioned table.
ASKER
ASKER
Microsoft SQL Server is a suite of relational database management system (RDBMS) products providing multi-user database access functionality.SQL Server is available in multiple versions, typically identified by release year, and versions are subdivided into editions to distinguish between product functionality. Component services include integration (SSIS), reporting (SSRS), analysis (SSAS), data quality, master data, T-SQL and performance tuning.
TRUSTED BY
AFAIK, that type of update is always non-deterministic and whatever order the optimizer chooses isn't something you should bank on. Changing the sql so it's deterministic statement is the only way to ensure a consistent result.
Did you see this thread?
https://www.experts-exchange.com/questions/28704278/UPDATE-Based-on-Join-nondeterministic-example.html