我想创建一个SQL Server 2012查询,它插入一个常量的权限名称列表,例如: “ViewUsersPermission”,“ModifyUsersPermission”进入表RolePermissions。
此表有两列:“RoleID”,它是Roles表的外键,以及varchar列“PermissionTypeName”。
INSERT INTO [dbo].[RolePermissions] ([PermissionTypeName], [RoleID])
SELECT 'ViewUsersPermission' AS [PermissionTypeName]
UNION ALL
SELECT 'ModifyUsersPermission' AS [PermissionTypeName]
UNION ALL
SELECT 'ViewRolesPermission' AS [PermissionTypeName]
INNER JOIN (SELECT [RoleID] FROM [dbo].[Roles]
WHERE [Name] = 'Administrator')
我正在寻找更好的解决方案。我想指定一个权限类型列表,如:
SELECT FROM 'ViewUsersPermission', 'ModifyUsersPermission', 'ViewRolesPermission'
而不是使用UNION ALL
构造。
答案 0 :(得分:7)
假设你至少参加了2008年
SELECT RoleName
FROM (VALUES('ViewUsersPermission'),
('ModifyUsersPermission'),
('ViewRolesPermission')) V(RoleName)
答案 1 :(得分:0)
基于Martin的帮助,完整的SQL INSERT语句如下所示:
INSERT INTO [dbo].[RolePermissions] ([PermissionTypeName], [RoleID])
SELECT [PermissionTypeName], [RoleID] FROM (VALUES('ViewUsersPermission'),
('ModifyUsersPermission'), ('ViewRolesPermission')) V([PermissionTypeName])
INNER JOIN [dbo].[Roles] AS [Role]
ON [Role].[Name] = 'Administrator'