游标使用系列一

查看所有数据库的账号和对应的角色:

 

--drop table #UserDB

--drop table #DB_Role

 

Create table #DB_Role

(

DBName varchar(100) null,

UserName varchar(100),

RoleName Varchar(1000),

LoginName Varchar(100),

DefDBName varchar(100),

DefSchemaName varchar(100),

UserID varchar(100),

SID varchar(1000))

Create table #UserDB

(name varchar(1000));

insert into #UserDB

                      select name from sys.databases where name not in           

                      (N'master', N'model', N'msdb', N'tempdb', N'distribution', N'DWDiagnostics', N'DWConfiguration', N'DWQueue', N'resource',N'ReportServer',N'ReportServerTempDB')

Declare @UserDB nvarchar(1000);

Declare DBrole_cursor cursor

                      for

select * from #UserDB;

open DBrole_cursor;

fetch next from DBrole_cursor into @UserDB;

while @@FETCH_STATUS=0

begin

Declare @DBrole_SQL varchar(1000)

set @DBrole_SQL=

'use '+QUOTENAME(@UserDB,'[]')+';

insert into  #DB_Role

(

UserName,

RoleName,

LoginName,

DefDBName,

DefSchemaName,

UserID,

SID) exec sp_helpuser;

update #DB_Role set DBName='+''''+@UserDB+''''+' where DBName is null

'

print (@DBrole_SQL)

exec (@DBrole_SQL)

fetch next from DBrole_cursor into @UserDB;

end

close DBrole_cursor;

deallocate DBrole_cursor;

 

select * from #DB_Role

 

批量对数据库进行TDE加密:

use master;
go
if not exists
   (select * from sys.databases where name=N'master' and is_master_key_encrypted_by_server=1)
create master key
   encryption by password='Password1';
go
if not exists
   (select * from sys.certificates where name='TDE_cert')
create certificate TDE_cert
   with subject='TDE certificate';
go
use master;
go
backup certificate TDE_cert to file='C:\TDE_Files\TDE_cert'
   with private key
   (file='C:\TDE_Files\TDE_private_key',
   encryption by password='Password2'
   )
Create table #UserDB
(name varchar(1000));
insert into #UserDB
   select name from sys.databases where name not in
                (N'master', N'model', N'msdb', N'tempdb', N'distribution', N'DWDiagnostics', N'DWConfiguration', N'DWQueue', N'resource',N'ReportServer',N'ReportServerTempDB')
Declare @UserDB nvarchar(1000);
Declare TDE_Cursor cursor
   for
select * from #UserDB;
open TDE_cursor;
fetch next from TDE_Cursor into @UserDB;
while @@FETCH_STATUS=0
begin
Declare @TDE_SQL varchar(1000)
set @TDE_SQL=
'use '+QUOTENAME(@UserDB,'[]')+';
 create database encryption key
   with algorithm=AES_256
   encryption by server certificate TDE_cert;
 Alter database '+QUOTENAME(@UserDB,'[]')+' set encryption on;
'
print @TDE_SQL;
exec (@TDE_SQL)
fetch next from TDE_Cursor into @UserDB;
end
close TDE_Cursor;
deallocate TDE_Cursor;
 

游标对所有列进行有效信息筛选(每个字段有相同的关键字时):

Use [ITSP_Audit]

--drop table #Failed_List

--drop table #Column

Declare @Columns varchar(100)

create table #Failed_List

(Server_Name varchar(100),

Failed_Item Varchar(100)

)

 

Declare ColumnName cursor for

select name from sys.columns where OBJECT_ID=object_id('AuditItem')

open ColumnName;

Fetch next from ColumnName into @Columns;

while @@FETCH_STATUS=0

Begin

create table #Column

(Column_name varchar(100))

insert into #Column select @Columns

Declare @SQL varchar(1000)

set @SQL='Insert into #Failed_List

select a.ServerName, b.Column_name from AuditItem a

,#Column b

where '+@Columns+' like '+'''%Miss%'''

Print (@SQL)

Exec (@SQL)

drop table #Column

fetch next from ColumnName into @Columns;

end

close ColumnName

deallocate ColumnName

 

select * from #Failed_List order by Server_Name,Failed_Item

select * from [dbo].[AuditItem]

posted @ 2019-07-12 17:52  MarkL9527  阅读(198)  评论(0编辑  收藏  举报