Solved

Don''t create if already exists

Posted on 2006-11-14
10
332 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
Windows Server 2016: All you need to know

Learn about Hyper-V features that increase functionality and usability of Microsoft Windows Server 2016. Also, throughout this eBook, you’ll find some basic PowerShell examples that will help you leverage the scripts in your environments!

 
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

Optimizing Cloud Backup for Low Bandwidth

With cloud storage prices going down a growing number of SMBs start to use it for backup storage. Unfortunately, business data volume rarely fits the average Internet speed. This article provides an overview of main Internet speed challenges and reveals backup best practices.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
DTS Connection Failed 7 70
query execution hang 5 29
Run SQL Server Proc from Access 11 31
Present Absent from working date rage 11 21
I wrote this interesting script that really help me find jobs or procedures when working in a huge environment. I could I have written it as a Procedure but then I would have to have it on each machine or have a link to a server-related search that …
Introduction In my previous article (http://www.experts-exchange.com/Microsoft/Development/MS-SQL-Server/SSIS/A_9150-Loading-XML-Using-SSIS.html) I showed you how the XML Source component can be used to load XML files into a SQL Server database, us…
Familiarize people with the process of retrieving data from SQL Server using an Access pass-thru query. Microsoft Access is a very powerful client/server development tool. One of the ways that you can retrieve data from a SQL Server is by using a pa…
Viewers will learn how to use the UPDATE and DELETE statements to change or remove existing data from their tables. Make a table: Update a specific column given a specific row using the UPDATE statement: Remove a set of values using the DELETE s…

773 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