Click here to monitor SSC
SQLServerCentral is supported by Red Gate Software Ltd.
 
Log in  ::  Register  ::  Not logged in
 
 
 

Get your favorite SSC scripts directly in SSMS with the free SQL Scripts addin. Search for scripts directly from SSMS, and instantly access any saved scripts in your SSC briefcase from the favorites tab.
Download now (direct download link)

Find gaps in primary keys across multiple databases

By Richard White,

What it does: This script polls multiple databases to find gaps in a Primary Key ID in a table common to multiple databases. It is useful not only for finding gaps in PK's, but also for finding a single PK or group of PK's that are available in all databases. This is handy for being able to create a single script for adding new records that can run on multiple databases that have a common purpose and help maintain standardization between them. It is very convenient that it can be run from a local server and pull and gather data from remote servers, including linked servers to other database types.

Use: The script is parameterized so it can be easily edited to run against different tables and PK's. Edit the variables at the top to specify the table and primary key id names. There is also a variable for defining the minimum gap size to look for. This script is designed for four databases: 2 running separate SQL Servers and two running on separate Sybase servers (accessed via a linked server). Note, that for the SQL Server instances, I found that I neeed to re-declare the variables inside the dynamic SQL query. This section should be edited to fit your situation. I suppose the next best thing would be to install as a parameterized stored procedure.

Kudos: With the help that I gleaned from Alexander Kozak's analysis of finding "islands and gaps in sequential numbers" (ref: http://msdn.microsoft.com/en-us/library/aa175780(v=sql.80).aspx), I am able to greatly simplify the task of finding gaps and groups of available PK's in tables across multiple systems allowing me to write "one-size-fits-all" scripts.

Total article views: 807 | Views in the last 30 days: 1
 
Related Articles
ARTICLE

Manage Multiple SQL Server Installations and Databases with OSQL

This article explains how to use a simple batch file and script file to manage multiple databases on...

ARTICLE

Applying Your Scripts across Multiple SQL Server Databases

This review of RedGate's new SQL Multi Script tool that aids you in deploying a script across multip...

ARTICLE

Using "Multiple Assignment Variables" for a Generic Compare Data Script

Comparing data between tables seems like a simple thing to do, but it can eat up a lot of a SQL Serv...

ARTICLE

Deploying Scripts To Multiple Instances

Most DBAs manage multiple SQL servers, each handling different needs for your business. Even with th...

FORUM

Alter Database with variable

Alter Database with variable

Tags
data gaps    
primary key (pk)    
 
Contribute

Join the most active online SQL Server Community

SQL knowledge, delivered daily, free:

Email address:  

You make SSC a better place

As a member of SQLServerCentral, you get free access to loads of fresh content: thousands of articles and SQL scripts, a library of free eBooks, a weekly database news roundup, a great Q & A platform… And it’s our huge, buzzing community of SQL Server Professionals that makes it such a success.

Join us!

Steve Jones
Editor, SQLServerCentral.com

Already a member? Jump in:

Email address:   Password:   Remember me: Forgotten your password?
Steve Jones