Other: Creating Unique Display Numbers
The following script will create unique Final_ReferenceCode for all Contacts. It can be easily modified to apply to other record types.
Please note that the Final_ReferenceCode that are generated are based on the data in the database.
If you are doing a delta migration, you will likely want to run a script to copy display numbers from the alpha to the delta (much like Result_Ids are) otherwise records may have different display numbers between the alpha and delta.
- --This is the pattern you want DisplayNumbers to follow.
- DECLARE @FORMAT NVARCHAR(MAX) = 'C0000#'
-
--This pattern means:
- -- * Use a 'C' followed by 5 numbers.
- -- * If we have a number that is less than 5 digits, prefix
with 0.
-
- --This is the lowest number we want our series to use
- DECLARE @SERIES_START INT = 1000
-
- --ANYTHING BELOW THIS LINE WILL NOT GENERALLY NEED TO BE
MODIFIED
-
- /*
- In the
following code we will essentially work with three tables.
- __M_Contacts
is the table we are assigning display numbers to.
- @DisplayNumbers
is a [Row_Number(), DisplayNumber]
- @Rows is a
[Row_Number(), __M_Contacts.Row]
-
- The code
works by doing the following:
- 1.0 Generate a list of display numbers
(@DisplayNumbers)
- 1.1 Remove any from the list that are already in
use
- 1.2 Assign a Row_Number() [Temp_Row] to anything
in the list. This will result in the
Row_Numbers starting at 1 and there being no holes.
-
- 2.0 Generate the list of rows we need to update
(@Rows)
- 2.1 Get a list of all __M_Contacts that dont have
a display number
- 2.2 Assign a Row_Number() [Temp_Row] to anything
in the list. This will result in the
Row_Numbers starting at 1 and there being no holes.
-
- 3.0 Because @Rows and @DisplayNumbers now both
have a [Temp_Row] with no holes, we will match using that value
-
-
- */
-
- --1.0 Generate a list
of display numbers (@DisplayNumbers)
- --We will generate 2n display numbers because some might
already be used.
- DECLARE @SERIES_COUNT INT = (SELECT 2 * COUNT(*) FROM
__M_Contacts)
-
- --Generate the list
- DECLARE @DisplayNumbers TABLE(Temp_Row INT INDEX IX1,
New_DisplayNumber NVARCHAR(MAX))
- INSERT INTO @DisplayNumbers
- SELECT
- 0,
FORMAT(V1.value, @FORMAT)
- FROM
- generate_series(@SERIES_START,
@SERIES_START + @SERIES_COUNT) V1
-
- --1.1 Remove any from
the list that are already in use
- DELETE FROM
- @DisplayNumbers
- WHERE New_DisplayNumber IN (
- SELECT
- Final_ReferenceCode
- FROM
- __M_Contacts
- )
-
- --1.2 Assign a
Row_Number() [Temp_Row] to anything in the list. This will result in the Row_Numbers starting
at 1 and there being no holes.
- UPDATE
- @DisplayNumbers
- SET
- Temp_Row =
V2.Temp_Row
- FROM
- @DisplayNumbers
V1,
- (
- SELECT
- ROW_NUMBER()
over(order by New_DisplayNumber) as Temp_Row,
- New_DisplayNumber
- FROM
- @DisplayNumbers
- ) V2
- WHERE 1=1
- AND
V1.New_DisplayNumber = V2.New_DisplayNumber
-
-
-
- --2.0 Generate the
list of rows we need to update (@Rows)
- --2.1 Get a list of
all __M_Contacts that dont have a display number
- --2.2 Assign a
Row_Number() [Temp_Row] to anything in the list. This will result in the Row_Numbers starting
at 1 and there being no holes.
- DECLARE @Rows TABLE(Temp_Row INT INDEX IX1, Row INT INDEX
IX2)
- INSERT INTO @Rows
- SELECT
- ROW_NUMBER()
OVER(ORDER BY Final_Created_At, Id),
- Row
- FROM
- __M_Contacts
- WHERE 1=1
- AND Final_ReferenceCode = ''
-
-
- --3.0 Because @Rows
and @DisplayNumbers now both have a [Temp_Row] with no holes, we will match
using that value
- UPDATE
- __M_Contacts
- SET
- Final_ReferenceCode = V3.New_DisplayNumber
- FROM
- __M_Contacts
V1,
- @Rows V2,
- @DisplayNumbers
V3
- WHERE 1=1
- AND V1.Row
= V2.Row
- And
V2.Temp_Row = V3.Temp_Row
Related Articles
Matters: Trimming Client Numbers from Matter Numbers
The following script will peel off client numbers from matter numbers. For example, if a matter has a ReferenceCode like: ABC-0001 After the script runs, it will have a reference code of: 0001 Scenario 1: There are Client Reference Codes In some ...
Custom Fields: Creating Custom Fields via Queries
Sometimes you may need to create a new custom field to handle an unmapped column in a database. The queries below will help you do that. -- This is the ID we are giving our custom field definition. DECLARE @FieldId NVARCHAR(MAX) = 'ConNo --- CUSTOM' ...
Create a "Legacy Matter Number" Custom Field
Some firms rely heavily on Matter Numbers to search for a specific matter. Sometimes when they migrate to a new system it can't replicate the automatic numbering system from their old system so they want to renumber all their matters to a new scheme ...
Detecting Duplicates by Reference Code
In some scenarios, you need to ensure that a reference code is completely unique among a record type. To do this, use the following SQL script: --Look for duplicate records in the __M_Matters table SELECT * FROM --Change this to the table we want to ...
Create a "Legacy Originating Attorney" field (from __M_Matters_Participating_Entities)
Most destination systems will only let you set the Originating Attorney and Responsible Attorney to an active user. This is why user mapping is used to map inactive users from the legacy system to an active user in the new system. But the firm may ...