467,915 Members | 1,282 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 467,915 developers. It's quick & easy.

Create User cmd problem

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
  • viewed: 2285
Share:
3 Replies
debasisdas
Expert 4TB
I am don't think spaces are allowed .

Try enclosing the fullname with double quotes. That might help.
Oct 26 '08 #2
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
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.

Similar topics

reply views Thread by Renato Neves | last post: by
4 posts views Thread by I_AM_DON_AND_YOU? | last post: by
6 posts views Thread by Philip Wagenaar | last post: by
2 posts views Thread by Jeff | last post: by
16 posts views Thread by Mich | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.