﻿<?xml version='1.0' encoding='UTF-8'?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/"><channel><title>SQLServerCentral / SQL Server 2005 / SQL Server 2005 Security  / Create Login Script / Latest Posts</title><generator>InstantForum.NET v2.9.0</generator><description>SQLServerCentral</description><link>http://www.sqlservercentral.com/Forums/</link><webMaster>notifications@sqlservercentral.com</webMaster><lastBuildDate>Tue, 21 May 2013 10:04:27 GMT</lastBuildDate><ttl>20</ttl><item><title>RE: Create Login Script</title><link>http://www.sqlservercentral.com/Forums/Topic497615-359-1.aspx</link><description>[quote][b]K. Brian Kelley (5/10/2008)[/b][hr]The problem is that CREATE LOGIN and ALTER LOGIN won't take the variable. The way around this is to use Dynamic SQL. For instance:[code]CREATE PROCEDURE dbo.Create_Login	@username sysnameASBEGIN	SET NOCOUNT ON	DECLARE @SQL NVARCHAR(4000);		SET @SQL = 'CREATE LOGIN ' + @username + ' WITH PASSWORD = ''12345'', DEFAULT_DATABASE=[dbname], DEFAULT_LANGUAGE=[British], CHECK_EXPIRATION=OFF, CHECK_POLICY=ON';	EXECUTE(@SQL);		EXEC sys.sp_addsrvrolemember @loginame = @username, @rolename = N'sysadmin';		SET @SQL = 'ALTER LOGIN ' + @username + ' DISABLE';	EXECUTE(@SQL);END;[/code][/quote]Brian, now I realise why I could not get your script working I for got the brackets in "EXECUTE(@SQL);".</description><pubDate>Fri, 13 Apr 2012 14:17:18 GMT</pubDate><dc:creator>Manie Verster</dc:creator></item><item><title>RE: Create Login Script</title><link>http://www.sqlservercentral.com/Forums/Topic497615-359-1.aspx</link><description>Hi guys,Sorry for never giving feedback here but work has been hectic taking all my free time but no, sorry Brian I could not get your script going and was strapped for time at the time and got a different script and here it is.[code="sql"]declare @userid sysname = 'jon', @password sysname = 'password', @sqlstr nvarchar(250)      exec sp_addlogin @loginame = @userid,		@passwd = @password,		@defdb = 'ToolboxDB',		@deflanguage = [British],		@sid = null,		@encryptopt= null      EXEC sys.sp_addsrvrolemember @userid, @rolename = N'sysadmin'USE [ToolboxDB]set @sqlstr = 'CREATE USER '+@userid+' FOR LOGIN '+@useridEXECUTE sp_executesql @sqlstr[/code]Allow me to explain. The top part creates a login in sql server (not the database). You will see a security folder just below the database folder as follows:[code="sql"]exec sp_addlogin @loginame = @userid,		@passwd = @password,		@defdb = 'ToolboxDB',		@deflanguage = [British],		@sid = null,		@encryptopt= null[/code]The following part adds the login to a server role. I have sysadmin here but you can change that according to your own needs.[code="sql"]      EXEC sys.sp_addsrvrolemember @userid, @rolename = N'sysadmin'[/code]You can also add it to more than one role if needed. The following part maps your login to a specific database and here you can also use more than one database just put the code in for each database.[code="sql"]USE [ToolboxDB]set @sqlstr = 'CREATE USER '+@userid+' FOR LOGIN '+@useridEXECUTE sp_executesql @sqlstr[/code]Check out the link below for more on sp_executesql but the reason why I used this is because when I used Brian's script I kept getting "Cannot find the stored procedure ..........".[url=http://http://www.mssqltips.com/sqlservertip/1160/execute-dynamic-sql-commands-in-sql-server/][/url]</description><pubDate>Fri, 13 Apr 2012 14:13:09 GMT</pubDate><dc:creator>Manie Verster</dc:creator></item><item><title>RE: Create Login Script</title><link>http://www.sqlservercentral.com/Forums/Topic497615-359-1.aspx</link><description>I'm looking for a similar thing but can't get this to work.Can anyone think of a reason why this won't work in 2008 R2?Does it need any alterations to work properly?</description><pubDate>Thu, 12 Apr 2012 04:15:43 GMT</pubDate><dc:creator>richardmgreen1</dc:creator></item><item><title>RE: Create Login Script</title><link>http://www.sqlservercentral.com/Forums/Topic497615-359-1.aspx</link><description>[quote][b]david.griffiths 57552 (11/29/2010)[/b][hr]Can this script be amended so it will read a spreadsheet with a list ofUsername, password database access?[/quote]You can create a stored procedure that accepts username, password, etc..Then in another column of your excel file using concatenate function create the sql statement for each onsomethin Like =CONCATENATE("EXEC usp_CreateLogin @username='",A2,"', @Password = '",B2,"' … ")Then copy the cell and paste it all the way down until a statement per username is created, after that copy the whole column and paste it in the SQL Management StudioThis should create all the users</description><pubDate>Tue, 07 Jun 2011 14:43:22 GMT</pubDate><dc:creator>Aristides Rapozo</dc:creator></item><item><title>RE: Create Login Script</title><link>http://www.sqlservercentral.com/Forums/Topic497615-359-1.aspx</link><description>Can this script be amended so it will read a spreadsheet with a list ofUsername, password database access?</description><pubDate>Mon, 29 Nov 2010 06:11:38 GMT</pubDate><dc:creator>david.griffiths 57552</dc:creator></item><item><title>RE: Create Login Script</title><link>http://www.sqlservercentral.com/Forums/Topic497615-359-1.aspx</link><description>Yes, you need to use dynamic SQL to do this. Which means that whoever causes the stored procedure to execute needs the appropriate permissions to create logins, modify database users, grant permissions, etc. So unless this is just a convenience for people who are in the SysAdmin role, you will want to look at the solution referenced in the first reply o your post (which does use dynamic SQL for CREATE LOGIN).Repeating the link:http://blogs.msdn.com/lcris/archive/2005/06/15/sql-server-2005-procedure-signing-demo.aspx</description><pubDate>Thu, 17 Dec 2009 12:05:02 GMT</pubDate><dc:creator>DLathrop</dc:creator></item><item><title>RE: Create Login Script</title><link>http://www.sqlservercentral.com/Forums/Topic497615-359-1.aspx</link><description>K Brian Kelly, you are a star! I am defeinitely going to try this and I am sure it will work. This is the best answer I got do far! :P:D:):w00t:</description><pubDate>Wed, 14 May 2008 00:36:17 GMT</pubDate><dc:creator>Manie Verster</dc:creator></item><item><title>RE: Create Login Script</title><link>http://www.sqlservercentral.com/Forums/Topic497615-359-1.aspx</link><description>The problem is that CREATE LOGIN and ALTER LOGIN won't take the variable. The way around this is to use Dynamic SQL. For instance:[code]CREATE PROCEDURE dbo.Create_Login	@username sysnameASBEGIN	SET NOCOUNT ON	DECLARE @SQL NVARCHAR(4000);		SET @SQL = 'CREATE LOGIN ' + @username + ' WITH PASSWORD = ''12345'', DEFAULT_DATABASE=[dbname], DEFAULT_LANGUAGE=[British], CHECK_EXPIRATION=OFF, CHECK_POLICY=ON';	EXECUTE(@SQL);		EXEC sys.sp_addsrvrolemember @loginame = @username, @rolename = N'sysadmin';		SET @SQL = 'ALTER LOGIN ' + @username + ' DISABLE';	EXECUTE(@SQL);END;[/code]</description><pubDate>Sat, 10 May 2008 06:14:32 GMT</pubDate><dc:creator>K. Brian Kelley</dc:creator></item><item><title>RE: Create Login Script</title><link>http://www.sqlservercentral.com/Forums/Topic497615-359-1.aspx</link><description>Should you add OUTPUT ASCREATE PROCEDURE dbo.Create_Login        (                @username varchar(50) [b]OUTPUT[/b]        )AS...?</description><pubDate>Fri, 09 May 2008 14:34:30 GMT</pubDate><dc:creator>SQL ORACLE</dc:creator></item><item><title>RE: Create Login Script</title><link>http://www.sqlservercentral.com/Forums/Topic497615-359-1.aspx</link><description>Hi Manie,take a look at this:[url=http://blogs.msdn.com/lcris/archive/2005/06/15/sql-server-2005-procedure-signing-demo.aspx]http://blogs.msdn.com/lcris/archive/2005/06/15/sql-server-2005-procedure-signing-demo.aspx[/url]</description><pubDate>Fri, 09 May 2008 01:46:08 GMT</pubDate><dc:creator>Torsten Schüßler-406123</dc:creator></item><item><title>Create Login Script</title><link>http://www.sqlservercentral.com/Forums/Topic497615-359-1.aspx</link><description>I have the following stored procedure to create a login on SQL Server 2005. As is it works perfectly but I want to pass a parameter for the login name but keeps getting incorrect syntax errors. The script looks as follows:CREATE PROCEDURE dbo.Create_LoginASBEGIN	SET NOCOUNT ONCREATE LOGIN [johnny] WITH PASSWORD = '12345', DEFAULT_DATABASE=[dbname], DEFAULT_LANGUAGE=[British], CHECK_EXPIRATION=OFF, CHECK_POLICY=ONEXEC sys.sp_addsrvrolemember @loginame = N'johnny', @rolename = N'sysadmin'ALTER LOGIN [@username] DISABLEENDWhen I pass the parameter it looks like this:CREATE PROCEDURE dbo.Create_Login	(		@username varchar(50)	)ASBEGIN	SET NOCOUNT ONCREATE LOGIN @username WITH PASSWORD = '12345', DEFAULT_DATABASE=[dbname], DEFAULT_LANGUAGE=[British], CHECK_EXPIRATION=OFF, CHECK_POLICY=ONEXEC sys.sp_addsrvrolemember @loginame = @username, @rolename = N'sysadmin'ALTER LOGIN @username DISABLEENDPlease can someone help me to get this right. I have searched but nowhere any site says anything about passing a parameterThanksManie Verster</description><pubDate>Fri, 09 May 2008 00:52:30 GMT</pubDate><dc:creator>Manie Verster</dc:creator></item></channel></rss>