?? 課堂練習.txt
字號:
二、學生作業
利用globaltoyz完成以下作業
3、創建函數,輸出指定玩具號、指定數量的商品的總價錢
Create Function PayAmount(@Toyid char(6),@qty int)
returns money
as
begin
declare @price money
Select @price=價格 from 玩具 Where 玩具號=@Toyid
Return @price*@qty
end
調用函數
--Select dbo.PayAmount('000001',10)
----------------------------------------------------------------------------------------------------------------------
4、創建函數,根據指定的定單號,輸出定單號、玩具名、數量清單
Create Function OrderDetails(@Orderid char(6))
returns Table
as
Return(Select 定單號,玩具名,數量
from 定單詳情 d,玩具 w
where d.玩具號=w.玩具號
and d.定單號=@Orderid)
go
調用函數
Select * from OrderDetails('000001')
----------------------------------------------------------------------------------------------------------------------
5、創建函數,指定玩具類型,輸出類型名、玩具名、銷售數量清單
Create Function GetToyQty(@toytype char(3))
returns @return Table
(類型 char(4),玩具名 varchar(20),銷售數量 smallint)
as
begin
insert into @return select '進貨',玩具名,sum(銷售數量) from 玩具 a inner join
月銷售情況 b on a.玩具號 = b.玩具號 where a.類別號=@toytype group by 玩具名
insert into @return select '銷售',玩具名,sum(數量+銷售數量) as 進貨數量 from 玩具 a inner join
月銷售情況 b on a.玩具號 = b.玩具號 where a.類別號=@toytype group by 玩具名
return
end
go
調用函數
select * from GetToyQty('001') order by 玩具名
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -