Solved

Creating new user programmatically

Posted on 2007-11-28
6
1,205 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
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 
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

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

When you hear the word proxy, you may become apprehensive. This article will help you to understand Proxy and when it is useful. Let's talk Proxy for SQL Server. (Not in terms of Internet access.) Typically, you'll run into this type of problem w…
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…
Via a live example, show how to extract information from SQL Server on Database, Connection and Server properties
Viewers will learn how to use the UPDATE and DELETE statements to change or remove existing data from their tables. Make a table: Update a specific column given a specific row using the UPDATE statement: Remove a set of values using the DELETE s…

757 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

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now