Académique Documents
Professionnel Documents
Culture Documents
(
CustID INT IDENTITY(1,1) PRIMARY KEY,
CustName VARCHAR(100),
OpBalance MONEY
)
GO
GO
Part I
Part II based on Stored Procedure, Trigger, Data Integrity & Transaction.sql file
1. Create Stored Procedure USP_SaveTransaction, which should insert record in
Trans Table, with following parameters
a. @CustID, @TransType, @Amount, @TransDate
Note:
When “CustID” is not found in Customer Table then it should prompt the user
“Customer does not exists”
When “TransType” other than “C” and “D” then prompt the user “Invalid
Transaction Type”
2. Create Trigger Tri_Trans for Insert, Update, and Delete. Whenever trigger fires
following should be done
a. If “TransType” is “C” i.e. Credit then Opening Balance should be
increased in Customer Table
b. If “TransType” is “D” i.e. Debit then Opening Balance should be decrease
in Customer Table
c. If the “Trans” record is deleted then based on deleted.TransType
i. If it is “C”, i.e. Credit then Opening Balance should be decrease in
Customer Table
ii. If it is “D”, i.e. Debit then Opening Balance should be decrease in
Customer Table
d. If Customer -> OpBalance is less than Zero then nothing should be done.
Prompt the user “Balance is less than zero, so transaction rejected”
3. Create Trigger Tri_Customer_InsteadOf, which should not allow the user for
updating/deleting the record, but it should allow the user to insert the record in
customer table.
PART I-ANWERS:
Note:
When “CustID” is not found in Customer Table then it should prompt the
user
“Customer does not exists”
When “TransType” other than “C” and “D” then prompt the user “Invalid
Transaction Type”
SELECT * FROM CUSTOMER
SELECT * FROM TRANS
BEGIN
PRINT 'CUSTOMER DOES NOT EXIST'
END
ELSE IF @TransType<>'C' AND @TransType<>'D'
BEGIN
PRINT 'Invalid Transaction Type'
END
ELSE
BEGIN
INSERT INTO TRANS(CustID, TransType, Amount, TransDate)
VALUES(@CustID, @TransType, @Amount, @TransDate)
END
END
--UPDATE CUSTOMER
--SET OPBALANCE=300
EXEC UPS_TransactionSummaryReport
6. Disable the Tri_Customer_InsteadOf then achieve the following task
a. Whenever a record is deleted in Customer Table, even it should delete
related Customer records in Trans Table also
i. Method I
1. Used Cascade
ii. Method II
1. Write a Stored Procedure “USP_CustomerDel” by passing Parameter @CustID
2. Use Transactions while deleting the records from both Customer Table
and Trans Table
EXEC USP_CustomerDel 22
SP_HELP TRANS
SELECT * FROM CUSTOMER
SELECT * FROM TRANS