溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶服務(wù)條款》

MSSQL 如何導(dǎo)出db所有用戶權(quán)限創(chuàng)建語(yǔ)句

發(fā)布時(shí)間:2020-08-15 07:46:12 來(lái)源:ITPUB博客 閱讀:239 作者:qextoqoha 欄目:數(shù)據(jù)安全

use db

go

DECLARE 

@Database varchar(255),

@loginName varchar(255),

@roleName varchar(255),

@sql nvarchar(max);

SET @sql=N'';

DECLARE curLogin CURSOR LOCAL for

select db_name() as dbname,dp.name as username,dpr.name as rolename 

from sys.database_principals dp 

join sys.database_role_members drm on drm.member_principal_id=dp.principal_id

join sys.database_principals dpr on drm.role_principal_id=dpr.principal_id 

join sys.server_principals sp on sp.name=dp.name

where 1=1

--and dpr.is_fixed_role=1 

and dp.type<>'R'

and dp.type in('S','U','G') --SQL USER,WINDOWS USER AND windows group

order by username,rolename

OPEN curLogin;

FETCH NEXT FROM curLogin INTO @Database,@loginName,@roleName;

WHILE @@FETCH_STATUS = 0

BEGIN

SET @sql=@sql+N'

use '+@Database+';

if not exists(select * from '+@Database+'.sys.database_principals where name='''+@LoginName+''') 

begin

CREATE USER '+QUOTENAME(@LoginName)+';

end

else 

begin

ALTER USER '+QUOTENAME(@LoginName)+' with login = '+QUOTENAME(@LoginName)+'

end

;

'

--print @sql

--exec sp_executesql @sql

 

select @sql=@sql+N'

use '+@Database+';

exec sp_addrolemember '''+@roleName+''', ''' + @LoginName + ''''

--exec sp_executesql @sql

FETCH NEXT FROM curLogin INTO @Database,@loginName,@roleName;

END

CLOSE curLogin

DEALLOCATE curLogin

;

--select len(@sql)

--print @sql --this will be truncated 

exec sysadmin.dbo.printmax @sql

go

向AI問(wèn)一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI