Ò׽ؽØͼÈí¼þ¡¢µ¥Îļþ¡¢Ãâ°²×°¡¢´¿ÂÌÉ«¡¢½ö160KB

ORACLE PL/SQL ´¥·¢Æ÷(trigger)ѧϰ±Ê¼Ç

1¡¢´¥·¢Æ÷µÄ¸ÅÄî
´¥·¢Æ÷Ò²ÊÇÒ»ÖÖ´øÃûµÄPL/SQL¿é¡£´¥·¢Æ÷ÀàËÆÓÚ¹ý³ÌºÍº¯Êý£¬ÒòΪËüÃǶ¼ÊÇÓµÓÐÉùÃ÷¡¢Ö´ÐкÍÒì³£´¦Àí¹ý³ÌµÄ´øÃûPL/SQL¿é¡£Óë°üÀàËÆ£¬´¥·¢Æ÷±ØÐë´æ´¢ÔÚÊý¾Ý¿âÖв¢ÇÒ²»Äܱ»¿é½øÐб¾µØ»¯ÉùÃ÷¡£
¶ÔÓÚ´¥·¢Æ÷¶øÑÔ£¬µ±´¥·¢Ê¼þ·¢ÉúµÄʱºò¾Í»áÏÔʽµØÖ´Ðиô¥·¢Æ÷£¬²¢ÇÒ´¥·¢Æ÷²»½ÓÊܲÎÊý¡£
 
´´½¨´¥·¢Æ÷µÄÓï·¨ÈçÏÂ
 
 
CREATE [OR REPLACE] TRIGGER trigger_name
{BEFORE | AFTER | INSTEAD OF} triggering_event
[referencing_clause]
[WHEN trigger_condition]
[FOR EACH ROW]
Trigger_body;
  
ÆäÖÐreferencing_clause×Ó¾äµÄÓÃ;ÊÇͨ¹ýÒ»¸ö²»Í¬µÄÃû³Æ£¬ÒýÓõ±Ç°ÕýÔÚ±»¸üеļǼÐÐÖеÄÊý¾Ý¡£WHEN×Ó¾äÖеÄtrigger_condition—Èç¹û³öÏÖ—¾ÍÓ¦¸ÃÊ×ÏÈÖ´ÐÐÅжϣ¬Ö»Óе±Õâ¸öÌõ¼þֵΪÕæµÄʱºò£¬²Å»áÖ´Ðд¥·¢Æ÷µÄÖ÷Ìå´úÂë¡£
 
 
2¡¢DML´¥·¢Æ÷µÄ¼¤»î˳Ðò
1£©Ö´ÐÐbeforeÓï¾ä¼¶´¥·¢Æ÷—Èç¹û´æÔÚÕâÖÖ´¥·¢Æ÷
2£©¶ÔÊܸÃÓï¾äÓ°ÏìµÄÿһÐмǼ
Ö´ÐÐbeforeÐм¶´¥·¢Æ÷—Èç¹û´æÔÚÕâÖÖ´¥·¢Æ÷
Ö´ÐиÃÓï¾ä±¾Éí
Ö´ÐÐafterÐм¶´¥·¢Æ÷--Èç¹û´æÔÚÕâÖÖ´¥·¢Æ÷
3£©Ö´ÐÐafterÓï¾ä¼¶´¥·¢Æ÷--Èç¹û´æÔÚÕâÖÖ´¥·¢Æ÷
 
ͬһÖÖÀàÐ͵Ĵ¥·¢Æ÷µÄµã»ð´ÎÐòûÓо­¹ý¶¨Òå¡£Èç¹û¸Ã´ÎÐòºÜÖØÒªµÄ»°£¬ÄÇô½¨Ò齫ËùÓÐÕâЩ²Ù×÷×éºÏµ½Ò»¸ö´¥·¢Æ÷µ±ÖС£
 
3¡¢Ðм¶´¥·¢Æ÷ÖеĹØÁª±êʶ·û
´¥·¢Æ÷µÄ¼¤»îÓï¾äÿ´¦ÀíÒ»ÐÐÊý¾Ý£¬Ðм¶´¥·¢Æ÷¾Í»á¼¤»îÒ»´Î¡£¿ÉÒÔÔÚÕâÖÖÐм¶´¥·¢Æ÷ÄÚ²¿£¬·ÃÎÊÕý±»´¦ÀíµÄ¼Ç¼ÐÐÖеÄÊý¾Ý¡£ÕâÊÇͨ¹ýÁ½¸ö¹ØÁª±êʶ·û--:oldºÍ:new—ʵÏֵġ£¹ØÁª±êʶ·ûÒ²ÊÇPL/SQLµÄÒ»ÖÖÌØÊâµÄ°ó¶¨±äÁ¿¡£±êʶ·ûÇ°ÃæµÄðºÅ£¬¼È˵Ã÷Õâ¶þÕ߶¼Êǰ󶨱äÁ¿£¬Í¬Ê±Ò²ËµÃ÷ËüÃDz»ÊÇÒ»°ãµÄPL/SQL±äÁ¿¡£PL/SQL±àÒëÆ÷»á½«ËüÃÇ¿´×÷ÏÂÃæÕâ¸öÀàÐ͵ļǼ£º
 
