Log in  ::  Register  ::  Not logged in

## Repopulating a sequenced column that has gone out of sequence.

 Author Message Maddave SSCarpal Tunnel Group: General Forum Members Points: 4466 Visits: 1487 We have an application which has had a big design floor and is now causing an issue which needs to be rectified.It inserted data in batches of 2000 rows which should have always been in sequence. A column was used which recorded an ever increasing numerical value for each row that was inserted. So batch 1 would have a start value of 1 and an end value of 2001. Batch 2 would have a start value of 2002 and an end value of 4002 etc. The problem is the application which inserted these records in order, missed some records. So in actual fact we have:Batch 1 = seq 1 - 2001 (total 2000)Batch 2 = seq 2002 - 3998 (total 1996, 4 missing)batch 3 = seq 3999 - 5999 (total 2000)Now the missing data has been found, it will be inserted at the end of the table, as we cannot insert it where its should be as the sequence value has been used by the next batch already. Lets say the table is 10,000 rows so far, so the seq for batch 2, for example, will be 2002-3998, then after the missing data is inserted, 10,001 - 10,005. The application uses these sequences to show the range of data so where it should be 2000 rows being returned, it would now show 8003 rows - all the other batches between the first and last sequences for the batch!I need to change the sequence values across the whole table so they are in order. I can select the data in the correct sort order as there is another unique key on the table, but unsure how best to use this to change the sequence column to be in order for all rows. Adding an identity column would work and then remove and rename the seq column, but I obviously cannot add this based on another columns sort order. The identity column would use the clustered key which is in the incorrect order!!I would be interested if anyone can think of an approach to sort this mess out and I be very grateful.Thanks. Lynn Pettis SSC Guru Group: General Forum Members Points: 225938 Visits: 40423 Hard to say with what you posted but looks doable. If you post the DDL for the table, some sample data (as INSERT statements), and the expected results I am sure we could show you how to do it easily since it sounds like all the info we need exists in the data (just be sure to show us the pertinent data elements). Lynn PettisFor better assistance in answering your questions, click hereFor tips to get better help with Performance Problems, click hereFor Running Totals and its variations, click here or when working with partitioned tablesFor more about Tally Tables, click hereFor more about Cross Tabs and Pivots, click here and hereManaging Transaction LogsSQL Musings from the Desert Fountain Valley SQL (My Mirror Blog) Maddave SSCarpal Tunnel Group: General Forum Members Points: 4466 Visits: 1487 Thanks. Picture speaks a thousand words. So`--Example tablecreate table Labels(Id int identity (1,1),Value varchar(20),IntSequence bigint,ExternalSequence bigint,BatchId int)-- Insert some dataInsert into labels(Value, IntSequence, ExternalSequence, BatchId)Values--batch 1('exampleValue', 1,1,1),('exampleValue', 2,2,1),('exampleValue', 3,3,1),--batch 2('exampleValue', 4,6,2),('exampleValue', 5,7,2),('exampleValue', 6,8,2),-- batch 3('exampleValue', 7,10,3),('exampleValue', 8,11,3),-- Insert the missing values for batch 1('exampleValue', 9,4,1),('exampleValue', 10,5,1),-- insert the missing values for batch 2('exampleValue', 11,9,2)select * from Labels -- Current order showing external seq in wrong order, but internal seq in correct order.Select * from labels order by ExternalSequence asc -- Order by the other column which is in correct order shows true order, but internal sequence in wrong order `Hopefully the above code helps. I need to get the internal sequence to be in the sequential order dependant on the external sequence. My real problem is like the above, but unfortunately I cannot just copy the external sequence to internal! I was playing around with using something like`declare @id intset @id = 0update labelsset @id = intsequence = @id + 1`but not sure on how to update it in sort order? Lynn Pettis SSC Guru Group: General Forum Members Points: 225938 Visits: 40423 Can you explain why you can't copy the ExternalSequence to the InternalSequence? Lynn PettisFor better assistance in answering your questions, click hereFor tips to get better help with Performance Problems, click hereFor Running Totals and its variations, click here or when working with partitioned tablesFor more about Tally Tables, click hereFor more about Cross Tabs and Pivots, click here and hereManaging Transaction LogsSQL Musings from the Desert Fountain Valley SQL (My Mirror Blog) Maddave SSCarpal Tunnel Group: General Forum Members Points: 4466 Visits: 1487 It's data that has come from an external source and is different to what is required for the internal sequence. In the real system, the external sequence starts at a very large number and increases, but the internal sequence must start at 1. I wish I could just use the ext sequence but unfortunately the business/application rules forbid it. Lynn Pettis SSC Guru Group: General Forum Members Points: 225938 Visits: 40423 So something like this (run in an empty test database):`--Example tablecreate table dbo.Labels(Id int identity (1,1),Value varchar(20),IntSequence bigint,ExternalSequence bigint,BatchId int)-- Insert some dataInsert into dbo.Labels(Value, IntSequence, ExternalSequence, BatchId)Values--batch 1('exampleValue', 1,100001,1),('exampleValue', 2,100002,1),('exampleValue', 3,100003,1),--batch 2('exampleValue', 4,100006,2),('exampleValue', 5,100007,2),('exampleValue', 6,100008,2),-- batch 3('exampleValue', 7,100010,3),('exampleValue', 8,100011,3),-- Insert the missing values for batch 1('exampleValue', 9,100004,1),('exampleValue', 10,100005,1),-- insert the missing values for batch 2('exampleValue', 11,100009,2)select * from dbo.Labels order by IntSequence asc; -- Current order showing external seq in wrong order, but internal seq in correct order.select * from dbo.Labels order by ExternalSequence asc; -- Order by the other column which is in correct order shows true order, but internal sequence in wrong order with basedata as (select Value, IntSequence, ExternalSequence, BatchId, rn = row_number() over (order by BatchId, ExternalSequence)from dbo.Labels)update basedata set IntSequence = rn;select * from dbo.Labels order by IntSequence; -- Current order showing external seq in wrong order, but internal seq in correct order.select * from dbo.Labels order by ExternalSequence asc -- Order by the other column which is in correct order shows true order, but internal sequence in wrong order drop table dbo.Labels;` Lynn PettisFor better assistance in answering your questions, click hereFor tips to get better help with Performance Problems, click hereFor Running Totals and its variations, click here or when working with partitioned tablesFor more about Tally Tables, click hereFor more about Cross Tabs and Pivots, click here and hereManaging Transaction LogsSQL Musings from the Desert Fountain Valley SQL (My Mirror Blog) Maddave SSCarpal Tunnel Group: General Forum Members Points: 4466 Visits: 1487 Perfect! Thanks so much.I was on to something a tiny bit similar myself, but for some reason my code doesn't change the data at all.`declare @id bigintset @id = 0;With cte As(select IntSequence, ROW_NUMBER() OVER (order by externalsequence asc) as rownumber from labels)update cteset @id = IntSequence = @id + 1`That's a big help. Thanks again. Lynn Pettis SSC Guru Group: General Forum Members Points: 225938 Visits: 40423 This is all you needed:`With cte As(select IntSequence, ROW_NUMBER() OVER (order by externalsequence asc) as rownumber from labels)update cteset IntSequence = rownumber;` Lynn PettisFor better assistance in answering your questions, click hereFor tips to get better help with Performance Problems, click hereFor Running Totals and its variations, click here or when working with partitioned tablesFor more about Tally Tables, click hereFor more about Cross Tabs and Pivots, click here and hereManaging Transaction LogsSQL Musings from the Desert Fountain Valley SQL (My Mirror Blog) Maddave SSCarpal Tunnel Group: General Forum Members Points: 4466 Visits: 1487 Seems so simple now!Perfect thanks. Jeff Moden SSC Guru Group: General Forum Members Points: 511841 Visits: 44306 I might be missing something but I guess my question would be, why use the internal sequence at all? Just use the external sequence? --Jeff ModenRBAR is pronounced ree-bar and is a Modenism for Row-By-Agonizing-Row.First step towards the paradigm shift of writing Set Based code: Stop thinking about what you want to do to a row... think, instead, of what you want to do to a column.If you think its expensive to hire a professional to do the job, wait until you hire an amateur. -- Red Adair Helpful Links:How to post code problemsHow to post performance problemsForum FAQs

