SQL´¥·¢Æ÷µÄʹÓÃ
Ò»¡¢´´½¨Ò»¸ö¼òµ¥µÄ´¥·¢Æ÷
´¥·¢Æ÷ÊÇÒ»ÖÖÌØÊâµÄ´æ´¢¹ý³Ì£¬ÀàËÆÓÚʼþº¯Êý£¬SQL Server? ÔÊÐíΪ INSERT¡¢UPDATE¡¢DELETE ´´½¨´¥·¢Æ÷£¬¼´µ±ÔÚ±íÖвåÈë¡¢¸üС¢É¾³ý¼Ç¼ʱ£¬´¥·¢Ò»¸ö»òһϵÁÐ T-SQLÓï¾ä¡£
´¥·¢Æ÷¿ÉÒÔÔÚ²éѯ·ÖÎöÆ÷Àï´´½¨£¬Ò²¿ÉÒÔÔÚ±íÃûÉϵãÓÒ¼ü->“ËùÓÐÈÎÎñ”->“¹ÜÀí´¥·¢Æ÷”À´´´½¨£¬²»¹ý¶¼ÊÇҪд T-SQL Óï¾äµÄ£¬Ö»ÊÇÔÚ²éѯ·ÖÎöÆ÷ÀïÒªÏÈÈ·¶¨µ±Ç°²Ù×÷µÄÊý¾Ý¿â¡£
´´½¨´¥·¢Æ÷Óà CREATE TRIGGER
CREATE TRIGGER ´¥·¢Æ÷Ãû³Æ
ON ±íÃû
FOR INSERT¡¢UPDATE »ò DELETE
AS
T-SQL Óï¾ä
×¢Ò⣺´¥·¢Æ÷Ãû³ÆÊDz»¼ÓÒýºÅµÄ¡£
ÈçÏÂÊÇÁª»ú´ÔÊéÉϵÄÒ»¸öʾÀý£¬µ±ÔÚ titles ±íÉϸü¸Ä¼Ç¼ʱ£¬·¢ËÍÓʼþ֪ͨ MaryM¡£
CREATE TRIGGER reminder
ON titles
FOR INSERT, UPDATE, DELETE
AS
EXEC master..xp_sendmail 'MaryM',
'Don''t forget to print a report for the distributors.'
¶þ¡¢É¾³ý´¥·¢Æ÷
Óòéѯ·ÖÎöÆ÷ɾ³ý
ÔÚ²éѯ·ÖÎöÆ÷ÖÐʹÓà drop trigger ´¥·¢Æ÷Ãû³Æ À´É¾³ý´¥·¢Æ÷¡£
Ò²¿ÉÒÔͬʱɾ³ý¶à¸ö´¥·¢Æ÷£ºdrop trigger ´¥·¢Æ÷Ãû³Æ,´¥·¢Æ÷Ãû³Æ...
×¢Ò⣺´¥·¢Æ÷Ãû³ÆÊDz»¼ÓÒýºÅµÄ¡£ÔÚɾ³ý´¥·¢Æ÷֮ǰ¿ÉÒÔÏÈ¿´Ò»Ï´¥·¢Æ÷ÊÇ·ñ´æÔÚ£º
if Exists(select name from sysobjects where name=´¥·¢Æ÷Ãû³Æ and xtype='TR')
ÓÃÆóÒµ¹ÜÀíÆ÷ɾ³ý
ÔÚÆóÒµ¹ÜÀíÆ÷ÖУ¬ÔÚ±íÉϵãÓÒ¼ü->“ËùÓÐÈÎÎñ”->“¹ÜÀí´¥·¢Æ÷”£¬Ñ¡ÖÐËùҪɾ³ýµÄ´¥·¢Æ÷£¬È»ºóµã»÷“ɾ³ý”¡£
Èý¡¢ÖØÃüÃû´¥·¢Æ÷
Óòéѯ·ÖÎöÆ÷ÖØÃüÃû
exec sp_rename ÔÃû³Æ, ÐÂÃû³Æ
sp_rename ÊÇ SQL Server? ×Ô´øµÄÒ»¸ö´æ´¢¹ý³Ì£¬ÓÃÓÚ¸ü¸Äµ±Ç°Êý¾Ý¿âÖÐÓû§´´½¨µÄ¶ÔÏóµÄÃû³Æ£¬Èç±íÃû¡¢ÁÐ±í¡¢Ë÷ÒýÃûµÈ¡£
ÓÃÆóÒµ¹ÜÀíÆ÷ÖØÃüÃû
ÔÚ±íÉϵãÓÒ¼ü->“ËùÓÐÈÎÎñ”->“¹ÜÀí´¥·¢Æ÷”£¬Ñ¡ÖÐËùÒªÖØÃüÃûµÄ´¥·¢Æ÷£¬Ð޸Ĵ¥·¢Æ÷Óï¾äÖеĴ¥·¢Æ÷Ãû³Æ£¬µã»÷“È·¶¨”¡£
ËÄ¡¢more....
INSTEAD OF
Ö´Ðд¥·¢Æ÷Óï¾ä£¬µ«²»Ö´Ðд¥·¢´¥·¢Æ÷µÄ SQL Óï¾ä£¬±ÈÈçÊÔͼɾ³ýÒ»Ìõ¼Ç¼ʱ£¬½«Ö´Ðд¥·¢Æ÷Ö¸¶¨µÄÓï¾ä£¬´Ëʱ²»ÔÙÖ´ÐÐ delete Óï¾ä¡£Àý£º
create trigger f
on tbl
instead of delete
as
insert into Logs...
IF UPDATE(ÁÐÃû)
¼ì²éÊÇ·ñ¸üÐÂÁËijһÁУ¬ÓÃÓÚ insert »ò update£¬²»ÄÜÓÃÓÚ delete¡£Àý£º
create trigger f
on tbl
fo
Ïà¹ØÎĵµ£º
½ñÌìÔÚÓÃUNION½øÐн«Á½¸ö²éѯ½áºÏʱ£¬·¢ÏÖ¼ÓÁËtextÀàÐ͵Ä×ֶνøÈ¥¾Í±¨´íÁË£¬´íÎóÐÅÏ¢ÈçÏ£º
²»ÄÜÒÔ DISTINCT ·½Ê½Ñ¡Ôñ text¡¢ntext »ò image Êý¾ÝÀàÐÍ¡£
¾¹ý·ÔÄ×ÊÁϺó²ÅÖªµÀ£¬ÔÒòÔÚÓÚÈç´Ë²Ù×÷ ÎÞ·¨¶ÔtextÀàÐÍÅжÏÊÇ·ñÖظ´£¬ËùÒÔÒªÔÚUNIONºó¼Ó¸öALL¹Ø¼ü×Ö£¬ÈçÏ£º
select top ......
--SQL½âÃÜctext×Ö¶ÎÄÚÈݺ¯Êý
--exec sp_decrypt 'Ô¼ÊøÃû³Æ'
--exec sp_decrypt 'DF_InsuranceRecord_PeriodUnit'
--exec sp_decrypt '´æ´¢¹ý³ÌÃû³Æ'
--exec sp_decrypt 'sp_SearchPrinting_InsuranceRecord'
--Ö÷ÒªÓÃ;:¶ÁÈ¡systemcommentsÖеÄ×Ö¶ÎÄÚÈÝ¡£°üÀ¨Ô¼Êø,´æ´¢¹ý³ÌµÈµÈ¡£
--ÔÎÄÓ¦¸ÃÀ´×Ôhttp://blog.csdn.net ......
1¡¢join
A±íµÄÖ÷¼üÊÇ×÷ΪB±íµÄÍâ¼ü¡£ÔÚ²éѯµÄʱºò£¬¿ÉÒÔͨ¹ý²»Í¬µÄjoin½«AºÍB±íÁ´½ÓÆðÀ´£¬´Ó¶øµÃµ½²»Í¬µÄ²éѯ½á¹û¡£
* JOIN: Èç¹û±íÖÐÓÐÖÁÉÙÒ»¸öÆ¥Å䣬Ôò·µ»ØÐÐ
* INNER JOIN: Èç¹ûÁ½¸ö±íÖÐÓÐÆ¥ÅäµÄ£¬Ôò·µ»ØÐÐ
* LEFT JOIN: ¼´Ê¹ÓÒ±íÖÐûÓÐÆ¥Å䣬Ҳ´Ó×ó± ......
¡¡²Ù×÷·ûÓÅ»¯
¡¡¡¡IN ²Ù×÷·û
¡¡¡¡ÓÃINд³öÀ´µÄSQLµÄÓŵãÊDZȽÏÈÝÒ×д¼°ÇåÎúÒ׶®£¬Õâ±È½ÏÊʺÏÏÖ´úÈí¼þ¿ª·¢µÄ·ç¸ñ¡£
¡¡¡¡µ«ÊÇÓÃINµÄSQLÐÔÄÜ×ÜÊDZȽϵ͵ģ¬´ÓORACLEÖ´ÐеIJ½ÖèÀ´·ÖÎöÓÃINµÄSQLÓë²»ÓÃINµÄSQLÓÐÒÔÏÂÇø±ð£º
¡¡¡¡ORACLEÊÔͼ½«Æäת»»³É¶à¸ö±íµÄÁ¬½Ó£¬Èç¹ûת»»²»³É¹¦ÔòÏÈÖ´ÐÐINÀïÃæµÄ×Ó²éѯ£¬ÔÙ²éѯÍâ²ãµÄ±í¼Ç¼£ ......
SQL like×Ó¾äµÄÁíÒ»ÖÖʵÏÖ·½·¨,ËٶȱÈlike¿ì(ת)
Ò»°ãÀ´ËµÊ¹ÓÃÄ£ºý²éѯ£¬´ó¼Ò¶¼»áÏëµ½LIKE
select * from table where a like '%×Ö·û%'
Èç¹ûÒ»¸öSQLÓï¾äÖÐÓöà¸ö likeÄ£ºý²éѯ£¬²¢ÇҼǼÌõÊýºÜ´ó£¬ÄÇËÙ¶ÈÒ»¶¨»áºÜÂý¡£
ÏÂÃæÁ½ÖÖ·½·¨Ò²¿ÉʵÏÖÄ£ºý²éѯ£º
select * from table where patindex('%×Ö·û%',a)>0 ......