Triggering_table%ROWTYPE
 
ÆäÖÐtriggering_tableÊÇÔÚÆäÉ϶¨Òå´¥·¢Æ÷µÄ±íÃû¡£ÓÚÊÇ£¬ÏÂÃæÕâÖÖÒýÓÃ
 
:new.field
 
¾ÍÖ»Óе±ÆäÖеÄfieldÊǸô¥·¢±íÖеÄ×Ö¶ÎÃûʱ²Å»áÓÐЧ¡£
 
´¥·¢Óï¾ä
:old
:new
INSERT
䶨Òå—ËùÓÐ×ֶξùΪNULL
´¥·¢Óï¾äÍê³ÉµÄʱºò£¬Òª²åÈëµÄÖµ
UPDATE
¸üÐÂÒÔÇ°ÏàÓ¦¼Ç¼ÐеÄԭʼֵ
´¥·¢Óï¾äÍê³ÉµÄʱºò£¬Òª¸üеÄÖµ
DELETE
ɾ³ýÒÔÇ°ÏàÓ¦¼Ç¼ÐеÄԭʼֵ
䶨Òå—ËùÓÐ×ֶξùΪNULL
 
×¢Ò⣺INSERTÓï¾äÉÏûÓж¨Òå:old±êʶ·û£¬DELETEÓï¾äÉÏҲûÓж¨Òå:new±êʶ·û¡£Èç¹ûÔÙINSERTÓï¾äÉ


Ïà¹ØÎĵµ£º

sql ÿ×éÊý¾Ýֻȡǰ¼¸ÌõÊý¾ÝµÄд·¨

select *
  from (select row_number() over(partition by t.type order by date desc) rn,
               t.*
          from ±íÃû t)
 where rn <= 2;
typeÒª·ÖµÄÀà
date ÅÅÐò ......

Êý¾Ý¿âÐÐתÁеÄsqlÓï¾ä£¨×ª£©


Êý¾Ý¿âÐÐתÁеÄsqlÓï¾ä
ÎÊÌâÃèÊö
¼ÙÉèÓÐÕÅѧÉú³É¼¨±í(CJ)ÈçÏÂ
Name Subject Result
ÕÅÈý ÓïÎÄ 80
ÕÅÈý Êýѧ 90
ÕÅÈý ÎïÀí 85
ÀîËÄ ÓïÎÄ 85
ÀîËÄ Êýѧ 92
ÀîËÄ ÎïÀí 82
ÏÖÔÚ Ïëд sql Óï¾ä     ²éѯºó½á¹û Ϊ    
ÐÕÃû ÓïÎÄ Êýѧ ÎïÀí
ÕÅÈý 80 90 85
ÀîËÄ 85 92 82& ......

дORACLE SQL

Ò».°´Ó¢ÎÄ×ÖĸÅÅÐò:
        select * from table order by nlssort(name,'NLS_SORT=schinese_pinyin_M');
 ¶þ. дһ¸öSQLÓï¾ä£¬²éѯѡÐÞÁË5Ãſγ̵ÄѧÉúѧºÅºÍÐÕÃû£¨9·ÖÖÓ£©
´ð£ºSQLÓï¾äÈçÏ£º
select stu.sno, stu.sname
from student stu
where (select count(*) from s ......

SQL SERVER 2005 .NET UDF

using System;
using System.Text.RegularExpressions;
using Microsoft.SqlServer.Server;
public partial class UserDefinedFunctions
{
[Microsoft.SqlServer.Server.SqlFunction(IsDeterministic = true,IsPrecise = true)]
public static bool RegExIsMatch(string pattern,string matchString)
{ ......

£¨×ª£©SQL¾­µäÃæÊÔÌ⼯£¨Èý£©


µÚ¶þÊ®Ì⣺
ÔõôÑù³éÈ¡Öظ´¼Ç¼
񡜧
id name
--------
1 test1
2 test2
3 test3
4 test4
5 test5
6 test6
2 test2
3 test3
2 test2
6 test6
²é³öËùÓÐÓÐÖظ´¼Ç¼µÄÊý¾Ý£¬ÓÃÒ»¾äsql À´ÊµÏÖ
create table D(
id varchar (20),
name varchar (20)
)
insert into D values('1','test1')
insert into D v ......
© 2009 ej38.com All Rights Reserved. ¹ØÓÚE½¡ÍøÁªÏµÎÒÃÇ | Õ¾µãµØͼ | ¸ÓICP±¸09004571ºÅ