oracle merge的确是个很好的命令,用一条命令就可以解决不少的逻辑关系,但是如果碰到需要更新目标表的连接列的时候,问题就出现了
Merge Into Srv_Instance Si
Using (Select Array_Circuit(i).Circuit_Id Circuit_Id From Dual) o
On (Si.Route_Id = o.Circuit_Id)
When Matched Then
Update Set Si.Route_Id = v_Pw_Id Where Si.Delete_State = '0'
When Not Matched Then
Insert(
Inst_Id,Dis_Seq,Prod_Inst_Id,Srv_Id,Route_Id,
Route_Type,Order_Seq,Opr_State_Id,Delete_State,
Order_Layer,Act_Type,Old_Sp,Modify_Op,Modiry_Date)
Values(
Lpad(Seq_Srv_Instance_Inst_Id.Nextval, 24, '0'),
To_Char(Systimestamp, 'missff') ||Seq_Srv_Instance_Inst_Id.Nextval,
Trim('RefreshId' || To_Char(Systimestamp, 'YYMMDDhh24missff') ||
Lpad(Dbms_Random.Value(0, 1000), 5, '0')),
'1010',
v_Pw_Id,
'1048',
0,
170003,
0,
0,
'101',
Gv_Old_Sp,
'1',
Sysdate);
如果执行这个代码的话就要报ora-38104错误了,我是这样解决的问题的,具体看代码
Update Srv_Instance Si
Set Si.Route_Id = v_Pw_Id
Where Si.Route_Id = Array_Circuit(i).Circuit_Id;
If (Sql%Rowcount = 0) Then
Insert Into Srv_Instance
(Inst_Id,
Dis_Seq,
Prod_Inst_Id,
Srv_Id,
Route_Id,
Route_Type,
Order_Seq,
Opr_State_Id,
Delete_State,
Order_Layer,
Act_Type,
Old_Sp,
Modify_Op,
Modiry_Date)
Values
(Lpad(Seq_Srv_Instance_Inst_Id.Nextval, 24, '0'),
To_Char(Systimestamp, 'missff') || Seq_Srv_Instance_Inst_Id.Nextval,
Trim('RefreshId' || To_Char(Systimestamp, 'YYMMDDhh24missff') ||
Lpad(Dbms_Random.Value(0, 1000), 5, '0')),
'1010',
v_Pw_Id,
'1048',
0,
170003,
0,
0,
'101',
Gv_Old_Sp,
'1',
Sysdate);
End If;