Курсовые работы / Разработка приложения на ЯВУ для доступа к базе данных
.pdfСписок использованной литературы
1.К. Дж. Дейт Введение в системы баз данных = Introduction to
Database Systems — 8-е изд. — М.: Вильямс, 2006. — С. 1328. — ISBN 5-8459-0788-8
2.http://msdn.microsoft.com/library - MSDN Russia
31
Приложения
Приложение 1. Функциональные зависимости
«Покупка»
(Purchase_ID) → (Purchase_ID) (Purchase_ID) → Employee_ID (Purchase_ID) → Supplier_ID (Purchase_ID) → Customer_ID (Purchase_ID) → Medicine_ID (Purchase_ID) → MedicineAmount
«Лекарство»
(Medicine_ID) → (Medicine_ID) (Medicine_ID) → Dose (Medicine_ID) → WayOfUse (Medicine_ID) → Name
«Покупатель»
(Customer_ID) → (Customer_ID) (Customer_ID) → FIO (Customer_ID) → ContactPhone
«Склад»
(Warehouse_ID) → (Warehouse_ID) (Warehouse_ID) → Address (Warehouse_ID) → ContactPhone (Warehouse_ID) → DateOfOpening
32
(Warehouse_ID) → DateOfClosing
«Работник»
(ID_Работника) → (Employee_ID) (Employee_ID) → FIO (Employee_ID) → PassportNumber (Employee_ID) → Salary
(Employee_ID) → EmploymentHistoryNumber
(Employee_ID) → DateOfAcceptance
(Employee_ID) → DateOfDismissal
«Поставщик»
(Supplier_ID) → (Supplier_ID) (Supplier_ID) → OrganizationName (Supplier_ID) → ContactPhone (Supplier_ID) → DateOfConclusion (Supplier_ID) → DateOfTermination (Supplier_ID) → Warehouse_ID
«Поставка»
(Supplier_ID,Medicine_ID) → (Supplier_ID,Medicine_ID) (Supplier_ID,Medicine_ID) → Price
33
Приложение 2. Код хранимой процедуры
Create procedure CustomerReport @output varchar(8000) output as
declare @medicineprofit money declare @cutomerprofit money declare @cutname varchar(255) declare @medname varchar(255) declare @dose int
declare @price money
declare @cuttemp varchar (255) declare @totalprofit money select @output = ''
declare statistic cursor for
select FIO, Medicine.Name, Dose, Price, SUM(MedicineAmount*Price) from Customer join Purchase on Purchase.Customer_ID = Customer.Customer_ID
join Supply on (Supply.Medicine_ID = Purchase.Medicine_ID )AND (Supply.Supplier_ID = Purchase.Supplier_ID)
join Medicine on Purchase.Medicine_ID = Medicine.Medicine_ID group by FIO,Medicine.Name,Dose,Price
select @cutomerprofit = 0 select @totalprofit = 0 open statistic
--пробный fetch
fetch statistic into @cutname,@medname, @dose, @price, @medicineprofit --проверка статуса --крах
if (@@fetch_status=-2) begin
select @output = @output + 'Ошибка при выполнении первого FETCH' close statistic
return
end --отсутствие данных
if (@@fetch_status=-1) begin
select @output = @output + 'Данные не найдены' close statistic
return
end
--нормальный ход событий
select @cutomerprofit = @cutomerprofit+@medicineprofit select @totalprofit = @totalprofit + @medicineprofit select @cuttemp = @cutname
select @output = @output + 'Покупатель '+@cutname+' купил/а '+@medname+' с дозировкой '+str(@dose) + 'мг на сумму ' + str(@medicineprofit) +' рублей по цене '+str(@price) +' рублей
'
while(@@FETCH_STATUS=0) begin
--выборка следующей записи
fetch statistic into @cutname,@medname, @dose, @price,
@medicineprofit
--если поменялся город,то: if(@cutname!=@cuttemp)
begin
select @output = @output + 'Покупатель '+@cuttemp+'
купил/а лекарств на сумму '+str(@cutomerprofit)+' рублей
'
select @cuttemp = @cutname
34
select @cutomerprofit = 0
end
--если закончились данные if (@@FETCH_STATUS = -1)
begin
break
end
select @output = @output + 'Покупатель '+@cutname+' купил/а '+@medname+' с дозировкой '+str(@dose) + ' на сумму ' + str(@medicineprofit) +' по цене '+str(@price)+'
'
select @cutomerprofit = @cutomerprofit+@medicineprofit select @totalprofit = @totalprofit+@medicineprofit
end close statistic
select @output = @output + 'Покупатель '+@cuttemp+' купил/а лекарств на сумму '+str(@cutomerprofit)+' рублей
'
select @output = @output + '-----------------------------
'
select @output = @output + 'Всего куплено на '+str(@totalprofit)+' рублей
'
deallocate statistic
return
Приложение 3. Код триггера
Create Trigger InsertSupplier on Supplier for INSERT as declare @insterm datetime
declare @insconc datetime declare @dw datetime begin
if UPDATE(DateOfConclusion) begin
select @dw = getdate(), @insterm = inserted.DateOfTermination,@insconc = inserted.DateOfConclusion from inserted
if @insconc>=@dw
begin
raiserror 30001 'Дата подписания больше
текущей'
rollback tran return
end
if @insconc>=@insterm begin
raiserror 30001 ‘Дата подписания больше
даты расторжения'
rollback tran return
end
end
35
if UPDATE(DateOfTermination) begin
select @dw=GETDATE(), @insterm = inserted.DateOfTermination,@insconc = inserted.DateOfConclusion from inserted
if @insterm>=@dw begin
raiserror 30001 ‘Дата расторжения больше
текущей'
rollback tran return
end
if @insconc>=@insterm begin
raiserror 30001 ‘Дата подписания больше
даты расторжения'
rollback tran return
end
end
if UPDATE(Warehouse_ID) begin
select @dw = Warehouse.DateOfClosing from inserted,Warehouse where inserted.Warehouse_ID = Warehouse.Warehouse_ID
if Not(@dw IS NULL) begin
raiserror 30001 'Этот склад уже закрыт!' rollback tran
return
end
end
end
36