Oracle 에 수정 사항 이 존재 하고 기록 SQL 삽입 이 존재 하지 않 습 니 다.

11675 단어 Oacle
      ,           MS SQL。Oracle             ,          ,            Oracle,           Oracle。     Oracle  ,      Oracle    MS SQL   ,               。     ,Oralce MSSQL   ,  ,    CRUD     ,          Oracle   ,            。

                ,   Account ,        AccountID, AccountName,  AccountID   ,         ,        ,           ,      。

   : MS SQL 

        Account ,    ,    nvarchar(50)      。      :



if object_id(N'Account',N'U') is not null
drop table Account
create table Account
(
    AccountID nvarchar(50) primary key  not null,
    AccountName nvarchar(50)
)



                      

if not exists (select * from Account where AccountID = '1') 
    insert into Account(AccountID,AccountName) values('1','Sam Xiao')
else
    update Account set AccountName = '  ' where AccountID = '1'

      ,   SQL           ,    Oracle ,        ,        。      Oracle             。

   : Oracle 

                ,         ,   MS SQL OBJECT_ID('   ','    ')    ,       Oracle           ?   ,Oracle                 ,              ,MS SQL      Select Name From SysObjects Where XType='U'       ,    Oracle    select * from user_tables,       ,              ,       ,     。



declare num number;   
begin
    select count(1) into num from user_tables where table_name='ACCOUNT'; 
    if num > 0 then   
      dbms_output.put_line('  !');
      execute immediate 'drop table ACCOUNT '; 
    end if;   
      execute immediate 'create table Account
                        (
                                AccountID nvarchar2(50) primary key,
                                AccountName nvarchar2(50) 
                        )'; 
      dbms_output.put_line('     !');
end; 



 MS SQL       ,              ?        。
         ,            , Oracle ,      ,             ,                ,      ,      ,     Oracle  exists()                  , MS SQL      。             ,                 。           Oracle           ,         ,      :

1:      SQL%NOTFOUND   SQL%FOUND

SQL%NOTFOUND  SQL        ,            ,             ,    false,                 :



begin
update account set AccountName = '  -a' where AccountID = '5';
IF SQL%NOTFOUND THEN
   insert into account(AccountID,AccountName) values('5','  -b');
END IF;
end;



     ID           ,           ,   ,  SQL%NOTFOUND  false。          ,SQL%NOTFOUND  true,        。

2:    DUP_VAL_ON_INDEX

 Oracle     ,     exception    



begin
insert into account(AccountID,AccountName) values('6','  -b');
exception 
when DUP_VAL_ON_INDEX then begin 
update account set AccountName = '  -b' where AccountID = '6';
end;
end;



          ,         ,              ,      ,          。

3:      dual

dual      ,    select     ,oracle  dual          。



declare t_count number;
begin
select count(*) into t_count from dual where exists(select 1 from account where AccountID='11');
if t_count< 1 then
  dbms_output.put_line('  ');
  insert into account(AccountID,AccountName) values('11','  -11');
else
  dbms_output.put_line('  ');
  update account set AccountName = '  -11' where AccountID = '11';
  end if;
end;



       t_count, dual     t_count,       1,       ,      ,  ,         。

4:no_data_found 

             ,     ,      。       :



declare t_cols number;
begin
select AccountName into t_cols from account where AccountID = '8';
exception 
when no_data_found then begin 
   --dbms_output.put_line('  ');
   insert into account(AccountID,AccountName) values('8','  -8');
end;
when others then 
  begin
    --dbms_output.put_line('  ');
    update account set AccountName = '  -8' where AccountID = '8';
end;
end;



5:merge 

     merge   ,



MERGE INTO table_name alias1   
USING (table|view|sub_query) alias2  
ON (join condition)   
WHEN MATCHED THEN   
    UPDATE table_name SET col1 = col_val1
WHEN NOT MATCHED THEN   
    INSERT (column_list) VALUES (column_values);



  merge    ,                         。 



merge into Account t1  
using (select '3' AccountID,'   ' AccountName from dual) t2 
on (t1.AccountID = t2.AccountID)  
when matched then  
     update set t1.AccountName = t2.AccountName
when not matched then  
     insert values (t2.AccountID, t2.AccountName);  
commit; 



                  。    ,       Oracle       。    MS SQL  ,            。  ,        。

               ,           ,         
     Merge into     : merge into HU_REST.HTS_SPONSOR t1 using (select :ID as ID, :NAME as NAME, :NSPELL as NSPELL , :TEL as TEL, :ADDRESS as ADDRESS, :CARDNO as CARDNO, :XS as XS, :STOPPED as STOPPED from dual) t2 on (t1.ID = t2.ID)--        when not matched then insert --             (t1.ID, t1.NAME, t1.NSPELL, t1.TEL, t1.ADDRESS, t1.CARDNO, t1.XS, t1.STOPPED) values (t2.ID, t2.NAME, t2.NSPELL, t2.TEL, t2.ADDRESS, t2.CARDNO, t2.XS, t2.STOPPED) ---------------- merge into tstaff d using (select '   ' STAFFNAME,'A110108196703168950' staffid,'110108196703168950' idnum,'        ' companyname,'010-62528010-1888' tel,'13901209558' mobile,'[email protected]' email,'100080' zip,'           21 DSP        ' address from dual) s on (d.staffid = s.staffid) when not matched then insert (d.STAFFNAME,d.STAFFID,d.IDNUM,d.COMPANYNAME,d.TEL,d.MOBILE,d.EMAIL,d.ZIP,d.ADDRESS) values(s.STAFFNAME,s.STAFFID,s.IDNUM,s.COMPANYNAME,s.TEL,s.MOBILE,s.EMAIL,s.ZIP,s.ADDRESS);

http://www.cnblogs.com/xcj26/p/3452628.html http://blog.csdn.net/jumtre/article/details/39644117

좋은 웹페이지 즐겨찾기