merge into 的用法

已创建 1 行。
 
SQL> commit;
 
----不带where子句
SQL>  merge into merge1
  2     using merge2
  3   on (merge1.id = merge2.id)
  4   when matched then
  5     update
  6    set merge1.name = merge2.name;
 
3 行已合并。
 
SQL> rollback;
 
回退已完成。
 
---带where子句
SQL>   merge into merge1 a
  2    using merge2 b
  3   on (a.id = b.id)
  4   when matched then
  5    update
  6    set a.name = b.name
  7   where a.name <> b.name;
 
0 行已合并。
 
------如果要更新b表,这样写会出错
SQL>    merge into merge1 a
  2     using merge2 b
  3    on (a.id = b.id)
  4    when matched then
  5     update
  6     set b.name = a.name;
   set b.name = a.name
       *
第 6 行出现错误:
ORA-00904: "B"."NAME": 标识符无效
----------------------------------------

;MERGE INTO Loanee p
USING ( SELECT * FROM @TApply AS nc) c on p.ApplicationID=c.ApplicationID
WHEN MATCHED THEN UPDATE set p.UpdateTime=getdate()
WHEN NOT MATCHED BY TARGET THEN INSERT
(
LoaneeNumber,LoaneeName,LoanDate,LockMatchRatio,SurplusMatchRatio,
MatchRatio,MatchAmount,LockAmount,LoaneeAmount,ExpectedYield,
LoanPeriod,CreditRating,FreeState,UpdateTime,AddTime,
DeleteState,IsFullState,FullTime,LoaneeType,ApplicationID,
CashAccountNo,ContractTime,RelaseRatio,MatchOrder,KsshDate,
LockState,CashAccountNo_Name,loaneeSource,Broker_CashAccount,Broker_CashAccountName,ApprovalTime)
VALUES(
c.LoaneeNumber,c.[LoaneeName],c.[LoanDate],0,1,
0,0,0,c.[LoaneeAmount],c.[ExpectedYield],
c.[LoanPeriod],c.[CreditRating],0,getdate(),getdate(),
0,0,null,0,c.[ApplicationID],
c.[CashAccountNo],c.[ContractTime],0,c.[MatchOrder],[KsshDate],
0,c.[CashAccountNo_Name],0,c.[Broker_CashAccount],c.[Broker_CashAccountName],c.ApprovalTime
);

以上例子的作用就是,向一个表中插入数据时,判断是否存在,不存在则插入,存在则更新
 
 
 
posted @ 2015-09-11 18:20  秋香姑娘请你不要紧张  阅读(3236)  评论(0编辑  收藏  举报