--创建角色
EXEC sp_addrole @RoleName
--授予数据库中所有用户表查询权限给某一个角色
declare @UserTablename varchar(20)
Declare Cur Cursor For
select Name from sysobjects where xtype='u' and status>=0
declare @SQL Varchar(200)
Open Cur
Fetch Cur Into @UserTablename
While @@FETCH_STATUS=0
BEGIN
Set @sql='GRANT SELECT ON '+@UserTablename+' TO '+@RoleName+''
Exec(@sql)
Fetch Cur Into @UserTablename
End
Close Cur
Deallocate cur
--添加登录,设置密码,默认数据库
EXEC sp_addlogin @LoginUserName,@LoginUserPwd,@DbName
--为登录在数据库中添加安全账户
EXEC sp_grantdbaccess @LoginUserName,@SafeAccount
--添加安全帐户为角色的成员
EXEC sp_addrolemember @RoleName,@SafeAccount