SQL Server, PASS, and other data mishaps
Database Automagic
This months TSQL Tuesday is hosted by a good friend Pat right over at SQL Asylum
For this months entry I decided to keep it short and sweet, following in my Bits N Bytes theme.
The Meta Script
In the true sense of the word automation, this really doesn’t fit but, in the terms of quickly getting something done that would otherwise be a mundane repetitive task, this can save a world of time.
Lets say we have a list of objects in the Sales Schema and we have a request to grant Select and Insert access to a user for those objects. There are two approaches, 1 is to grant select and insert to the actual schema like this
GRANT SELECT, INSERT ON SCHEMA::Sales TO BusinessUser
However you might decide that you only want to grant direct SELECT and INSERT on the tables that exist in the DBO Schema today not those tables which may be created in the future (auditors love to make us do this)
A simple way to automate granting these rights is by writing a script that writes a script like so
ON obj.schema_id = sch.schema_id
and obj.type = ‘U’
This should give you a result set that looks something like the following:
GRANT SELECT, INSERT ON Sales.People TO BusinessUser
GRANT SELECT, INSERT ON Sales.Sales TO BusinessUser
At this point, run the output in a separate command window and viola you’ve automated that grant of permissions
This may not be true “automation” in the sense that Pat was looking for but, perfecting the ability to write scripts that write scripts is a huge timesaver
| Print article | This entry was posted by Allen Kinsel on February 8, 2011 at 11:11 am, and is filed under Bits N Bytes, SQL Server, Syndicated, TSQLTuesday. Follow any responses to this post through RSS 2.0. You can leave a response or trackback from your own site. |







about 2 years ago
You sell yourself short Allen. That is a great tip, and very useful. I use it fairly regularly, and constantly run into places where my fellow dba’s are doing so (Kendra) and wish I’d thought of it.
about 2 years ago
Thanks John, Im sometimes amazed when I see what others do with these sorts of generic tips. Saw a script made by another DBA in my environment yesterday that I need to figure out how to tone down a bit and post up here, it was simply amazing
about 2 years ago
mad love for you tonight! This was exactly what I needed quickly create a script to update 73 columns to default values
about 2 years ago
Glad it worked! Wait till you find yourself writing 1000 line scripts like this, you’ll hate me