在sql中添加一个用户,只能访问指定数据库中的指定的表

--创建角色
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
posted @ 2007-05-17 21:39  jun.ma  阅读(683)  评论(0编辑  收藏  举报