Solved

Don''t create if already exists

Posted on 2006-11-14
10
344 Views
Last Modified: 2012-06-21
Got the script below:
However, I do not want to create this login and assign access and roles
if they already exist. How would I modify this script to take this into
account? Using MSDE, MSSQL 2000 and 2005 Express.

EXEC Master..sp_addlogin 'SW', 'SWPWD', 'MASTER', 'us_english', 0x5CE521831B15A3928B66002DCC774486

--Grant access to database SW
IF EXISTS (select * from dbo.sysdatabases where [name]='SW')
BEGIN
  EXEC SW..sp_grantdbaccess 'SW', 'SW'
  EXEC SW..sp_addsrvrolemember 'SW', 'sysadmin'
END

GO
 
 
0
Comment
Question by:EddieShipman
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 4
10 Comments
 
LVL 16

Expert Comment

by:Hillwaaa
ID: 17942377
Hi EddieShipman,

You could do something like:

if (select count(*) from syslogins where name = 'SW' ) = 0
BEGIN

... add your code here ...

END

Cheers!
0
 
LVL 26

Author Comment

by:EddieShipman
ID: 17942570
I have no SW user in the syslogins table in my master database but the login DOES exist.
0
 
LVL 16

Expert Comment

by:Hillwaaa
ID: 17942641
OK, you can also check if they are a database user using the same code structure, but looking in the 'sysusers' table instead of 'syslogins' - does that help?
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 26

Author Comment

by:EddieShipman
ID: 17942846
nope
0
 
LVL 26

Author Comment

by:EddieShipman
ID: 17942849
The user may already exist yet the role and access has not been granted. I need checks for all of those.
0
 
LVL 16

Expert Comment

by:Hillwaaa
ID: 17943818
Eddie,

Sorry about the delay on this...

you can manually check these things using
sp_helpuser, sp_helpsrvrole and sp_helpsrvrolemember.

Can you please run

"exec sp_helpuser" and let me know what the row with 'SW' contains?
0
 
LVL 26

Author Comment

by:EddieShipman
ID: 17947602
Nevermind, I figured it out on my own...

IF NOT EXISTS (select * from dbo.sysxlogins where [name]='SW')
BEGIN
  EXEC Master..sp_addlogin 'SW', 'SWPWD', 'MASTER', 'us_english', 0x5CE521831B15A3928B66002DCC774486
END

IF EXISTS (select * from dbo.sysdatabases where [name]='SW')
BEGIN
  PRINT REPLICATE('-',70)

  IF (select hasdbaccess from SW..sysusers where name = 'SW') = 0
  BEGIN
    PRINT 'Granting Access to SW DB'
    EXEC SW..sp_grantdbaccess 'SW', 'SW'
  END

  IF EXISTS (select lgn.name from master.dbo.spt_values spv,
                   master.dbo.sysxlogins lgn
                   where spv.type = 'SRV' and spv.name = 'sysadmin' and lgn.name = 'SW')
  BEGIN                
    PRINT 'Adding sysadmin role to SW user for SW DB'
    EXEC SW..sp_addsrvrolemember 'SW', 'SW'
  END
END
0
 
LVL 16

Expert Comment

by:Hillwaaa
ID: 18050837
DarthMod - ok to refund by me - Eddie has answered his own question.
0
 
LVL 1

Accepted Solution

by:
DarthMod earned 0 total points
ID: 18076857
PAQd, 500 points refunded.

DarthMod
CS Moderator
0

Featured Post

Webinar: Aligning, Automating, Winning

Join Dan Russo, Senior Manager of Operations Intelligence, for an in-depth discussion on how Dealertrack, leading provider of integrated digital solutions for the automotive industry, transformed their DevOps processes to increase collaboration and move with greater velocity.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction SQL Server Integration Services can read XML files, that’s known by every BI developer.  (If you didn’t, don’t worry, I’m aiming this article at newcomers as well.) But how far can you go?  When does the XML Source component become …
The Delta outage: 650 cancelled flights, more than 1200 delayed flights, thousands of frustrated customers, tens of millions of dollars in damages – plus untold reputational damage to one of the world’s most trusted airlines. All due to a catastroph…
Familiarize people with the process of utilizing SQL Server functions from within Microsoft Access. Microsoft Access is a very powerful client/server development tool. One of the SQL Server objects that you can interact with from within Microsoft Ac…
This video shows, step by step, how to configure Oracle Heterogeneous Services via the Generic Gateway Agent in order to make a connection from an Oracle session and access a remote SQL Server database table.

726 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question