Database & SQL

Moderators: None (Apply to moderate this forum)
Number of threads: 1194
Number of posts: 2247

This Forum Only
Post New Thread
Single Post View       Linear View       Threaded View      f

Report
Help with SQL Triggers!! Posted by JoGo on 14 Nov 2009 at 11:48 PM
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

Report
Re: Help with SQL Triggers!! Posted by zajactb on 6 Jan 2010 at 2:53 AM
Good way to do so is to use STORED PROCEDURES and CONSTRAINTS.
www.shs-g.com



 

Recent Jobs

Official Programmer's Heaven Blogs
Web Hosting | Browser and Social Games | Gadgets

Popular resources on Programmersheaven.com
Assembly | Basic | C | C# | C++ | Delphi | Flash | Java | JavaScript | Pascal | Perl | PHP | Python | Ruby | Visual Basic
© Copyright 2011 Programmersheaven.com - All rights reserved.
Reproduction in whole or in part, in any form or medium without express written permission is prohibited.
Violators of this policy may be subject to legal action. Please read our Terms Of Use and Privacy Statement for more information.
Operated by CommunityHeaven, a BootstrapLabs company.