By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
438,419 Members | 1,648 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 438,419 IT Pros & Developers. It's quick & easy.

Create User cmd problem

P: 4
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:

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 ?
Oct 26 '08 #1
Share this Question
Share on Google+
3 Replies


debasisdas
Expert 5K+
P: 8,127
I am don't think spaces are allowed .

Try enclosing the fullname with double quotes. That might help.
Oct 26 '08 #2

P: 4
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
Oct 26 '08 #3

P: 4
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
Oct 27 '08 #4

Post your reply

Sign in to post your reply or Sign up for a free account.