Log in
::
Register
::
Not logged in
Home
Tags
Articles
Editorials
Stairways
Forums
Scripts
Videos
Blogs
QotD
Books
Ask SSC
SQL Jobs
Training
Authors
About us
Contact us
Newsletters
Write for us
Recent Posts
Recent Posts
Popular Topics
Popular Topics
Home
Search
Members
Calendar
Who's On
Home
»
SQL Server 2008
»
T-SQL (SS2K8)
»
Add login, user, roles in mult db's...
Add login, user, roles in mult db's dynamically
Rate Topic
Display Mode
Topic Options
Author
Message
SkyBox
SkyBox
Posted Wednesday, March 06, 2013 7:43 AM
SSC-Enthusiastic
Group: General Forum Members
Last Login: Thursday, May 16, 2013 12:12 PM
Points: 146,
Visits: 549
I am attempting to create T-SQL that I can re-use on different db’s, users, domains, roles, etc…
The trouble I’m having is when trying to change database names dynamically. I need the ability to hit multiple db’s in one shot. My goal for starters is to only change the username parameter for each user/executio and execute the rest.
I have attached the code. Any suggestions would be helpful - there must be a better way.
This is what’s failing after adding the +(@DB) variable:
set @USER = + QUOTENAME(@DB) + 'CREATE USER ' + QUOTENAME(@DN + @UN) + ' FOR LOGIN ' + quotename(@DN + @UN) + ' WITH DEFAULT_SCHEMA=[' + @DN + @UN + ']' --db1
Post Attachments
createUserSample.txt
(
8 views,
2.50 KB
)
Post #1427410
Gazareth
Gazareth
Posted Wednesday, March 06, 2013 7:51 AM
UDP Broadcaster
Group: General Forum Members
Last Login: Wednesday, May 15, 2013 6:22 AM
Points: 1,474,
Visits: 2,341
QUOTENAME (@DB) is resulting in [USE MyDB] - needs to be USE [MyDB]
Post #1427417
SkyBox
SkyBox
Posted Wednesday, March 06, 2013 8:16 AM
SSC-Enthusiastic
Group: General Forum Members
Last Login: Thursday, May 16, 2013 12:12 PM
Points: 146,
Visits: 549
Gazareth (3/6/2013)
QUOTENAME (@DB) is resulting in [USE MyDB] - needs to be USE [MyDB]
Good catch! Still open to a better overall approach for this.
Post #1427445
Gazareth
Gazareth
Posted Wednesday, March 06, 2013 8:32 AM
UDP Broadcaster
Group: General Forum Members
Last Login: Wednesday, May 15, 2013 6:22 AM
Points: 1,474,
Visits: 2,341
Approach is fine. The other way to do it if possible would be to give role membership(s) to AD group(s), and control user membership in AD.
That way you won't need to create a new user everywhere all the time.
Or look into using powershell - might give you a slightly tidier script that can easily be pointed at multiple servers.
Post #1427456
SkyBox
SkyBox
Posted Wednesday, March 06, 2013 10:07 AM
SSC-Enthusiastic
Group: General Forum Members
Last Login: Thursday, May 16, 2013 12:12 PM
Points: 146,
Visits: 549
Gazareth (3/6/2013)
The other way to do it if possible would be to give role membership(s) to AD group(s), and control user membership in AD.
That way you won't need to create a new user everywhere all the time.
AD groups would be ideal.
Post #1427512
Gazareth
Gazareth
Posted Thursday, March 07, 2013 3:08 AM
UDP Broadcaster
Group: General Forum Members
Last Login: Wednesday, May 15, 2013 6:22 AM
Points: 1,474,
Visits: 2,341
It's a good way to go about it. A fair bit of work to set up, but much easier after that!
Post #1427863
« Prev Topic
|
Next Topic »
Permissions
You
cannot
post new topics.
You
cannot
post topic replies.
You
cannot
post new polls.
You
cannot
post replies to polls.
You
cannot
edit your own topics.
You
cannot
delete your own topics.
You
cannot
edit other topics.
You
cannot
delete other topics.
You
cannot
edit your own posts.
You
cannot
edit other posts.
You
cannot
delete your own posts.
You
cannot
delete other posts.
You
cannot
post events.
You
cannot
edit your own events.
You
cannot
edit other events.
You
cannot
delete your own events.
You
cannot
delete other events.
You
cannot
send private messages.
You
cannot
send emails.
You
may
read topics.
You
cannot
rate topics.
You
cannot
vote within polls.
You
cannot
upload attachments.
You
may
download attachments.
You
cannot
post HTML code.
You
cannot
edit HTML code.
You
cannot
post IFCode.
You
cannot
post JavaScript.
You
cannot
post EmotIcons.
You
cannot
post or upload images.
Copyright © 2002-2013 Simple Talk Publishing. All Rights Reserved.
Privacy Policy.
Terms of Use.
Report Abuse.