Create users dynamic names and assgin roles

前端 未结 2 1891
情话喂你
情话喂你 2020-12-11 19:56

I need to create user with dynamic names with a variable name

example :

The following code gives a syntax error.

Create Log         


        
2条回答
  •  一整个雨季
    2020-12-11 20:17

    You can not use create login with variables. You have to create the statement dynamically or you can use sp_addlogin. According to http://msdn.microsoft.com/en-us/library/ms173768.aspx sp_addlogin is deprecated.

    declare @UN sysname
    declare @PW sysname
    declare @S nvarchar(100)
    
    set @UN = 'UserName'
    set @PW = 'Password'
    
    set @S = 'CREATE LOGIN ' + quotename(@UN) + ' WITH PASSWORD = ' + quotename(@PW, '''')
    
    exec (@S)
    

提交回复
热议问题