You are on page 1of 2

Функции

1)
create function task_function1(@velik nchar(20))
returns nchar(20)
as
begin
declare @type nchar(20)
select @type = T.Type
from Bicycle as B, TypesOfBicycle as T
where B.IdType = T.IdType and B.Name = @velik
return @type
end

declare @type nchar(20)


execute @type = task_function1 'Giant'
raiserror('Тип велосипеда %s', 0, 1, @type)

2)
create function task_function2(@id_client int)
returns table

as

return
(
select B.Name as Bicycle, T.Type as [Type]
from Bicycle as B, TypesOfBicycle as T, [Rent date] as R
where B.IdType = T.IdType and R.BicycleId = B.IdBicycle and R.ClientId = @id_client
)

select * from task_function2(1)

Триггеры
1)
create trigger check_date
on [Rent date]
for insert
as
begin
declare @dateto datetime
declare @datefrom datetime
select @dateto = [To], @datefrom = [From]
from inserted

if(@dateto < @datefrom)


begin
raiserror('Запись выдачи не может быть записана, дата выдачи велосипеда не может быть
больше даты возврата велосипеда клиентом', 0, 1)
rollback transaction
end
else PRINT('Данные добавлены успешно');
end

insert into [Rent date](IdRent, ClientId, BicycleId, [From], [To])


values (34, 10, 5, '2018-09-11', '2018-08-11')
insert into [Rent date](IdRent, ClientId, BicycleId, [From], [To])
values (34, 10, 5, '2018-09-11', '2018-10-11')

2)
create trigger OnDelete on [Rent date]
for delete
as
declare @idrent int
declare @clientid int
declare @bicycleid int
declare @from date
declare @to date
declare @time smalldatetime = GETDATE()
select @idrent = deleted.IdRent, @clientid = deleted.ClientId, @bicycleid =
deleted.BicycleId, @from = deleted.[From], @to = deleted.[To]
from deleted
insert into TableOfChanging(IdRent, IdClient, IdBicycle, [From], [To], TimeOfAdding)
values(@idrent, @clientid, @bicycleid, @from, @to, @time)

You might also like