Oracleº¯Êýmerge
°ÑÊý¾Ý´ÓÒ»¸ö±í¸´ÖƵ½ÁíÒ»¸ö±í£¬²åÈëÐÂÊý¾Ý»òÌæ»»µôÀÏÊý¾ÝÊÇÿһ¸öORACLE DBA¶¼»á¾³£Åöµ½µÄÎÊÌâ¡£ÔÚORACLE9iÒÔǰµÄÄê´ú£¬ÎÒÃÇÒªÏȲéÕÒÊÇ·ñ´æÔÚÀÏÊý¾Ý£¬Èç¹ûÓÐÓÃUPDATEÌæ»»£¬·ñÔòÓÃINSERTÓï¾ä²åÈ룬Æä¼äÉÙ²»ÁË»¹ÓÐһЩ±ê¼Ç±äÁ¿µÈµÈ£¬·±ËöµÄºÜ¡£ÏÖÔÚORACLE9iרΪÕâÖÖÇé¿öÌṩÁËMERGEÓï¾ä£¬Ê¹ÕâÒ»¹¤×÷±äµÃÒì³£ÇáËÉ£¬
MERGEÓï¾äÔÚ9iµÄÓï·¨ÈçÏ£º
MERGE [hint] INTO [schema .] table [t_alias] USING [schema .]
{ table | view | subquery } [t_alias] ON ( condition )
WHEN MATCHED THEN merge_update_clause
WHEN NOT MATCHED THEN merge_insert_clause;
Àý£º
MERGE INTO tdest d
USING tsrc s ON (s.srckey = d.destkey)
WHEN MATCHED THEN
UPDATE SET d.destdata = d.destdata + s.srcdata
WHEN NOT MATCHED THEN
INSERT (destkey,destdata) VALUES (srckey,srcdata)
Ò»ÌõÓï¾ä´úÌæÁËÔÀ´µÄÒ»¶Î¸´ÔÓÓï¾ä£¬µ«ÊÇʹÓÃMERGEÒ²ÊÇÓÐҪעÒâµÄµØ·½µÄ£¬¿´ÏÂÃæµÄÀý×Ó£º
CREATE TABLE tdest(destkey KEYTYE,destdata DATATYPE);
CREATE TABLE tsrc(srckey KEYTYE,srcdata DATATYPE);
--insert some rows into tsrc here.
MERGE INTO tdest d
USING tsrc s ON (d.destkey = s.srckey)
WHEN MATCHED THEN
UPDATE SET d.destkey = s.srckey
WHEN NOT MATCHED THEN
INSERT (d.destkey,d.destdata) VALUES (s.srckey,s.srcdata);
ÔÚOracle 10gÖÐMERGEÓÐÈçÏÂһЩ¸Ä½ø£º
1¡¢UPDATE»òINSERT×Ó¾äÊÇ¿ÉÑ¡µÄ
2¡¢UPDATEºÍINSERT×Ó¾ä¿ÉÒÔ¼ÓWHERE×Ó¾ä
3¡¢ÔÚONÌõ¼þÖÐʹÓó£Á¿¹ýÂËν´ÊÀ´insertËùÓеÄÐе½Ä¿±ê±íÖÐ,²»ÐèÒªÁ¬½ÓÔ´±íºÍÄ¿±ê±í
4¡¢UPDATE×Ó¾äºóÃæ¿ÉÒÔ¸úDELETE×Ó¾äÀ´È¥³ýһЩ²»ÐèÒªµÄÐÐ
Ê×ÏÈ´´½¨Ê¾Àý±í£º
create table PRODUCTS
(
PRODUCT_ID INTEGER,
PRODUCT_NAME VARCHAR2(60),
CATEGORY VARCHAR2(60)
);
insert into PRODUCTS values (1501, 'VIVITAR 35MM', 'ELECTRNCS');
insert into PRODUCTS values (1502, 'OLYMPUS IS50', 'ELECTRNCS');
i
Ïà¹ØÎĵµ£º
¶ÔÓÚORACLEÊý¾Ý¿âµÄÊý¾Ý´æÈ¡£¬Ö÷ÒªÓÐËĸö²»Í¬µÄµ÷Õû¼¶±ð£¬µÚÒ»¼¶µ÷ÕûÊDzÙ×÷ϵͳ¼¶°üÀ¨Ó²¼þƽ̨,µÚ¶þ¼¶µ÷ÕûÊÇORACLE RDBMS¼¶µÄµ÷Õû,µÚÈý¼¶ÊÇÊý¾Ý¿âÉè¼Æ¼¶µÄµ÷Õû,×îºóÒ»¸öµ÷Õû¼¶ÊÇSQL¼¶¡£Í¨³£ÒÀ´ËËļ¶µ÷Õû¼¶±ð¶ÔÊý¾Ý¿â½øÐе÷Õû¡¢ÓÅ»¯£¬Êý¾Ý¿âµÄÕûÌåÐÔÄÜ»áµÃµ½ºÜ´óµÄ¸ÄÉÆ¡£ÏÂÃæ´Ó¼¸¸ö²»Í¬·½Ã ......
¡¡¡¡´ÓÓαêÌáÈ¡Êý¾Ý
¡¡¡¡´ÓÓαêµÃµ½Ò»ÐÐÊý¾ÝʹÓÃFETCHÃüÁÿһ´ÎÌáÈ¡Êý¾Ýºó£¬Óα궼ָÏò½á¹û¼¯µÄÏÂÒ»ÐС£Óï·¨ÈçÏ£º
FETCH cursor_name INTO variable[,variable,...]
¡¡¡¡¶ÔÓÚSELECT¶¨ÒåµÄÓαêµÄÿһÁУ¬FETCH±äÁ¿ÁÐ±í¶¼Ó¦¸ÃÓÐÒ»¸ö±äÁ¿ÓëÖ®Ïà¶ÔÓ¦£¬±äÁ¿µÄÀàÐÍÒ²ÒªÏàͬ¡£
¡¡¡¡Àý£º
SET SERVERIUTPUT ON
DECLARE
......
¶ÔÓÚOracle£¬±ÊÕßÒ²ÊÇÒ»¸ö³õѧÕߣ¬ÈçÎÄÕÂÖеIJ»×ãÖ®´¦»¹Çë¸÷Î»ÍøÓÑÖ¸Õý
ÏÂÃæÎÒÃÇ¿ªÊ¼°²×°Oracle£¬ÔÚ°²×°Ö®Ç°£¬ÎÒÃÇÏÈÀ´ÁгöһЩÔÚ°²×°OracleÊý¾Ý¿âʱÐèҪעÒâµÄÊÂÏOracle°²×°³ÌÐò£¨OUI£©²»Ö§³Ö×Ö·û½çÃæ°²×°£¬Òò´Ë°²×°·þÎñÆ÷±ØÐëÓÐX windowsÖ§³Ö ÏÂÃæÊÇÎÒ°²×°OracleÊý¾Ý¿âµÄϵͳÐÅÏ¢
²Ù×÷ϵ ......
OracleÊý¾Ý¿âϵͳ¸ù¾Ý³õʼ»¯²ÎÊýÎļþinit.oraÖÐÉèÖõIJÎÊýÀ´ÅäÖÃ×ÔÉíµÄÆô¶¯£¬Ã¿¸öʵÀýÔÚÆô¶¯Ö®Ç°£¬Ê×ÏȶÁÈ¡ÕâЩ²ÎÊýÎļþÖÐÉèÖõIJ»Í¬²ÎÊý¡£
OracleϵͳÖеIJÎÊý£¬¸ù¾ÝϵͳʹÓÃÇé¿ö¿ÉÒÔ¼òµ¥·ÖΪÁ½´óÀࣺ
ÆÕͨ²ÎÊý£ºÒ²¾ÍÊÇOracleϵͳÕý³£Ê¹ÓõÄһЩ²ÎÊý£¬
·Ç·²²ÎÊý£º°üÀ¨ÈýÖÖ£¬¹ýʱ²ÎÊý¡¢Ç¿µ÷²ÎÊýºÍÒþ²Ø²ÎÊý¡£
Ëæ×ÅOrac ......
DECODEº¯ÊýÊÇORACLE PL/SQLÊǹ¦ÄÜÇ¿´óµÄº¯ÊýÖ®Ò»£¬Ä¿Ç°»¹Ö»ÓÐORACLE¹«Ë¾µÄSQLÌṩÁ˴˺¯Êý£¬ÆäËûÊý¾Ý¿â³§É̵ÄSQLʵÏÖ»¹Ã»Óд˹¦ÄÜ¡£DECODEÓÐʲôÓÃÍ¾ÄØ£¿ Ïȹ¹ÔìÒ»¸öÀý×Ó£¬¼ÙÉèÎÒÃÇÏë¸øÖÇÐÇÖ°Ô±¼Ó¹¤×Ê£¬Æä±ê×¼ÊÇ£º¹¤×ÊÔÚ8000ÔªÒÔϵĽ«¼Ó20£¥£»¹¤×ÊÔÚ8000ÔªÒÔÉϵļÓ15£¥£¬Í¨³£µÄ×ö·¨ÊÇ£¬ÏÈÑ¡³ö¼Ç¼ÖеŤ×Ê×Ö¶ÎÖµ? select s ......