Click here to monitor SSC
SQLServerCentral is supported by Red Gate Software Ltd.
 
Log in  ::  Register  ::  Not logged in
 
 
 
        
Home       Members    Calendar    Who's On


Add to briefcase 12»»

Sequence Expand / Collapse
Author
Message
Posted Tuesday, December 21, 2010 8:30 PM
SSC Veteran

SSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC Veteran

Group: General Forum Members
Last Login: Yesterday @ 1:38 PM
Points: 227, Visits: 351
Comments posted to this topic are about the item Sequence
Post #1038039
Posted Tuesday, December 21, 2010 9:26 PM
SSCommitted

SSCommittedSSCommittedSSCommittedSSCommittedSSCommittedSSCommittedSSCommittedSSCommitted

Group: General Forum Members
Last Login: Thursday, October 23, 2014 11:58 AM
Points: 1,676, Visits: 1,758
This is a good basic question, thank you. One thing I would like to point out is that the update script in question is more complex than it needs to be and it uses what Jeff Moden calls a triangular join, which is one of the most evil things one can do to kill performance of the T-SQL query. Of course back in the dark ages when we had to work with SQL Server 2000, the triangular joins were a necessary evil sometimes, but in nowadays when the end of support for SQL Server 2000 is long as ended and the end of support for SQL Server 2005 is looming (12 of April 2011), it is about time to stop the triangular joins insanity and take a look at the windowing functions instead. For example, the update in question can be easily restated like this:

;with records (RecID, Seq) as
(
select
RecID, row_number() over(partition by Value order by RecID)
from #Test
)
update #Test
set
Seq = records.Seq
from #Test inner join records
on #Test.RecID = records.RecID;

Please, please read Jeff's article: http://www.sqlservercentral.com/articles/T-SQL/61539/
It is a true eye opener!

Just my 2 cents.

Oleg
Post #1038056
Posted Tuesday, December 21, 2010 10:48 PM


Ten Centuries

Ten CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen Centuries

Group: General Forum Members
Last Login: Tuesday, October 14, 2014 4:43 AM
Points: 1,130, Visits: 1,391
Good question.
Thanks 'Oleg Netchaev' for query & link.


Thanks
Post #1038073
Posted Wednesday, December 22, 2010 1:08 AM
Ten Centuries

Ten CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen Centuries

Group: General Forum Members
Last Login: Monday, October 27, 2014 2:52 PM
Points: 1,012, Visits: 997
Nice question, thanks also Oleg for the remark and link to Triangular Joins.

Regards,
Iulian
Post #1038101
Posted Wednesday, December 22, 2010 2:17 AM


SSChampion

SSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampion

Group: General Forum Members
Last Login: Yesterday @ 1:52 PM
Points: 13,368, Visits: 11,148
The question itself was OK, as a test to see what output the T-SQL would give.
But not OK for the purpose mentioned in the explanation, as there are other statements that will do it much better and faster (as Oleg has already mentioned).




How to post forum questions.
Need an answer? No, you need a question.
What’s the deal with Excel & SSIS?

Member of LinkedIn. My blog at LessThanDot.

MCSA SQL Server 2012 - MCSE Business Intelligence
Post #1038122
Posted Wednesday, December 22, 2010 3:59 AM
SSCommitted

SSCommittedSSCommittedSSCommittedSSCommittedSSCommittedSSCommittedSSCommittedSSCommitted

Group: General Forum Members
Last Login: Today @ 11:09 AM
Points: 1,789, Visits: 6,513
I get a pile of errors when I run it - Cannot insert the value NULL into column 'Seq'.
I eventually worked out that this is because I have ANSI_NULL_DFLT_ON set to False, so Seq was added as a Not Null column.

This never normally affects me, as I always specify NULL or NOT NULL and don't rely on a default, but can anyone point me in the direction of where this might be set?
Post #1038156
Posted Wednesday, December 22, 2010 4:06 AM


SSC Eights!

SSC Eights!SSC Eights!SSC Eights!SSC Eights!SSC Eights!SSC Eights!SSC Eights!SSC Eights!

Group: General Forum Members
Last Login: Monday, March 25, 2013 4:03 AM
Points: 850, Visits: 71
Good question.

Regards,
Jagan.
Post #1038159
Posted Wednesday, December 22, 2010 5:16 AM


SSChampion

SSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampion

Group: General Forum Members
Last Login: Yesterday @ 1:52 PM
Points: 13,368, Visits: 11,148
Toreador (12/22/2010)
I get a pile of errors when I run it - Cannot insert the value NULL into column 'Seq'.
I eventually worked out that this is because I have ANSI_NULL_DFLT_ON set to False, so Seq was added as a Not Null column.

This never normally affects me, as I always specify NULL or NOT NULL and don't rely on a default, but can anyone point me in the direction of where this might be set?


Right-click on the database in SSMS, select properties and in the properties editor select the options tab. There you can set the value for 'ANSI NULL default'. You can also use the sp_dboption procedure.




How to post forum questions.
Need an answer? No, you need a question.
What’s the deal with Excel & SSIS?

Member of LinkedIn. My blog at LessThanDot.

MCSA SQL Server 2012 - MCSE Business Intelligence
Post #1038195
Posted Wednesday, December 22, 2010 7:14 AM
Ten Centuries

Ten CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen Centuries

Group: General Forum Members
Last Login: Today @ 9:43 AM
Points: 1,108, Visits: 1,470
I got it right based on the fact that an update statement shouldn't ever be able to change the number of records in a table - since 8 rows were inserted the only possible answer was the first one (assuming there was no error). I got it right but was I correct in making that assumption or did I just get lucky? I can't think of any circumstance where an update would do that but have I missed something?

Paul
Post #1038260
Posted Wednesday, December 22, 2010 8:13 AM


SSChampion

SSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampion

Group: General Forum Members
Last Login: Yesterday @ 1:52 PM
Points: 13,368, Visits: 11,148
paul.goldstraw (12/22/2010)
I got it right based on the fact that an update statement shouldn't ever be able to change the number of records in a table - since 8 rows were inserted the only possible answer was the first one (assuming there was no error). I got it right but was I correct in making that assumption or did I just get lucky? I can't think of any circumstance where an update would do that but have I missed something?

Paul


Unless you implement the update as an delete and an insert and something goes horribly wrong in between, I would be very surprised




How to post forum questions.
Need an answer? No, you need a question.
What’s the deal with Excel & SSIS?

Member of LinkedIn. My blog at LessThanDot.

MCSA SQL Server 2012 - MCSE Business Intelligence
Post #1038294
« Prev Topic | Next Topic »

Add to briefcase 12»»

Permissions Expand / Collapse