Функции для вычисляемых столбцов:
Функция 1:
Подсчет суммарной стоимости промежуточной сделки
Create function dbo.CostOfCouple
(@productId int, @quantity int)
returns int
as
begin
declare @price int
Select @price = w.Price * @quantity
from dbo.warehouse as w
where w.ProductID = @productId
return(@price)
end;
Функция 2:
Вычисляется начальная стоимость сделки (без учета скидок)
Create function dbo.BegCost
(@PurchasementID int)
returns int
as
begin
declare @cost int
Select @cost = sum(PP.TotalCost)
from dbo.PurchProd as PP
where PP.PurchasementID = @PurchasementID
return(@cost)
end;
Функция 3:
Вычисляется конечная стоимость сделки, с учетом всех скидок
Create function dbo.FinCost
(@purchasementID int, @customerID int)
returns int
as
begin
declare @friend bit,
@finCost int,
@begCost int
Select @friend = c.IsFriend
from dbo.customer as c
where c.CustomerID = @customerID
Select @begCost = sum(PP.TotalCost)
from dbo.PurchProd as PP
where PP.PurchasementID = @PurchasementID
if @friend = 0
begin
if @begCost < 1000
set @finCost = @begCost;