|
|
|
SSCoach
         
Group: General Forum Members
Last Login: Yesterday @ 1:07 PM
Points: 18,733,
Visits: 12,332
|
|
|
|
|
|
SSCertifiable
       
Group: General Forum Members
Last Login: Today @ 1:54 AM
Points: 7,086,
Visits: 7,138
|
|
Carlo Romagnano (12/30/2010)
Try this for fun: DECLARE @MyTable TABLE (Col1 INT, Col2 INT ,Col3 INT) DECLARE @i INT -- Col1 Col2 INSERT INTO @MyTable(Col1,Col2) VALUES (1, 2) INSERT INTO @MyTable(Col1,Col2) VALUES (3, 4) SET @i = 0 UPDATE @MyTable SET @i = @i + Col1 , Col1 = Col2 , Col2 = Col1 , Col3 = @i
SELECT Col1, Col2, Col3 FROM @MyTable Mixing column swap with quirky update in this form is perhaps interesting, but certainly not deterministic - even though there are no indexes involved so that the only thing likely to disturb the sequencing is parallelism and doing this serially is probably going to be faster than the synchronisation overhead incurred in doing it in parallel so the optimiser is unlikely to choose parallel evaluation, there's nothing in the language specification that guarantees the order in which the rows are processed so the system can choose either row to process first or indeed can process both rows at once, which means there are 3 different results all of which would be "correct". So if you were relying on what goes into col3, you would need to add protective code to check that the (inter-row) sequencing is what you expected and abort the operation if it isn't.
Tom Que conclure à la fin de tous mes longs propos? C'est que les préjugés sont la raison des sots. (Voltaire, 1756)
|
|
|
|
|
SSCrazy
      
Group: General Forum Members
Last Login: Monday, May 13, 2013 11:21 AM
Points: 2,163,
Visits: 2,148
|
|
Thanks for the question!
I think this would fall under the "Halloween Protection" portions of updates and deletes.
For some trivia you can read some about the Halloween problem here.
|
|
|
|
|
SSCommitted
      
Group: General Forum Members
Last Login: Today @ 12:54 AM
Points: 1,969,
Visits: 1,820
|
|
Tom.Thomson (12/30/2010)
[b]Carlo Romagnano Mixing column swap with quirky update in this form is perhaps interesting, but certainly not deterministic - even though there are no indexes involved so that the only thing likely to disturb the sequencing is parallelism and doing this serially is probably going to be faster than the synchronisation overhead incurred in doing it in parallel so the optimiser is unlikely to choose parallel evaluation, there's nothing in the language specification that guarantees the order in which the rows are processed so the system can choose either row to process first or indeed can process both rows at once, which means there are 3 different results all of which would be "correct". So if you were relying on what goes into col3, you would need to add protective code to check that the (inter-row) sequencing is what you expected and abort the operation if it isn't. Here a real situation for using local variable in the update statement.
-- prologue CREATE TABLE toProcTable ( ID int identity(1,1) NOT NULL primary key clustered ,cCol1 sysname ,bProcessed BIT ) CREATE INDEX IDX__toProcTable ON toProcTable (bProcessed) -- in sql2008, add here a filter bProcessed IS NULL OR bProcessed = 0
DECLARE @id INT ,@cCol1 sysname INSERT INTO toProcTable(cCol1) SELECT name from sys.objects -- end prologue
-- process one row at time WHILE 1=1 BEGIN BEGIN TRAN -- in the table a list of rows to process one after one, doesn't matter in which order UPDATE TOP (1) toProcTable SET bProcessed = 1 -- save some info for later processing ,@id = toProcTable.ID ,@cCol1 = toProcTable.cCol1 WHERE bProcessed IS NULL OR bProcessed = 0 IF @@ROWCOUNT < 1 BREAK
-- do some process PRINT 'Processing: ' + @cCol1 SELECT * FROM sys.tables s WHERE s.name = @cCol1 COMMIT END
DROP TABLE toProcTable
|
|
|
|
|
Ten Centuries
      
Group: General Forum Members
Last Login: Yesterday @ 8:55 AM
Points: 1,038,
Visits: 1,355
|
|
[quote]Carlo Romagnano (12/31/2010) Here a real situation for using local variable in the update statement.
Carlo, I'm curious as to the context of your code (not included here for brevity and because somewhere between me and SQLServerCentral.com is a stupid IPS that thinks any text with a certain common SQL word in it is an injection attack) -- I'm guessing you abbreviated the row process in what you posted from some form of dynamic SQL.
I'm curious for an explanation of why you're doing row-by-row processing here instead of set-based operations.
Also, have you tested this implementation against a cursor-based implementation?
|
|
|
|
|
UDP Broadcaster
      
Group: General Forum Members
Last Login: Tuesday, May 14, 2013 6:45 AM
Points: 1,441,
Visits: 2,485
|
|
Koen (da-zero) (12/30/2010) Nice question, but I hope I will never need to swap data between two columns. You might need to swap if you have a pair of columns such as first_name, last_name and the user enters them in reverse, e.g. "Thomas Mitchell" when it should have been "Mitchell Thomas".
|
|
|
|
|
SSCertifiable
       
Group: General Forum Members
Last Login: Today @ 2:31 AM
Points: 5,235,
Visits: 7,033
|
|
Nice question. Though I'm surprised at the high percentage of incorrect answers.
Also, the disclaimer can be removed. This behaviour of the UPDATE statement is how the ANSI standard prescribes it to behave, Any other result, in any version of SQL Server (or even any other relational database) would qualify as a bug. And a severe one. THe UPDATE should work as if all the results are computed first (based on the old information), and then all rows are updated with the new values instantaneously. That's not actually possible in current technology, but the effect should be the same as if it were possible.
Hugo Kornelis, SQL Server MVP Visit my SQL Server blog: http://sqlblog.com/blogs/hugo_kornelis
|
|
|
|
|
UDP Broadcaster
      
Group: General Forum Members
Last Login: Wednesday, April 17, 2013 10:57 PM
Points: 1,491,
Visits: 3,008
|
|
Koen (da-zero) (12/30/2010)
jts_2003 (12/30/2010)
...usually when something has gone wrong!  That's why I hope I will never need to use it  Although you may never have to simply swap the values of two columns, isn't it valuable to know that the original value of a column remains available within an UPDATE even following code that would appear to modify it?
|
|
|
|
|
Ten Centuries
      
Group: General Forum Members
Last Login: Yesterday @ 5:44 PM
Points: 1,289,
Visits: 3,857
|
|
Nice question - although the title does give the answer!
I also wonder about points allocation on these questions - although I don't really care about it - but it seems almost random!
MM
|
|
|
|
|
Mr or Mrs. 500
      
Group: General Forum Members
Last Login: Wednesday, July 25, 2012 9:04 PM
Points: 542,
Visits: 187
|
|
Question title provided answer for the question
|
|
|
|