Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories

Welcome to the new platform of Programmer's Heaven! We apologize for the inconvenience caused, if you visited us from a broken link of the previous version. The main reason to move to a new platform is to provide more effective and collaborative experience to you all. Please feel free to experience the new platform and use its exciting features. Contact us for any issue that you need to get clarified. We are more than happy to help you.

Help With SQL Triggers!!

JoGoJoGo Posts: 5Member
This is what I had to do for my assigment!! I am just checking if this is correct!! Any help would be nice!! Thank You in Advance!

Create and test the following triggers for the database created in Programming Assignment 6.

1. A trigger that prevents the number of miles on a truck from being decreased.

This what I did for this one!

CREATE TRIGGER TURCK_MILES ON TRUCK
FOR INSERT,UPDATE
AS
declare @v1 decimal(8,2)
declare @v2 decimal(8,2)
select @v1 = TRUCK_MILES FROM TURCK
BEGIN
ROLLBACK
END


2. A trigger that prevents the Buy Date for truck from being after the current date.

This is what I put!

CREATE TRIGGER TRUCK_BUY_DATE ON TRUCK
FOR INSERT,UPDATE
AS
declare @date DATETIME
select @date=inserted.TRUCK_BUY_DATE FROM INSERTED
IF @date>GETDATE()
BEGIN
ROLLBACK TRANSACTION
END


3. A trigger that ensures that the Base Code in the Truck table is always in the Base table

My Answer!

CREATE TRIGGER BASE_CODE ON TRUCK
FOR INSERT,UPDATE
AS
declare @1_base_code int
declare @2_base_code int
select @1_base_code=inserted.BASE_CODE
FROM INSERTED,BASE,TRUCK
IF @1_base_code<>@2_base_code
BEGIN
ROLLBACK TRANSACTION
END

Sign In or Register to comment.