When I run the following script, Original_Table
gets updated by the UPDATE
statement
WITH temp AS (
SELECT
ROW_NUMBER() over (partition by x order by y) row_num, x, z
FROM Original_Table )
UPDATE temp set z = a + (select ISNULL(SUM(a),0) from temp A where A.x= temp.xand A.row_num < temp.row_num)
But when I replace WITH with the following
CREATE TABLE
#temp
(
row_num INT NOT NULL ,
x INT NOT NULL,
a DECIMAL NOT NULL ,
z DECIMAL NULL
);
insert into #temp
SELECT
ROW_NUMBER() over (partition by x order by y) row_num, x, z
FROM Original_Table
UPDATE temp set z = a + (select ISNULL(SUM(a),0) from temp A where A.x= temp.xand A.row_num < temp.row_num)
the UPDATE
updates only the #temp
table but not Original_Table
Why is that?