/* ------------------------ My Meta Content Here SEO ------------------------ */

Pages

Main Menu




Thursday, December 18, 2014

Dynamically create mutiple users login in sql server

Dynamically create multiple users login in sql server by passing dynamic value



DECLARE @site_value INT;
DECLARE @login varchar(20);
DECLARE @password varchar(20);
DECLARE @Sql varchar(max);
SET @site_value = 0;

WHILE @site_value <= 3
BEGIN  
   SET @site_value = @site_value + 1;
   SET @login = 'user' + cast(@site_value as varchar(10));
   SET @password = 'password@' + cast(@site_value as varchar(10));
   SET @Sql = 'CREATE LOGIN '+@login+' WITH PASSWORD = '+''''+@password+''''+'';
   PRINT(@Sql)
   exec (@Sql)
END;

PRINT 'Done WHILE LOOP';
GO

Result

CREATE LOGIN user1 WITH PASSWORD = 'password@1'
CREATE LOGIN user2 WITH PASSWORD = 'password@2'
CREATE LOGIN user3 WITH PASSWORD = 'password@3'
CREATE LOGIN user4 WITH PASSWORD = 'password@4'
Done WHILE LOOP


No comments:

Post a Comment

Recent Posts

My Blog List