尝试在azure SQL上添加多个用户

时间:2018-05-25 07:51:15

标签: sql-server azure-sql-database

declare @Name varchar(100),@sql varchar(400),@sql2 varchar(400),@sql3 varchar(400) ,@sql4 varchar(400) , @psw varchar(10)
--declare @psw varchar(10) = 'Traf@12345'
declare @T table (Name varchar(100)) 
insert into @T values 
('SampleUser1'),
('SampleUser1')
While exists(select * from @T) 
begin 
   select top 1 @Name = name from @T 
   IF NOT EXISTS (SELECT [name] FROM [sys].[sql_logins] WHERE [name] = @Name) 
   BEGIN 
      SET @psw = 'Traf@123'
      SET @sql = 'CREATE LOGIN '+'['+@name+']'+' WITH PASSWORD ='+@psw
      EXEC (@sql) 
   END 
   DELETE FROM @T WHERE Name = @Name 
END

输出

  

Msg 102,Level 15,State 1,Line 1

附近的语法不正确      
    

' TRAF @ 123'

  

1 个答案:

答案 0 :(得分:0)

请尝试以下更改:

declare @Name varchar(100),@sql varchar(400),@sql2 varchar(400),@sql3 varchar(400) ,@sql4 varchar(400) 
declare @psw varchar(10) = 'Traf@12345';
declare @T table (Name varchar(100)) ;
insert into @T values 
('SampleUser1'),
('SampleUser2')

While exists(select * from @T) 
begin 
   select top 1 @Name = name from @T 
   IF NOT EXISTS (SELECT [name] FROM [sys].[sql_logins] WHERE [name] = @Name) 
   BEGIN 

      SET @sql = 'CREATE LOGIN '+'['+@name+']'+' WITH PASSWORD ='''+ @psw + '''; '
      EXEC (@sql) 
   END 
   DELETE FROM @T WHERE Name = @Name 
END