?
Solved

Creating new user programmatically

Posted on 2007-11-28
6
Medium Priority
?
1,217 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 750 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
Get free NFR key for Veeam Availability Suite 9.5

Veeam is happy to provide a free NFR license (1 year, 2 sockets) to all certified IT Pros. The license allows for the non-production use of Veeam Availability Suite v9.5 in your home lab, without any feature limitations. It works for both VMware and Hyper-V environments

 
LVL 6

Assisted Solution

by:messen1975
messen1975 earned 750 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

Veeam and MySQL: How to Perform Backup & Recovery

MySQL and the MariaDB variant are among the most used databases in Linux environments, and many critical applications support their data on them. Watch this recorded webinar to find out how Veeam Backup & Replication allows you to get consistent backups of MySQL databases.

Question has a verified solution.

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

Why is this different from all of the other step by step guides?  Because I make a living as a DBA and not as a writer and I lived through this experience. Defining the name: When I talk to people they say different names on this subject stuff l…
One of the most important things in an application is the query performance. This article intends to give you good tips to improve the performance of your queries.
Via a live example, show how to extract insert data into a SQL Server database table using the Import/Export option and Bulk Insert.
Viewers will learn how to use the SELECT statement in SQL and will be exposed to the many uses the SELECT statement has.
Suggested Courses

839 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