項目中有一個需求,要求給其它單位提供數據,我們用到了視圖,並要求不能讓他們看到數據庫中的其它數據,我們為其創建了單獨的賬號,並只能看到指定視圖
一、創建視圖
CREATE VIEW [dbo].[v_Insurance_abroadPerson] AS SELECT TOP (100) PERCENT e.ReceivedletterNumber AS groupId, e.Principle AS ContactName, e.Email
FROM dbo.Ea AS e INNER JOIN dbo.chgrymd AS p ON p.xmbh = e.ReceivedletterNumber
INNER JOIN dbo.unitinfo AS u ON u.dm = e.unit_code
INNER JOIN dbo.ryk AS r ON r.xmbm = p.xmbm INNER JOIN dbo.Purpose AS pp ON e.mission_type = pp.Id
ORDER BY p.groupid
二、創建用戶,並為用戶指定訪問該視圖的權限
1、創建角色 seeviewForInsurance
exec sp_addrole 'seeviewForInsurance'
2、結角色賦查看視圖的權限,視圖名為:v_Insurance_abroadPerson 角色名為創建的角色 seeviewForInsurance
grant select on v_Insurance_abroadPerson to seeviewForInsurance
3、創建用戶,並為用戶指定默認的數據庫,用戶名:abroadInsurance 密碼:12345abc123! 默認的數據庫:cnpc
exec sp_addlogin 'abroadInsurance','12345abc123!','cnpc'
4、將用戶和角色進行綁定 ‘用戶’,‘密碼’,‘角色’
exec sp_adduser 'abroadInsurance','12345abc123!','seeviewForInsurance'
注:這個密碼如果長度和強度不夠的話是執行不了的。
