日期:2014-05-18 浏览次数:20518 次
--> 测试数据:[Permission] if object_id('[Permission]') is not null drop table [Permission] create table [Permission]([PID] varchar(4),[PName] varchar(4)) insert [Permission] select 'P001','读取' union all select 'P002','追加' union all select 'P003','删除' --> 测试数据:[USER] if object_id('[USER]') is not null drop table [USER] create table [USER]([USERID] varchar(4),[USERNAME] varchar(6)) insert [USER] select 'U001','赵用户' union all select 'U002','钱用户' union all select 'U003','孙用户' --> 测试数据:[PerAssign] if object_id('[PerAssign]') is not null drop table [PerAssign] create table [PerAssign]([PID] varchar(4),[USERID] varchar(4)) insert [PerAssign] select 'P001','U001' union all select 'P001','U002' union all select 'P001','U003' union all select 'P002','U002' union all select 'P002','U003' union all select 'P003','U002' SELECT C.USERID AS 用户ID,C.USERNAME AS 用户名, C.PID AS 权限ID,C.PName AS 权限名, CASE WHEN D.PName IS NULL THEN '否' ELSE '是'END AS 是否有权限 FROM(SELECT * FROM [USER] CROSS JOIN [Permission])C LEFT JOIN(SELECT A.PID,A.USERID,B.PName FROM [PerAssign] A LEFT JOIN [Permission] B ON A.PID=B.PID)D ON C.USERID=D.USERID AND C.PID=D.PID /* 用户ID 用户名 权限ID 权限名 是否有权限 U001 赵用户 P001 读取 是 U001 赵用户 P002 追加 否 U001 赵用户 P003 删除 否 U002 钱用户 P001 读取 是 U002 钱用户 P002 追加 是 U002 钱用户 P003 删除 是 U003 孙用户 P001 读取 是 U003 孙用户 P002 追加 是 U003 孙用户 P003 删除 否 */