SQL Clone
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


creating SQL login with cursor


creating SQL login with cursor

Author
Message
paul 77096
paul 77096
Valued Member
Valued Member (54 reputation)Valued Member (54 reputation)Valued Member (54 reputation)Valued Member (54 reputation)Valued Member (54 reputation)Valued Member (54 reputation)Valued Member (54 reputation)Valued Member (54 reputation)

Group: General Forum Members
Points: 54 Visits: 6
Hi I have 100 or so users in a database but I need them all to have a server login also

I was thinking of doing this in a cursor and have come up with


Declare @username varchar(50);

declare updateServerroles cursor for
SELECT name FROM sysusers where name = 'TEST\user'


open updateServerroles

fetch next from updateServerroles
into @username

While @@FETCH_STATUS = 0
begin

CREATE LOGIN @username FROM WINDOWS WITH DEFAULT_DATABASE=[***********], DEFAULT_LANGUAGE=[us_english]

exec sp_addrolemember db_datareader, @username
exec sp_addrolemember db_datawriter, @username


end
close updateServerroles
DEALLOCATE updateServerroles



but the 'create login' will not work. Please can someone give me some advise

thanks in advance

Paul
Bhuvnesh
Bhuvnesh
SSChampion
SSChampion (12K reputation)SSChampion (12K reputation)SSChampion (12K reputation)SSChampion (12K reputation)SSChampion (12K reputation)SSChampion (12K reputation)SSChampion (12K reputation)SSChampion (12K reputation)

Group: General Forum Members
Points: 12528 Visits: 4077
try this in your cursor code


DECLARE @SQL NVARCHAR(4000);

While
begin

SET NOCOUNT ON

SET @SQL = 'CREATE LOGIN ' + @username + ' WITH PASSWORD = ''12345'', DEFAULT_DATABASE=[dbname], DEFAULT_LANGUAGE=[British], CHECK_EXPIRATION=OFF, CHECK_POLICY=ON';
EXECUTE(@SQL);

exec sp_addrolemember db_datareader, @username ;
exec sp_addrolemember db_datawriter, @username ;

end




-------Bhuvnesh----------
I work only to learn Sql Server...though my company pays me for getting their stuff done;-)
paul 77096
paul 77096
Valued Member
Valued Member (54 reputation)Valued Member (54 reputation)Valued Member (54 reputation)Valued Member (54 reputation)Valued Member (54 reputation)Valued Member (54 reputation)Valued Member (54 reputation)Valued Member (54 reputation)

Group: General Forum Members
Points: 54 Visits: 6
Thats Brilliant, it worked like a dream. Thanks

Paul
Bhuvnesh
Bhuvnesh
SSChampion
SSChampion (12K reputation)SSChampion (12K reputation)SSChampion (12K reputation)SSChampion (12K reputation)SSChampion (12K reputation)SSChampion (12K reputation)SSChampion (12K reputation)SSChampion (12K reputation)

Group: General Forum Members
Points: 12528 Visits: 4077
paul 77096 (1/9/2013)
Thats Brilliant, it worked like a dream.
in future try to avoid cursors instead use loop or set based approach. why ???? google it :-)

-------Bhuvnesh----------
I work only to learn Sql Server...though my company pays me for getting their stuff done;-)
atapia_
atapia_
Old Hand
Old Hand (377 reputation)Old Hand (377 reputation)Old Hand (377 reputation)Old Hand (377 reputation)Old Hand (377 reputation)Old Hand (377 reputation)Old Hand (377 reputation)Old Hand (377 reputation)

Group: General Forum Members
Points: 377 Visits: 71
Hi, very true!

Cursors aren’t all bad. In fact, they can make some database problems a lot easier to solve. But take a step back and really examine the problem. There is a chance that you can use a more efficient set-based approach to solve your problem.

Ing. Aneurys Tapia González
DBA + BI Architect
Dominican Republic
atapia@outlook.com | admin@atapia.net
http://sqlservermasbi.wordpress.com
http://atapia.net
CapnHector
CapnHector
Hall of Fame
Hall of Fame (3K reputation)Hall of Fame (3K reputation)Hall of Fame (3K reputation)Hall of Fame (3K reputation)Hall of Fame (3K reputation)Hall of Fame (3K reputation)Hall of Fame (3K reputation)Hall of Fame (3K reputation)

Group: General Forum Members
Points: 3037 Visits: 1789
Bhuvnesh (1/9/2013)
paul 77096 (1/9/2013)
Thats Brilliant, it worked like a dream.
in future try to avoid cursors instead use loop or set based approach. why ???? google it :-)


Cursors VS Loops is an argument that will never be won. a cursor can be faster than a while loop if written correctly. This is one place where a loop or cursor is appropriate as each user needs to be run and is by its nature an RBAR process.


For faster help in answering any problems Please read How to post data/code on a forum to get the best help - Jeff Moden for the best way to ask your question.

For performance Issues see how we like them posted here: How to Post Performance Problems - Gail Shaw

Need to Split some strings? Jeff Moden's DelimitedSplit8K
Jeff Moden's Cross tab and Pivots Part 1
Jeff Moden's Cross tab and Pivots Part 2
ScottPletcher
ScottPletcher
SSCoach
SSCoach (18K reputation)SSCoach (18K reputation)SSCoach (18K reputation)SSCoach (18K reputation)SSCoach (18K reputation)SSCoach (18K reputation)SSCoach (18K reputation)SSCoach (18K reputation)

Group: General Forum Members
Points: 18604 Visits: 7402
I, too, don't have a problem with a cursor per se here; it could be done w/o one, but it's not a big deal for such a limited number of rows.

You should get away from "sysusers", which is also obsolete, but again that's not a killer issue either.

But, the cursor should be optimized with FAST_FORWARD:


...
declare updateServerroles cursor fast_foward for SELECT name FROM sysusers where name = 'TEST\user'
...



Edit: Corrected typo.

SQL DBA,SQL Server MVP(07, 08, 09) Prosecutor James Blackburn, in closing argument in the Fatal Vision murders trial:
If in the future, you should cry a tear, cry one for them [the murder victims]. If in the future, you should say a prayer, say one for them. And if in the future, you should light a candle, light one for them.
Bhuvnesh
Bhuvnesh
SSChampion
SSChampion (12K reputation)SSChampion (12K reputation)SSChampion (12K reputation)SSChampion (12K reputation)SSChampion (12K reputation)SSChampion (12K reputation)SSChampion (12K reputation)SSChampion (12K reputation)

Group: General Forum Members
Points: 12528 Visits: 4077
ScottPletcher (1/9/2013)
the cursor should be optimized with FAST_FORWARD:
Any article reference for this ?

-------Bhuvnesh----------
I work only to learn Sql Server...though my company pays me for getting their stuff done;-)
ScottPletcher
ScottPletcher
SSCoach
SSCoach (18K reputation)SSCoach (18K reputation)SSCoach (18K reputation)SSCoach (18K reputation)SSCoach (18K reputation)SSCoach (18K reputation)SSCoach (18K reputation)SSCoach (18K reputation)

Group: General Forum Members
Points: 18604 Visits: 7402
Bhuvnesh (1/9/2013)
ScottPletcher (1/9/2013)
the cursor should be optimized with FAST_FORWARD:
Any article reference for this ?



Books Online.

SQL DBA,SQL Server MVP(07, 08, 09) Prosecutor James Blackburn, in closing argument in the Fatal Vision murders trial:
If in the future, you should cry a tear, cry one for them [the murder victims]. If in the future, you should say a prayer, say one for them. And if in the future, you should light a candle, light one for them.
Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum

































































































































































SQLServerCentral


Search