½ðÁê¿Æ¼¼Ñ§ÔºÊµÑ鱨¸æ
group by sales.title_id,titles.price,titles.title_id
having(sales.title_id=titles.title_id)
£¨2£© select title_id,sum(qty) qty from sales group by title_id £¨3£©select title_id,sum(qty) qty from sales group by title_id having
(sum(qty)>30)
£¨4£© select count(title_id) num from titles where price is not null
3¡¢Á¬½Ó²éѯ
£¨1£©select distinct(a.title_id),b.au_id,a.au_id from titleauthor
a,titleauthor b
where a.title_id=b.title_id
£¨2£©select a.title,b.stor_name,c.qty from titles a,stores b ,sales c where a.title_id=c.title_id and b.stor_id=c.stor_id £¨3£©select a.title,b.pub_name from titles a,publishers b where a.pub_id=b.pub_id
4¡¢×Ó²éѯ
£¨1£© select * from titles,sales where sales.title_id=titles.title_id £¨2£© select sales.title_id,titles.title,titles.[type],titles.price from
titles,sales where sales.title_id=titles.title_id
¶þ¡¢º¯Êý
£¨1£©CREATE FUNCTION Sage_func ( @vardate datetime , @curdate datetime ) RETURNS tinyint AS BEGIN RETURN datediff ( yy , @vardate , @curdate ) END
select SNO as ѧºÅ, SName as ÐÕÃû, dbo.Sage_func(Birthday,getdate()) as ÄêÁä
from Student
£¨2£©CREATE FUNCTION grade_func ( @Scorce int ) RETURNS tinyint AS BEGIN RETURN @Scorce/20 END
select SName as ÐÕÃû, dbo.grade_func(Scorce) as Î弶·ÖÖÆ from Grade,Student where Grade.SNO=Student.SNO--Èý¡¢Á÷³Ì¿ØÖÆ
Èý¡¢Á÷³Ì¿ØÖÆ
(1)IF exists (select * from Student where SNO='19920101')
BEGIN
select * from Student where SNO='19920101'
END ELSE BEGIN
insert into Student values('19920101','Íõ¾ü','ÄÐ','CS01','ϹØ
#','1976-12-21 00:00:00',1.82,10) END
delete from Student where SNO='19920101' (2) declare
20
½ðÁê¿Æ¼¼Ñ§ÔºÊµÑ鱨¸æ
@sum int, @start int set @start=1 set @sum=0
begin
while(@start<101) begin
set @sum=@sum+@start set @start=@start+1
end
print @sum end
Áù¡¢ÊµÑéÌå»áºÍÊÕ»ñ
ʵÑéÏîÄ¿Ãû³Æ£º ´æ´¢¹ý³ÌºÍ´¥·¢Æ÷ ʵÑéѧʱ£º ͬ×éѧÉúÐÕÃû£º ʵÑéµØµã£º ʵÑéÈÕÆÚ£º ʵÑé³É¼¨£º Åú¸Ä½Ìʦ£º Åú¸Äʱ¼ä£º
ʵÑé6 ´æ´¢¹ý³ÌºÍ´¥·¢Æ÷
Ò»¡¢ÊµÑéÄ¿µÄºÍÒªÇó
1¡¢ ͨ¹ý¶Ô³£ÓÃϵͳ´æ´¢¹ý³ÌµÄʹÓã¬Á˽â´æ´¢¹ý³ÌµÄÀàÐÍ¡£
2¡¢ ͨ¹ý´´½¨ºÍÖ´Ðд洢¹ý³ÌÁ˽â´æ´¢¹ý³ÌµÄ»ù±¾¸ÅÄîÕÆÎÕʹÓô洢¹ý³Ì²Ù×÷¼¼Çɺͷ½·¨ 3¡¢ ͨ¹ý¶ÔÒÑ´´½¨µÄ´æ´¢¹ý³ÌµÄ¸Ä±ä£¬ÕÆÎÕÐ޸ġ¢É¾³ý´æ´¢¹ý³ÌµÄ¼¼ÇÉ¡£ 4¡¢ Á˽ⴥ·¢Æ÷µÄ»ù±¾¸ÅÄÀí½â´¥·¢Æ÷µÄ¹¦ÄÜ¡£ 5¡¢ ÕÆÎÕ´´½¨¡¢Ð޸ĺÍɾ³ýºÍʹÓô¥·¢Æ÷µÄ²Ù×÷·½·¨¡£
¶þ¡¢ÊµÑéÉ豸¡¢»·¾³
É豸£º±¼ÌÚ¢ô»ò±¼ÌÚ¢ôÒÔÉϼÆËã»ú
»·¾³£ºWINDOWS 2000 SERVER»òWINDOWS 2003 SERVER¡¢SQL Server2005ÖÐÎÄ°æ¡£
Èý¡¢ÊµÑé²½Öè
1¡¢¸ù¾ÝÌâÄ¿ÒªÇóÊìϤSQL Server2005µÄ¸÷ÖÖ¹ÜÀí¹¤¾ß¡£ 2¡¢·ÖÎöÌâÒ⣬Öصã·ÖÎöÌâÄ¿ÒªÇ󲢸ø³ö½â¾ö·½·¨¡£
3¡¢°´ÌâÄ¿ÒªÇóÍê³Éʵ¼Ê²Ù×÷ÈÎÎñ£¬²¢½«Ïà¹ØÎĵµ×ÊÁϱ£´æÔÚÒÔ×Ô¼ºÑ§ºÅÃüÃûµÄÎļþ¼ÐÖС£
21
½ðÁê¿Æ¼¼Ñ§ÔºÊµÑ鱨¸æ
4¡¢Ìá½»Íê³ÉµÄʵÑé½á¹û¡£
ËÄ¡¢ÊµÑéÄÚÈÝ
Ò»¡¢´æ´¢¹ý³ÌµÄÀàÐÍ¡£
1¡¢Ê¹ÓÃsp_helptext²é¿´byroyalty´æ´¢¹ý³ÌµÄÎı¾£¬¸Ã´æ´¢¹ý³ÌÔÚÊý¾Ý¿âpubsÖС£ ¶þ¡¢´´½¨ÓëÖ´Ðд洢¹ý³Ì
1¡¢ÔÚMyDBÖд´½¨´æ´¢¹ý³Ìproc_1£¬ÒªÇóʵÏÖÈçϹ¦ÄÜ£º²úÉúѧ·ÖΪ4µÄ¿Î³ÌѧÉúÑ¡¿ÎÇé¿öÁÐ±í£¬ÆäÖаüÀ¨¿Î³ÌºÅ¡¢¿Î³ÌÃû¡¢Ñ§·Ö¡¢Ñ§ºÅ¡¢ÐÕÃû¡¢×¨Òµ¡¢ÐÔ±ðµÈ¡£²¢µ÷Óô˴洢¹ý³Ì£¬ÏÔʾִÐнá¹û¡£
2¡¢ÔÚMyDBÖд´½¨´æ´¢¹ý³Ìproc_2£¬ÒªÇóʵÏÖÈçϹ¦ÄÜ£ºÊäÈëרҵÃû³Æ£¬²úÉú¸ÃרҵѧÉúµÄÑ¡¿ÎÇé¿öÁÐ±í£¬ÆäÖаüÀ¨×¨Òµ¡¢Ñ§ºÅ¡¢ÐÕÃû¡¢¿Î³ÌºÅ¡¢¿Î³ÌÃû¡¢³É¼¨¡¢Ñ§·ÖµÈ¡£²¢µ÷Óô˴洢¹ý³Ì£¬ÏÔʾ¡°¼ÆËã»úÓ¦Óá±×¨ÒµÑ§ÉúµÄÑ¡¿ÎÇé¿öÁÐ±í¡£
3¡¢ÔÚMyDBÖд´½¨´æ´¢¹ý³Ìproc_3£¬ÒªÇóʵÏÖÈçϹ¦ÄÜ£ºÊäÈëѧÉúѧºÅ£¬¸ù¾Ý¸ÃѧÉúËùÑ¡¿Î³ÌµÄ×Üѧ·ÖÏÔʾÌáʾÐÅÏ¢£¬Èç¹û×Üѧ·Ö<9,ÔòÏÔʾ¡°´ËѧÉúѧ·Ö²»×㣡¡±£¬·ñÔòÏÔʾ¡°´ËѧÉúѧ·ÖÒÑ×㣡¡±£¬²¢µ÷Óô˴洢¹ý³Ì£¬ÏÔʾ¡°19920102¡±Ñ§ÉúµÄ×Üѧ·ÖÇé¿ö¡£ Èý¡¢Ð޸Ĵ洢¹ý³Ì
1¡¢¶ÔMyDBÖÐÒÑ´´½¨µÄ´æ´¢¹ý³Ìproc_1½øÐÐÐ޸ģ¬ÒªÇóÔÚÏÔʾÁбíÖÐÔö¼Ó°à¼¶×ֶΣ¬¼´²úÉúѧ·ÖΪ¡°4¡±µÄ¿Î³ÌѧÉúÑ¡¿ÎÇé¿öÁÐ±í£¬ÆäÖаüÀ¨¿Î³ÌºÅ¡¢¿Î³ÌÃû¡¢Ñ§·Ö¡¢Ñ§ºÅ¡¢ÐÕÃû¡¢×¨Òµ¡¢°à¼¶¡¢ÐÔ±ðµÈ¡£
2¡¢ÔÚMyDBÖд´½¨µÄ´æ´¢¹ý³Ìproc_2½øÐÐÐ޸ģ¬ÒªÇóʵÏÖÈçϹ¦ÄÜ£ºÊäÈëרҵÃû³Æ£¬²úÉú¸ÃרҵËùÓÐÄÐÉúµÄÑ¡¿ÎÇé¿öÁÐ±í£¬ÆäÖаüÀ¨×¨Òµ¡¢Ñ§ºÅ¡¢ÐÕÃû¡¢¿Î³ÌºÅ¡¢¿Î³ÌÃû¡¢³É¼¨¡¢Ñ§·ÖµÈ¡£²¢µ÷ÓÃÐ޸ĺóµÄ´æ´¢¹ý³Ì£¬ÏÔʾ¡°¼ÆËã»úÓ¦Óá±×¨ÒµÄÐÉúµÄÑ¡¿ÎÇé¿öÁÐ±í¡£ 3¡¢¶ÔMyDBÖÐÒÑ´´½¨µÄ´æ´¢¹ý³Ìproc_3½øÐÐÐ޸ģ¬ÒªÇóʵÏÖÈçϹ¦ÄÜ£ºÊäÈëѧÉúѧºÅ£¬¸ù¾Ý¸ÃѧÉúËùÑ¡¿Î³ÌµÄ×Üѧ·ÖÏÔʾÌáʾÐÅÏ¢£¬Èç¹û×Üѧ·Ö<9£¬ÔòÏÔʾ¡°´ËѧÉúËùÑ¡×Üѧ·ÖΪXXX£¬Ñ§·Ö²»×㣡¡±£¬·ñÔòÏÔʾ¡°´ËѧÉúËùÑ¡×Üѧ·ÖΪXXX£¬Ñ§·ÖÒÑ×㣡¡±¡£²¢µ÷ÓÃÐ޸ĺóµÄ´æ´¢¹ý³Ì£¬ÏÔʾ¡°19920102¡±Ñ§ÉúµÄ×Üѧ·ÖÇé¿ö¡£ ËÄ¡¢É¾³ý´æ´¢¹ý³Ì
1¡¢É¾³ýMyDBÖеĴ洢¹ý³Ìproc_1¡£ Îå¡¢´´½¨´¥·¢Æ÷
1¡¢´´½¨´¥·¢Æ÷trigger_1,ʵÏÖµ±ÐÞ¸ÄѧÉú±í£¨Student£©ÖеÄÊý¾Ýʱ£¬ÏÔʾÌáʾÐÅÏ¢¡°Ñ§ÉúÇé¿ö±í±»ÐÞ¸ÄÁË¡±¡£
2¡¢ÔÚMyDBÖд´½¨´¥·¢Æ÷trigger_2,ʵÏÖÈçϹ¦ÄÜ£ºµ±ÔÚѧÉú³É¼¨±í£¨Gread£©ÖÐɾ³ýÒ»ÌõѧÉúÑ¡¿ÎÐÅÏ¢ºó£¬×Ô¶¯ÊµÏÖ¸üиÃѧÉúÔÚѧÉúÇé¿ö±í£¨Student£©ÖеÄ×Üѧ·ÖÐÅÏ¢¡£ 3¡¢´´½¨´¥·¢Æ÷trigger_3,ʵÏÖµ±ÐÞ¸ÄѧÉúÇé¿ö±í£¨Student£©ÖеÄij¸öѧÉúµÄѧºÅʱ£¬¶ÔӦѧÉú³É¼¨±í£¨Grade£©ÖеÄѧºÅÒ²×÷Ð޸ġ£ Áù¡¢Ð޸Ĵ¥·¢Æ÷
22
½ðÁê¿Æ¼¼Ñ§ÔºÊµÑ鱨¸æ
1¡¢¶ÔÒÑ´´½¨µÄ´¥·¢Æ÷trigger_1½øÐÐÐ޸ģ¬ÊµÏÖµ±ÐÞ¸ÄѧÉúÇé¿ö±í£¨Student£©ÖеÄÊý¾Ýʱ£¬ÏÔʾÌáʾÐÅÏ¢¡°Ñ§ÉúÇé¿ö±íÖÐXXXºÅѧÉú¼Ç¼±»ÐÞ¸ÄÁË¡±¡£ Æß¡¢É¾³ý´¥·¢Æ÷
1¡¢É¾³ýѧÉúÇé¿ö±íÉϵĴ¥·¢Æ÷trigger_1¡£
Îå¡¢ÎÊÌâ½â´ð¼°ÊµÑé½á¹û
Ò»¡¢´æ´¢¹ý³ÌµÄÀàÐÍ
use pubs exec sp_helptext byroyalty
¶þ¡¢´´½¨ÓëÖ´Ðд洢¹ý³Ì
1¡¢if exists (select name from sysobjects where name ='proc_8_t1 'and type
='p') drop procedure proc_8_t1 go create procedure proc_8_t1
as select xskc.¿Î³ÌºÅ,¿Î³ÌÃû,xskc.ѧ·Ö,xsqk.ѧºÅ,ÐÕÃû,ϵ±ð,רҵ,ÐÔ±ð from xsqk,xskc,xscj
where xscj.¿Î³ÌºÅ=xskc.¿Î³ÌºÅand xscj.ѧºÅ=xsqk.ѧºÅand xscj.ѧ·Ö='4' exec sp_helptext proc_8_t1 exec proc_8_t1
2¡¢if exists (select name from sysobjects where name ='proc_8_t2' and
type='p') drop procedure proc_8_t2
Go create procedure proc_8_t2(@speciality varchar(255))
As select רҵ,xscj.ѧºÅ,ÐÕÃû,xscj.¿Î³ÌºÅ,¿Î³ÌÃû,xscj.ѧ·Ö,xscj.³É¼¨ from xsqk,xscj,xskc
where xsqk.רҵ=@speciality and xsqk.ѧºÅ=xscj.ѧºÅand xscj.¿Î³ÌºÅ=xskc.¿Î³ÌºÅ exec sp_helptext proc_8_t2 exec proc_8_t2 'ÐÅÏ¢¹ÜÀí'
3¡¢if exists(select name from sysobjects where name = 'proc_8_t3' and type
='p') drop procedure proc_8_t3
Go create procedure proc_8_t3(@ѧºÅ varchar(255)) As declare @×Üѧ·Ö char(2)
select @×Üѧ·Ö=×Üѧ·Ö from xsqk where ѧºÅ=@ѧºÅ if(@×Üѧ·Ö<9) print 'ѧ·Ö²»×ã' else print'ѧ·ÖÒѳä×ã'
go exec sp_helptext proc_8_t3 exec proc_8_t3 '02020101'
Èý¡¢Ð޸Ĵ洢¹ý³Ì
1¡¢alter procedure proc_8_t1
as select xskc.¿Î³ÌºÅ,¿Î³ÌÃû,xskc.ѧ·Ö,xsqk.ѧºÅ,ÐÕÃû,ϵ±ð,רҵ,ÐÔ±ð,°à¼¶ from xsqk,xskc,xscj
where xscj.¿Î³ÌºÅ=xskc.¿Î³ÌºÅand xscj.ѧºÅ=xsqk.ѧºÅand xscj.ѧ·Ö='4' exec proc_8_t1
2¡¢alter procedure proc_8_t2(@speciality varchar(255))
As select רҵ,xscj.ѧºÅ,ÐÕÃû,xscj.¿Î³ÌºÅ,¿Î³ÌÃû,xscj.ѧ·Ö,xscj.³É¼¨ from xsqk,xscj,xskc
where xsqk.רҵ=@speciality and xsqk.ѧºÅ=xscj.ѧºÅ and xscj.¿Î³ÌºÅ=xskc.¿Î³ÌºÅ and xsqk.ÐÔ±ð='ÄÐ' exec proc_8_t2 'ÐÅÏ¢¹ÜÀí'
3¡¢alter procedure proc_8_t3(@ѧºÅ varchar(255))
As declare @×Üѧ·Ö varchar(255)
23