## Permissions

 You can't post new topics. You can't post topic replies. You can't post new polls. You can't post replies to polls. You can't edit your own topics. You can't delete your own topics. You can't edit other topics. You can't delete other topics. You can't edit your own posts. You can't edit other posts. You can't delete your own posts. You can't delete other posts. You can't post events. You can't edit your own events. You can't edit other events. You can't delete your own events. You can't delete other events. You can't send private messages. You can't send emails. You can read topics. You can't vote in polls. You can't upload attachments. You can download attachments. You can't post HTML code. You can't edit HTML code. You can't post IFCode. You can't post JavaScript. You can post emoticons. You can't post or upload images.

##### Select a forum
 SQL Server 2017      SQL Server 2017 - Administration      SQL Server 2017 - Development SQL Server 2016      SQL Server 2016 - Administration      SQL Server 2016 - Development and T-SQL SQL Server 2014      Administration - SQL Server 2014      Development - SQL Server 2014 SQL Server 2012      SQL 2012 - General      SQL Server 2012 - T-SQL SQL Server vNext      SQL Server 15 - Administration      SQL Server 15 - Development SQL Server 2008      SQL Server 2008 - General      T-SQL (SS2K8)      June 2007 CTP      Working with Oracle      July CTP      SQL Server Newbies      Security (SS2K8)      SQL Server 2008 High Availability      SQL Server 2008 Administration      Data Corruption (SS2K8 / SS2K8 R2)      SQL Server 2008 Performance Tuning Cloud Computing      SQL Azure - Development      SQL Azure - Administration      Amazon AWS and other cloud vendors      General Cloud Computing Questions      CosmosDB      Azure Data Lake      Azure Machine Learning Reporting Services      Reporting Services      Reporting Services 2005 Administration      Reporting Services 2005 Development      Reporting Services 2008/R2 Administration      Reporting Services 2008 Development      SSRS 2012      SSRS 2014      SSRS 2016 Programming      Connecting      General      SMO/RMO/DMO      XML      Service Broker      Powershell      Testing      TFS/Data Dude/DBPro      SSDT      Continuous Integration, Deployment, and Delivery      R Services and R Language Data Warehousing      Integration Services      Strategies and Ideas      Analysis Services      Data Transformation Services (DTS)      Performance Point      Data Mining      PowerPivot      R language      Machine Learning Database Design      Disaster Recovery      Design Ideas and Questions      Relational Theory      Hardware      Virtualization      Security and Auditing SQLServerCentral.com      Anything that is NOT about SQL!      Contests!      Editorials      SQLServerCentral.com Announcements      SQLServerCentral.com Website Issues      Suggestions      Tag Issues with Content      Podcast Feedback      SQLServerCentral.com Test Forum      Articles Requested SQL Server 2005      Administering      Backups      Business Intelligence      CLR Integration and Programming.      Data Corruption      Development      Working with Oracle      SQL Server 2005 Compact Edition      SQL Server 2005 General Discussion      SQL Server 2005 Security      SQL Server 2005 Strategies      SS2K5 Replication      SQL Server Express      SQL Server 2005 Performance Tuning      SQL Server 2005 Integration Services      T-SQL (SS2K5)      SQL Server Newbies SQL Server 7,2000      Administration      Backups      Data Corruption      General      Globalization      In The Enterprise      Working with Oracle      Security      Strategies      SQL Server Newbies      Service Packs      SQL Server CE      Performance Tuning      Replication      Sarbanes-Oxley      T-SQL      SQL Server Agent SQL Server and other platforms      MySQL      Oracle      PostgreSQL      DB2      SQL Server and Sharepoint Older Versions of SQL (v6.5, v6.0, v4.2)      Older Versions of SQL (v6.5, v6.0, v4.2) Career      Certification      Employers and Employees      Events      Job Postings      Resumes and Job Hunters      Presentations and Speaking      Retired Members Testing Center      SQL Server Security Skills      Question of the Day (QOD) Microsoft Access      Microsoft Access Products and Books      Third Party Products         SQLCentric         Extreme Technologies.         Innovartis         Embarcadero         SQL Sentry         Sonasoft         Golden Gate Software         Idera         Lumigent         Red Gate Software         ApexSQL         Quest Software         CA      Discussions about Books         Discuss Programming Books          Discuss XML Books          Discuss T-SQL Books          Discuss Data Warehousing Books          Discuss DTS Books          Discuss SQL Server 2000 Books         Discuss SQL Server 7.0 Books Notification Services      Administration Article Discussions Future Versions      SQL 12

## Search

 Copyright © 2002-2018 Redgate. All Rights Reserved. Privacy Policy. Terms of Use. Report Abuse.