Solved

Creating new user programmatically

Posted on 2007-11-28
6
1,212 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 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
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.

 
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

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone 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

In part one, we reviewed the prerequisites required for installing SQL Server vNext. In this part we will explore how to install Microsoft's SQL Server on Ubuntu 16.04.
What if you have to shut down the entire Citrix infrastructure for hardware maintenance, software upgrades or "the unknown"? I developed this plan for "the unknown" and hope that it helps you as well. This article explains how to properly shut down …
Via a live example combined with referencing Books Online, show some of the information that can be extracted from the Catalog Views in SQL Server.
Via a live example, show how to shrink a transaction log file down to a reasonable size.

717 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