Solved

Creating new user programmatically

Posted on 2007-11-28
6
1,209 Views
Last Modified: 2008-02-01
Hi there,

I am trying to create a new user from a stored procedure but I am getting an error. code is below for stored procedure:

Create PROCEDURE [dbo].[User_Insert]

@Loginname nvarchar(255),
@Password nvarchar(255)
AS
      SET NOCOUNT ON
      SET XACT_ABORT ON
CREATE LOGIN @Loginname WITH PASSWORD=@Password, DEFAULT_DATABASE=[master], DEFAULT_LANGUAGE=[us_english], CHECK_EXPIRATION=OFF, CHECK_POLICY=OFF

But i am getting following error when I try to execute the stored procedure in sql 2005 interface

Msg 102, Level 15, State 1, Procedure User_Insert
Incorrect syntax near '@Loginname'.

if i use following code, it works:

CREATE LOGIN [newuser] WITH PASSWORD=N'abcde234', DEFAULT_DATABASE=[master], DEFAULT_LANGUAGE=[us_english], CHECK_EXPIRATION=OFF, CHECK_POLICY=OFF

whereas [newuser] is the name of the user being added to the sql.

I'd like to call above stored procedure from a c# code so that it could add a user into sql server logins, that's why I need to pass loginname & password as a parameter to the stored procedure.

Could you also tell me how can I add newly created user(above) to a particular database user?

Thank you.
Regards,
0
Comment
Question by:OrionPMS
6 Comments
 
LVL 13

Expert Comment

by:ispaleny
ID: 20365949
Create PROCEDURE [dbo].[User_Insert]

@Loginname nvarchar(255),
@Password nvarchar(255)
AS
      SET NOCOUNT ON
      SET XACT_ABORT ON

exec master.sys.sp_addlogin
@loginame=@Loginname
,@passwd=@Password
,@defdb='master'
,@deflanguage='us_english'
0
 
LVL 13

Accepted Solution

by:
ispaleny earned 250 total points
ID: 20365976
Or try dynamic SQL:

Create PROCEDURE [dbo].[User_Insert]

@Loginname nvarchar(255),
@Password nvarchar(255)
AS
      SET NOCOUNT ON
      SET XACT_ABORT ON
declare @exec  varchar(500)
set @exec='CREATE LOGIN '+ @Loginname+ ' WITH PASSWORD='''+@Password+''', DEFAULT_DATABASE=[master], DEFAULT_LANGUAGE=[us_english], CHECK_EXPIRATION=OFF, CHECK_POLICY=OFF'
EXEC(@exec)
0
 
LVL 6

Expert Comment

by:messen1975
ID: 20366022


Create PROCEDURE [dbo].[User_Insert]
 
@Loginname nvarchar(255),
@Password nvarchar(255)
AS
      SET NOCOUNT ON
      SET XACT_ABORT ON
DECLARE @SQL nVarChar(max)
Set @SQL = 'CREATE LOGIN ' + @Loginname + ' WITH PASSWORD=@Password, DEFAULT_DATABASE=[master], DEFAULT_LANGUAGE=[us_english], CHECK_EXPIRATION=OFF, CHECK_POLICY=OFF'
END
 
END

Open in new window

0
Comprehensive Backup Solutions for Microsoft

Acronis protects the complete Microsoft technology stack: Windows Server, Windows PC, laptop and Surface data; Microsoft business applications; Microsoft Hyper-V; Azure VMs; Microsoft Windows Server 2016; Microsoft Exchange 2016 and SQL Server 2016.

 
LVL 6

Assisted Solution

by:messen1975
messen1975 earned 250 total points
ID: 20366027
Sorry Forgot:

Create PROCEDURE [dbo].[User_Insert]
 
@Loginname nvarchar(255),
@Password nvarchar(255)
AS
      SET NOCOUNT ON
      SET XACT_ABORT ON
DECLARE @SQL nVarChar(max)
Set @SQL = 'CREATE LOGIN ' + @Loginname + ' WITH PASSWORD=@Password, DEFAULT_DATABASE=[master], DEFAULT_LANGUAGE=[us_english], CHECK_EXPIRATION=OFF, CHECK_POLICY=OFF'
 
Exec @SQL
END
 

Open in new window

0
 
LVL 31

Expert Comment

by:James Murrell
ID: 20366119
check if user already exists
0
 

Author Comment

by:OrionPMS
ID: 20628852
Hello ispaleny,

I am really sorry for this late reply, its becuase I was on holiday.  I have tried your first example:

Create PROCEDURE [dbo].[User_Insert]

@Loginname nvarchar(255),
@Password nvarchar(255)
AS
      SET NOCOUNT ON
      SET XACT_ABORT ON

exec master.sys.sp_addlogin
@loginame=@Loginname
,@passwd=@Password
,@defdb='master'
,@deflanguage='us_english'


This actually creates a login name.  Would it be possible to assign this newly created login a role in a particular database.  Currently I have used sp_adduser with a role of db_owner for a login and I don't see this login in the database I want to add it to.  The following is what I used in a different stored procedure:

--Creates User with a role to a the Above DB (Addaction)
Create PROCEDURE [dbo].[BUser_Create_Role]

@Loginname nvarchar(255)
--@Password nvarchar(255)

AS
      SET NOCOUNT ON
      SET XACT_ABORT ON

--Adding user to Addaction
exec master.sys.sp_adduser
@loginame=@Loginname
--,@name_in_db ='AddactionHifi'
,@grpname = 'db_owner'


I would also appreciate the dynamic SQL version of the code.  Could you please give that for this example along with the stored procedure code for it.
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

Use this article to create a batch file to backup a Microsoft SQL Server database to a Windows folder.  The folder can be on the local hard drive or on a network share.  This batch file will query the SQL server to get the current date & time and wi…
Ever needed a SQL 2008 Database replicated/mirrored/log shipped on another server but you can't take the downtime inflicted by initial snapshot or disconnect while T-logs are restored or mirror applied? You can use SQL Server Initialize from Backup…
Via a live example, show how to backup a database, simulate a failure backup the tail of the database transaction log and perform the restore.
Viewers will learn how the fundamental information of how to create a table.

777 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