1

I have a database with 300 tables. I need to clean the tables, so I have written a script which drops all of the constraints, triggers, primary keys, and foreign keys.

I have managed to create scripts using the generate scripts feature, but they only include primary keys inside the CREATE statements.

I need to get them as ALTER TABLE statements so that I can drop the keys, clear database, insert new data, and restore all the keys, constraints, etc.

2
  • 2
    Why do you need to drop the constraints to clear the tables and insert data?
    – jle
    Mar 5, 2012 at 20:15
  • As @jle-s comment alludes to - a PK constraint is no obstacle to deletion - so maybe don't drop them in the first case? (Equally, any Unique constraints) Mar 6, 2012 at 20:24

2 Answers 2

1

Powershell and SMO are going to be your friends here:

$option_drop = new-object Microsoft.SqlServer.Management.Smo.ScriptingOptions;
$option_drop.ScriptDrops = $true;

"" > drop_primary_keys.sql
"" > create_primary_keys.sql

$server = new-object Microsoft.SqlServer.Management.Smo.Server ".";
$db = $server.Databases['AdventureWorks'];
foreach ($table in $db.Tables) {
    foreach ($index in $table.Indexes) {
        if ($index.IndexKeyType -eq "DriPrimaryKey") {
            $index.Script( $option_drop ) >> drop_primary_keys.sql
            $index.Script() >> create_primary_keys.sql
        }
    }
}

A couple of notes here:

  • Running this script will nuke any existing files of the name "drop_primary_keys.sql" and "create_primary_keys.sql", so proceed with caution
  • The script doesn't take into account any foreign keys since you said you already have a way to do that.
  • You may have to tweak the ScriptingOptions object to fit your needs. Specifically, I'm using the defaults on the create, so you may need to create another ScriptingOptions object and set whichever options you think appropriate.

Other than that, good hunting.

6
  • One other thing to consider: if your primary key is also your clustered index, it'll speed things up if you either disable or drop any non-clustered indexes on the table since dropping the clustered index will force a rebuild of all of the non-clustered indexes as will adding a clustered index to a heap.
    – Ben Thul
    Mar 6, 2012 at 16:57
  • Thanks. I get an error though: Property 'IndexKeyType' cannot be found on this object; make sure it exists and is settable. Very strange, since the docs for Microsoft.SqlServer.Management.Smo.Index clearly say that there should be such a property
    – Kir
    Mar 6, 2012 at 20:06
  • Oh I see what's wrong... the = should be -eq. Otherwise, the output looks good so far. Will post back when I have time to test it. Thanks Ben
    – Kir
    Mar 6, 2012 at 20:12
  • Ah crap... you're right. I thought I'd run it from where I copy/pasted it, but apparently not. Sorry for the hassle.
    – Ben Thul
    Mar 6, 2012 at 20:20
  • It was no hassle. This way I took some time to understand the script instead of just running it:). Everything worked perfectly. Answer accepted. Out of curiosity, do you know if this API has some way of doing UNIQUE keys? I did those by hand because there was only a handful, but I couldn't find anything in these classes that would let me do it automatically.
    – Kir
    Mar 12, 2012 at 13:27
0

Msdn has an article about disabling/enabling triggers and foreign keys:

http://msdn.microsoft.com/en-us/magazine/cc163442.aspx

3
  • This is not at all what I'm looking for. I've already got several ways of disabling triggers and such. none of them work particularly well because you can't truncate even with foreign keys disabled -you have to delete, which takes forever on 300 HUGE tables. I'm asking how to script all the PRIMARY keys as ALTER statements, so that I can add them back after I do what I need to.\
    – Kir
    Mar 5, 2012 at 20:34
  • You just have to modify the above code to use KEY_COLUMN_USAGE and REFERENTIAL_CONSTRAINTS and modify the ALTER statement. The code for the triggers should be valid. You should just generate the ALTER statements to create your keys before you drop them.
    – Eggi
    Mar 5, 2012 at 20:48
  • How to generate the primary keys ALTER statements is my question. If I knew how to do that I wouldn't be asking. I already know how to drop constraints and primary keys.
    – Kir
    Mar 6, 2012 at 14:00

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.