Êý¾Ý¿âÔ­ÀíÓëÓ¦ÓÿγÌʵÑéÖ¸µ¼Ê飨¸½´ð°¸£©(2) ÏÂÔر¾ÎÄ

£¨1£©»ùÓÚVIEW_CP_PRICE2000ÊÓͼ£¬²éѯ¼Û¸ñÔÚ2000ÒÔϲúÆ·µÄ²úÆ·±àºÅ¡¢Ãû³ÆºÍ¼Û¸ñ¡£

select ²úÆ·±àºÅ,²úÆ·Ãû³Æ,¼Û¸ñ from VIEW_CP_PRICE2000

£¨2£©»ùÓÚVIEW_GMQKÊÓͼ£¬²éѯ¸÷¿Í»§ÔÚ20004Äê3ÔÂ18ÈÕ¹ºÂò²úÆ·µÄÇé¿ö¡£ select * from VIEW_GMQK where ¹ºÂòÈÕÆÚ='2004-3-18'

3¡¢¸üÐÂÊÓͼ

ÀûÓÃT-SQLÓï¾ä¶ÔÓÚÊÓͼVIEW_12½øÐÐÒÔÏÂÊý¾Ý¸üС£ £¨1£© ²åÈëÒ»ÌõCP¼Ç¼£¨¡¯ '100042','ÊýÂëÏà»ú',3500,2£©¡£ insert into VIEW_CP12

values('100042','ÊýÂëÏà»ú',3500,2,null)

£¨2£© ½«²úÆ·±àºÅΪ¡¯100042¡¯µÄ¼Û¸ñ¸ÄΪ3000¡£ update VIEW_CP12 set ¼Û¸ñ=3000

where ²úÆ·±àºÅ='100042'

£¨3£© ɾ³ý²úÆ·±àºÅΪ¡¯100042¡¯µÄ²úÆ·¡£

delete VIEW_CP12

where ²úÆ·±àºÅ='100042'

4¡¢ÐÞ¸ÄÊÓͼ

½«VIEW_CP_PRICE2000ÊÓͼ²»¼ÓÃÜ¡£ alter view VIEW_CP_PRICE2000 as

select* from CP where ¼Û¸ñ<2000 with check option

5¡¢É¾³ýÊÓͼ

½«VIEW-GMQKÊÓͼɾ³ý¡£ drop view VIEW_GMQK

ʵÑé°Ë

1¡¢ÉùÃ÷Óαê

£¨1£©Ê¹ÓÃSQL-92Óï·¨ÉùÃ÷Ò»Ö»½øÖ»¶ÁÓαêCUR1£ºÒªÇó½á¹û¼¯20XXÄê3ÔÂ18ÈÕÏúÊÛÇé¿ö¡£

declare CUR1 insensitive cursor for

select *from CPXSB where ÏúÊÛÈÕÆÚ='2004-3-18' for read only

£¨2£©Ê¹ÓÃT-SQLÀ©Õ¹ÉùÃ÷Ò»¹ö¶¯¶¯Ì¬ÓαêCUR2£ºÒªÇó½á¹û¼¯Îª¿Í»§ÐÅÏ¢£¬²¢ÄÜͨ¹ý¸ÃÓαêÐ޸Ŀͻ§Ãû³ÆÁС£ declare CUR2 cursor scroll for

select * from XSS

for update of ¿Í»§Ãû³Æ

2¡¢´ò¿ªÓαê

´ò¿ªCUR2Óαꡣ open CUR2

3¡¢¶ÁÈ¡ÓαêÖÐÊý¾Ý

±àд³ÌÐò£¬ÊµÏÖÒÀ´Î¶ÁÈ¡ÓαêCUR2Öи÷ÐÐÊý¾Ý¡£

fetch next from CUR2

while @@FETCH_STATUS=0 begin

fetch next from CUR2 end

4¡¢¹Ø±ÕÓαê

¹Ø±ÕCUR2Óαꡣ close CUR2

5¡¢ÊÍ·ÅÓαê

ÊÍ·ÅCUR2Óαꡣ deallocate CUR2

ʵÑé¾Å

1¡¢±äÁ¿µÄ¶¨ÒåºÍ¸³Öµ

´´½¨Ò»ÃûΪ Customer_nameµÄ¾Ö²¿±äÁ¿£¬²¢ÔÚSELECTÓï¾äÖÐʹÓøñäÁ¿²éÕÒ¡°¹ãµç¹«Ë¾¡±¹ºÂò²úÆ·µÄÇé¿ö¡£

