我有一个表tt_auth_itm
(自动化项目),其中包含整数tt_auth_id
(授权配置文件ID)和tt_func_id
(功能ID)以及其他一些字段。
对于每个唯一tt_auth_id
(授权配置文件),必须存在tt_func_id
范围内的记录(所有现有功能,而不是连续范围BTW)。
但有些记录丢失了(图片中的彩色区域;它列出了tt_auth_itm
和tt_func_id
的所有组合,一次两列):
我知道,对于tt_auth_id
= 1,所有必需tt_func_id
的记录都存在。
要为tt_auth_id
= 2插入缺失的记录,我可以执行此操作,复制其中tt_func_id
= 1的相应tt_auth_id
记录中的其他字段值:
insert into tt_auth_item (tt_auth_id, tt_func_id, other, fields)
select 2 as tt_auth_id, tt_func_id, other, fields
from tt_auth_itm o where tt_auth_id=7
and not exists
(select * from tt_auth_itm i where i.tt_auth_id=2 and o.tt_func_id=i.tt_func_id)
现在我想将其扩展为一个插入语句,用于所有其他tt_auth_id
缺少记录的值。
我无法理解这一点。我试图将上述查询更改为:
insert into tt_auth_item (tt_auth_id, tt_func_id, other, fields)
select tt_auth_id, tt_func_id, other, fields
from tt_auth_itm o where tt_auth_id=1
and not exists
(select * from tt_auth_itm i where (i.tt_auth_id=o.tt_auth_id) and (o.tt_func_id=i.tt_func_id))
但这还不够(子选择为空),代码需要另一个间接级别....
什么SQL语句正确扩展了我的' 2'一般情况下的情况?
请使用通用SQL,因为我需要它用于SQL Server,Oracle,Firebird