Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- Declare @MaxID int,
- @CurrID int,
- @SQL nvarchar(255),
- @Debug bit = 0;
- Declare @DBs Table (DBID int identity(1, 1) primary key,
- SQLText nvarchar(255));
- Set NoCount On;
- Insert Into @DBs (SQLText)
- Select N'ALTER AUTHORIZATION ON database::' + quotename(name) + N' To [sa];'
- From sys.databases
- Where owner_sid <> 0x01
- And name Not In (N'master', N'model', N'tempdb', N'distribution');
- Select @MaxID = MAX(DBID),
- @CurrID = 1
- From @DBs;
- While @CurrID <= @MaxID
- Begin
- Select @SQL = SQLText
- From @DBs
- Where DBID = @CurrID;
- If @Debug = 1
- Begin
- Print @SQL;
- End
- Else
- Begin
- Print 'Running command: ' + @SQL;
- Exec sp_executesql @SQL;
- End
- Set @CurrID = @CurrID + 1;
- End
- Set NoCount Off;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement