четвер, 13 січня 2011 р.

Пример курсора


declare @Login nvarchar(255)
declare @Password nvarchar(255)
declare @i int

DECLARE curs CURSOR FOR
SELECT [Login], [Password]
FROM [UsersToCreate]

OPEN curs

FETCH NEXT FROM curs INTO @Login, @Password

set @i=1

WHILE @@FETCH_STATUS = 0
BEGIN
BEGIN TRY
exec('CREATE LOGIN ['+@Login+'] WITH PASSWORD = '''+@Password+'''')
exec('CREATE USER ['+@Login+'] FOR LOGIN ['+@Login+'] WITH DEFAULT_SCHEMA = my_sc')
exec('EXEC sp_addrolemember ''my_role'', ['+@Login+']')
END TRY
BEGIN CATCH
print cast(@i as char(3))+ ') login: ' + @Login + ' - error:' + ERROR_MESSAGE()
set @i= @i+1
END CATCH;
FETCH NEXT FROM curs INTO @Login, @Password
END

CLOSE curs
DEALLOCATE curs

Немає коментарів:

Дописати коментар