Find Rules and Defaults using the PowerShell for SQL Server 2008 Provider

Posted by BuckWoody on SQL Blog See other posts from SQL Blog or by BuckWoody
Published on Tue, 01 Jun 2010 13:53:38 GMT Indexed on 2010/06/01 15:05 UTC
Read the original article Hit count: 317

Filed under:
|

I ran into an issue the other day where I couldn't set up some features in SQL Server 2008 because they ddon't support the use of Rules or Defaults. Let me explain a little more about that. In older versions of SQL Server, you could decalre a "Rule" or "Default" just like you do with a Table Constraint today. You would then "bind" these rules or defaults to the tables you wanted them to apply to. Sure, there are advantages and disadvantages to this approach, but it certainly isn't standard Data Definition Language (DDL), so they are deprecated and many features don't work with them any more. Honestly, it's been so long since I've seen them in use I had forgotten to even check for them. My suspicion is that this was a new database created with an older script. Nevertheless, the feature failed when it ran into one.

Immediately I thought that I had better build some logic into my process to try and catch those - but how? Lots of choices here, but since I was using PowerShell to do the rest of the work, I thought I would investigate how easy it would be just to do it there. And using the SQL Server 2008 provider, this could not be simpler. I won't show all of the scrupt here, because I was testing for these as a condition and then bailing out of the script and sending a notification, but all it is using is the DIR command!

Here's an example on my "UNIVAC" computer for the "pubs" database:

Find Rules using PowerShell:
dir SQLSERVER:\SQL\UNIVAC\DEFAULT\Databases\pubs\Rules
dir SQLSERVER:\SQL\UNIVAC\DEFAULT\Databases\pubs\Defaults

And this one will look in all databases: 

#All Databases:
dir SQLSERVER:\SQL\UNIVAC\DEFAULT\Databases | select-object -property Name, Rules, Defaults

Awesome. Love me some PowerShell.

Script Disclaimer, for people who need to be told this sort of thing:

Never trust any script, including those that you find here, until you understand exactly what it does and how it will act on your systems. Always check the script on a test system or Virtual Machine, not a production system. Yes, there are always multiple ways to do things, and this script may not work in every situation, for everything. It’s just a script, people. All scripts on this site are performed by a professional stunt driver on a closed course. Your mileage may vary. Void where prohibited. Offer good for a limited time only. Keep out of reach of small children. Do not operate heavy machinery while using this script. If you experience blurry vision, indigestion or diarrhea during the operation of this script, see a physician immediately.

 

 

 


© SQL Blog or respective owner

Related posts about powershell

Related posts about SQL Server