?
Solved

Creating new user programmatically

Posted on 2007-11-28
6
Medium Priority
?
1,213 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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
How Blockchain Is Impacting Every Industry

Blockchain expert Alex Tapscott talks to Acronis VP Frank Jablonski about this revolutionary technology and how it's making inroads into other industries and facets of everyday life.

 
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

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
Ever wondered why sometimes your SQL Server is slow or unresponsive with connections spiking up but by the time you go in, all is well? The following article will show you how to install and configure a SQL job that will send you email alerts includ…
Familiarize people with the process of utilizing SQL Server functions from within Microsoft Access. Microsoft Access is a very powerful client/server development tool. One of the SQL Server objects that you can interact with from within Microsoft Ac…
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.
Suggested Courses

741 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