?
Solved

renumbering records in MS Sql

Posted on 2011-03-25
2
Medium Priority
?
370 Views
Last Modified: 2012-05-11

Is there a way to change the numbers of if a row is deleted using MS sql stored procedure?

For example using the below table, If I delete the row that contains field1 = 15 and field2 =2. I want the field2 rows that contain the values of 3 and 4 to become 2 and 3. I want to add the code to a stored procedure after the row is deleted.

field1, field2, field3
15,      1,      sample                                          
15,      2,      example                                            
15,      3,      model                                            
15,      4,      illustation                                          

                             
0
Comment
Question by:kw66722
2 Comments
 
LVL 5

Accepted Solution

by:
bitref earned 2000 total points
ID: 35219262
Create a trigger (after delete) to call the following procedure:

Create proc xyz(@DeletedField as int)
Update MyTable
Set field2 = field2 - 1
Where field2 > @DeletedField

Open in new window

0
 
LVL 1

Author Closing Comment

by:kw66722
ID: 35219755
Worked like a charm.  Thanks.
0

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article explains how to reset the password of the sa account on a Microsoft SQL Server.  The steps in this article work in SQL 2005, 2008, 2008 R2, 2012, 2014 and 2016.
This post contains step-by-step instructions for setting up alerting in Percona Monitoring and Management (PMM) using Grafana.
In this video, Percona Solution Engineer Dimitri Vanoverbeke discusses why you want to use at least three nodes in a database cluster. To discuss how Percona Consulting can help with your design and architecture needs for your database and infras…
In this video, Percona Solution Engineer Rick Golba discuss how (and why) you implement high availability in a database environment. To discuss how Percona Consulting can help with your design and architecture needs for your database and infrastr…

840 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question