Solved

Don''t create if already exists

Posted on 2006-11-14
10
312 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
  • 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
 
LVL 26

Author Comment

by:EddieShipman
ID: 17942846
nope
0
Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 
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

What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

Join & Write a Comment

Suggested Solutions

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 …
Everyone has problem when going to load data into Data warehouse (EDW). They all need to confirm that data quality is good but they don't no how to proceed. Microsoft has provided new task within SSIS 2008 called "Data Profiler Task". It solve th…
Using examples as well as descriptions, and references to Books Online, show the different Recovery Models available in SQL Server and explain, as well as show how full, differential and transaction log backups are performed
Via a live example, show how to set up a backup for SQL Server using a Maintenance Plan and how to schedule the job into SQL Server Agent.

743 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

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now