Connecting Tech Pros Worldwide Forums | Help | Site Map

Create User cmd problem

Newbie
 
Join Date: Oct 2008
Posts: 4
#1: Oct 26 '08
Hi,
I'm writing c# app and I want to add a new login and user to a SS 2005 DB using Stored Procedure.
I've come with the folowing code:

Quote:
DECLARE @fullName nvarchar(50)
SET @fullName = @lastName+' '+@firstName

SET @cmd = 'CREATE USER '+ @fullName +' FOR LOGIN ' + @shortName;
EXEC sp_executesql @cmd
can someone tell me how can I create a new user with a space between first and a last name ?

debasisdas's Avatar
Moderator
 
Join Date: Dec 2006
Location: Bangalore ,India
Posts: 7,511
#2: Oct 26 '08

re: Create User cmd problem


I am don't think spaces are allowed .

Try enclosing the fullname with double quotes. That might help.
Newbie
 
Join Date: Oct 2008
Posts: 4
#3: Oct 26 '08

re: Create User cmd problem


I've already tried with the " but it did not work either :(

If it's no problem to create that kind of user, using wizard from the context menu on the DB>Security>Users>New user ... - there should be some way of doing the same thing from the code level
Newbie
 
Join Date: Oct 2008
Posts: 4
#4: Oct 27 '08

re: Create User cmd problem


Ok I've did some research and came up with an answer to my problem:


Expand|Select|Wrap|Line Numbers
  1. CREATE PROCEDURE test
  2. AS
  3. DECLARE @s nvarchar(20)
  4. SET @s = 'jeden dwatrzy'
  5. EXEC sp_adduser test2 ,@s


or with the usage of the transaction
Expand|Select|Wrap|Line Numbers
  1. CREATE PROCEDURE test
  2. AS
  3. BEGIN transaction
  4. DECLARE @s nvarchar(100)
  5. declare @a nvarchar(30)
  6. SET @a = '[jakis nowy]'
  7.  
  8. SET @s = 'CREATE USER'+ @a +' FOR LOGIN [test2]'
  9. EXEC sp_executesql @s
  10. COMMIT transaction
Reply