Welcome to dbForumz.com!
FAQFAQ    SearchSearch      ProfileProfile    Private MessagesPrivate Messages   Log inLog in

problem with stored proc

 
   Database Forums (Home) -> MSDE RSS
Next:  Informix Roundup: 03 December 2008  
Author Message
Sagaert Johan

External


Since: Mar 24, 2006
Posts: 7



(Msg. 1) Posted: Wed Dec 03, 2008 7:26 pm
Post subject: problem with stored proc
Archived from groups: microsoft>public>sqlserver>msde (more info?)

hi

i have a sp with this in:

CREATE USER [@newuser] FOR LOGIN [@newuser]

why does it create a user @newuser instead of the contents of this var ?

Johan

 >> Stay informed about: problem with stored proc 
Back to top
Login to vote
Russell Fields

External


Since: Feb 21, 2007
Posts: 457



(Msg. 2) Posted: Thu Dec 04, 2008 9:06 am
Post subject: Re: problem with stored proc [Login to view extended thread Info.]
Archived from groups: per prev. post (more info?)

Johan,

You probably based this on seeing such examples as:

CREATE USER [Domain\Login] FOR LOGIN [Domain\Login]

Because the [ ] is an escape that tells the parser not to examine the
contents of the brackets more closely. It allows the use of unsupported
characters in names. But it means that the code does not even see the
variable name. If you remove [ ], then this should work.

SET @newuser = 'Domain\Login'

CREATE USER @newuser FOR LOGIN @newuser

A variable already safely contains the value with special characters, so
does not need to be bracketed.

RLF

"Sagaert Johan" wrote in message

> hi
>
> i have a sp with this in:
>
> CREATE USER [@newuser] FOR LOGIN [@newuser]
>
> why does it create a user @newuser instead of the contents of this var ?
>
> Johan
>
>
>
>
>
>
>
>
>
>

 >> Stay informed about: problem with stored proc 
Back to top
Login to vote
Sagaert Johan

External


Since: Mar 24, 2006
Posts: 7



(Msg. 3) Posted: Thu Dec 04, 2008 6:25 pm
Post subject: Re: problem with stored proc [Login to view extended thread Info.]
Archived from groups: per prev. post (more info?)

Hi
If i remove the square brackets i get a syntax error

My sp is declared as :

ALTER PROCEDURE dbo.AddAdminUser

(

@newuser nvarchar(256),

@newpw varchar

)

AS

CREATE USER @newuser FOR LOGIN @newuser;

RETURN





"Russell Fields" wrote in message

> Johan,
>
> You probably based this on seeing such examples as:
>
> CREATE USER [Domain\Login] FOR LOGIN [Domain\Login]
>
> Because the [ ] is an escape that tells the parser not to examine the
> contents of the brackets more closely. It allows the use of unsupported
> characters in names. But it means that the code does not even see the
> variable name. If you remove [ ], then this should work.
>
> SET @newuser = 'Domain\Login'
>
> CREATE USER @newuser FOR LOGIN @newuser
>
> A variable already safely contains the value with special characters, so
> does not need to be bracketed.
>
> RLF
>
> "Sagaert Johan" wrote in message
>
>> hi
>>
>> i have a sp with this in:
>>
>> CREATE USER [@newuser] FOR LOGIN [@newuser]
>>
>> why does it create a user @newuser instead of the contents of this var ?
>>
>> Johan
>>
>>
>>
>>
>>
>>
>>
>>
>>
>>
>
 >> Stay informed about: problem with stored proc 
Back to top
Login to vote
Lawrence Garvin

External


Since: Dec 15, 2005
Posts: 14



(Msg. 4) Posted: Fri Dec 05, 2008 10:24 am
Post subject: Re: problem with stored proc [Login to view extended thread Info.]
Archived from groups: per prev. post (more info?)

"Sagaert Johan" wrote in message


> Hi
> If i remove the square brackets i get a syntax error
>
> My sp is declared as :
>
> ALTER PROCEDURE dbo.AddAdminUser
> (
> @newuser nvarchar(256),
> @newpw varchar
> )
> AS
> CREATE USER @newuser FOR LOGIN @newuser;
> RETURN

The syntax error is caused because CREATE USER cannot take a variable as an
argument.

You can work around this by executing the proc using dynamic SQL:

CREATE PROCEDURE dbo.AddAdminUser (
@newuser nvarchar(256),
@newpw varchar
)
AS

DECLARE @sql varchar(max)
SET @sql = ('CREATE USER ' + @newuser + ' FOR LOGIN ' + @newuser)
EXEC @sql
RETURN

--
Lawrence Garvin, M.S., MCITP(x2), MCTS(x5), MCP(x7), MCBMSP
Principal/CTO, Onsite Technology Solutions, Houston, Texas
 >> Stay informed about: problem with stored proc 
Back to top
Login to vote
Display posts from previous:   
   Database Forums (Home) -> MSDE All times are: Pacific Time (US & Canada)
Page 1 of 1

 
You can post new topics in this forum
You can reply to topics in this forum
You can edit your posts in this forum
You can delete your posts in this forum
You can vote in polls in this forum



[ Contact us | Terms of Service/Privacy Policy ]