我正在尝试在表中插入多行,我基本上想要一个嵌套循环。我有以下建议代码:
with Permissions (Id) as
(
select Id from Surge.Permission where Name in (
'Web.Orders:Enable',
'App.Orders:Enable',
'Web.Orders:View',
'App.Orders:View',
'Web.Orders:Modify',
'App.Orders:Modify',
'Web.Orders:View.All',
'App.Orders:View.All'
)
)
insert Surge.[RolePermission] (DataPartitionId, PermissionId, RoleId)
select DP.Id, Permissions.Id, 2 from Surge.DataPartition as DP
这似乎不对(但LINQPad似乎不喜欢它;它似乎无法找到Permissions.Id
)。我如何使用常规SQL执行此操作?基本上,我希望输出为:
1, 1, 2
1, 2, 2,
1, 3, 2
...
2, 1, 2
2, 2, 2
2, 3, 2
等等,就像一个嵌套循环。
答案 0 :(得分:0)
根据您想要的Permission.ID
和DataPartition.ID
的每个组合的样本数据来猜测猜测:
with Permissions as
(select Id
from Surge.Permission
where Name in ('Web.Orders:Enable',
'App.Orders:Enable',
'Web.Orders:View',
'App.Orders:View',
'Web.Orders:Modify',
'App.Orders:Modify',
'Web.Orders:View.All',
'App.Orders:View.All'
)
)
INSERT INTO Surge.[RolePermission] (DataPartitionId, PermissionId, RoleId)
SELECT DP.Id, p.Id, 2
FROM Surge.DataPartition as DP
CROSS JOIN Permissions p
如果不是这种情况,您可以将CROSS JOIN
更改为其他JOIN
类型并添加条件。