declare @Customer_name varchar(20) set @Customer_name='¹ãµç¹«Ë¾'

select ¿Í»§Ãû³Æ,CPXSB.*from CPXSB,XSS

where ¿Í»§Ãû³Æ=@Customer_name and XSS.¿Í»§±àºÅ=CPXSB.¿Í»§±àºÅ

2¡¢Óû§×Ô¶¨ÒåÊý¾ÝÀàÐͶ¨Ò塢ʹÓúÍɾ³ý

ÓÃSQLÃüÁÒåÒ»ÃûΪCustomer_idµÄÓû§×Ô¶¨ÒåÊý¾ÝÀàÐÍ£¬ÒªÇóchar(6)£¬NOT NULL£¬ ²¢°Ñ¸Ã×Ô¶¨ÒåÊý¾ÝÀàÐÍÓÃÀ´¶¨ÒåXSS±íÖеĿͻ§±àºÅ£¬È»ºóɾ³ý¸Ã×Ô¶¨ÒåÊý¾ÝÀàÐÍ£¬ÇëÐðÊö¸Ã¹ý³Ì£¬²¢Ð´³öÏà¹ØÓï¾ä¡£

sp_addtype 'Customer_id','char(6)','not null' sp_droptype 'Customer_id'

ÏÈÓô´½¨Óï¾ä×Ô¶¨ÒåÊý¾ÝÀàÐÍ£¬È»ºó´ò¿ª¡°Êý¾Ý¿â-CPXSB-±í¡±£¬ÓÒ»÷XSS,Ñ¡Ôñ¡°Éè¼Æ±í¡±£¬½«¿Í»§±àºÅµÄÊý¾ÝÀàÐÍ ¸ÄΪCustomer_id,±£´æ¼´¿É

½«¿Í»§±àºÅµÄÊý¾ÝÀàÐ͸ĻØchar(6),È»ºóÓõڶþ¸öÓï¾äɾ³ý¸ÃÓû§×Ô¶¨ÒåÊý¾ÝÀàÐÍ

3¡¢T-SQLÓïÑÔ±à³Ì

£¨1£©ÓÃT-SQLÓïÑÔ±à³ÌÊä³ö3¡«300Ö®¼äÄܱ»7Õû³ýµÄÊý¡£ declare @i int,@j int set @i=3 set @j=7

while @i<=300 begin

if(@i%@j)=0 print @i

set @i=@i+1

end

£¨2£©ÓÃT-SQLÓïÑÔ±à³ÌÊä³ö3¡«100Ö®ÄÚµÄËØÊý¡£ declare @i int,@j int,@t int set @i=3

while @i<100 begin

set @j=2 while @j<@i begin

set @t=@i%@j if @t=0 break set @j=@j+1 if(@j>=@i) print @i end

set @i=@i+1 end

ʵÑéÊ®

1¡¢º¯ÊýµÄ¶¨Òå

¶ÔÓÚCPXSÊý¾Ý¿â£¬¶¨ÒåÍê³ÉÈçϹ¦Äܵĺ¯Êý£º £¨1£©¾Ý²úÆ·Ãû³Æ£¬²éѯ¸Ã²úÆ·µÄÏà¹ØÐÅÏ¢£»£¨º¯ÊýÃûΪFU_CP£© create function FU_CP(@²úÆ·Ãû³Æ char(30)) returns table as return

select * from CP where ²úÆ·Ãû³Æ=@²úÆ·Ãû³Æ

£¨2£©°´Ä³Äêij¼¾¶Èͳ¼Æ¸ø¶¨²úÆ·Ãû³ÆµÄÏúÊÛÊýÁ¿¼°ÏúÊÛ½ð¶î£»·Ö±ðÓÃÃûΪFU1_CPXSÄÚǶ±íÖµº¯ÊýºÍÃûΪFU2_CPXSµÄ¶àÓï¾ä±íÖµº¯Êý¡£

create function FU1_CPXS(@year int,@quarter int,@²úÆ·Ãû³Æ char(30)) returns table as return

select ²úÆ·Ãû³Æ,sum(ÊýÁ¿) as ÏúÊÛÊýÁ¿,sum(ÏúÊÛ¶î) as ÏúÊÛ½ð¶î from CP,CPXSB

where CP.²úÆ·±àºÅ=CPXSB.²úÆ·±àºÅ and datepart(yy,ÏúÊÛÈÕÆÚ)=@year and datepart(qq,ÏúÊÛÈÕÆÚ)=@quarter and ²úÆ·Ãû³Æ=@²úÆ·Ãû³Æ