新しい行を作成するために値を使用するのと同時に擬似ID値を更新するために、次の操作を実行しようとしましたが、APPLYはUPDATE文を右テーブルソース。単純にID列を使用する以外の最もエレガントな代替手段は何ですか?UPDATE文を行ソースとして使用してAPPLYを使用します。
create table Temp1(
id int not null identity(1,1) primary key
,data nvarchar(max) null)
create table Temp2(
id int not null primary key
,fkTemp1 int not null references Temp1(id)
,data nvarchar(max) null)
create table Numbering(
ObjectCode int not null primary key
,AutoKey int)
insert into Temp1(data) values('test string')
insert into Temp1(data) values('another test string')
insert into Numbering(ObjectCode, AutoKey) values(4, 1)
insert into Temp2(id, fkTemp1, data)
select n.AutoKey, t1.id, t1.data
from Temp1 t1
left join Temp2 t2 on t2.fkTemp1 = t1.id
cross apply (update Numbering set AutoKey = AutoKey + 1 output inserted.AutoKey where ObjectCode = 4) n
where t2.id is null -- only insert where a target row does not already exist
もあなたのクエリは私には意味を作っていない、APPLYを占めて...「T2.IDはNULL IS」問題を解決する必要があります。あなたは "どこにt2.idがnullであるか"をあなたのinsert..selectに持っていますが、Temp2のidカラムは絶対にnullにすることはできません。そのため、決して結果を挿入するべきではありません。 – RBarryYoung