函数

--标量函数
--
创建函数
create function Fun_Cost
(@x int,@y money)
returns money
as
begin
return (@x*@y)
end
--使用函数
alter table goods add 金额 as dbo.Fun_cost(数量,进货价)
go
--验证函数
select * from Goods
go
--表值函数
--
1\内嵌表值函数
create function Fun_Total
(@bt datetime,@ef datetime)
returns table
as
return
(select goods.商品名称,sell.数量 from goods,sell where sell.售出时间 between @bt and @ef and Goods.商品编号=sell.商品编号)
select * from dbo.Fun_Total('2005-1-1','2005-1-31')
go
--2\多语句表值函数
create function Fun_Lan
(@price money)
returns @Fun_Lan table
(
商品编号 int primary key not null,
商品名称 varchar(20) not null,
生产厂商 varchar(30) not null,
进货价 money not null,
进货时间 datetime not null
)
as
begin
insert @Fun_Lan
select 商品编号,商品名称,生产厂商,进货价,进货时间 from goods
where (进货价>@price)
return
end
select * from dbo.Fun_Lan(1000)
go
---


posted @ 2011-11-02 19:46  天璇翼  阅读(302)  评论(0编辑  收藏  举报