Wrox Programmer Forums
Go Back   Wrox Programmer Forums > SQL Server > SQL Server 2000 > SQL Server 2000
|
SQL Server 2000 General discussion of Microsoft SQL Server -- for topics that don't fit in one of the more specific SQL Server forums. version 2000 only. There's a new forum for SQL Server 2005.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the SQL Server 2000 section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old June 27th, 2005, 09:57 AM
Friend of Wrox
 
Join Date: Mar 2005
Posts: 264
Thanks: 0
Thanked 0 Times in 0 Posts
Default How to create a trigger for this constraint.

Hi every body.Could any sql sever expert show me how i can create a sql server 2000 trigger for this constraaint as this is my first time trying to create a trigger. I got many tables in my sql server. I want the trigger fires when a user tries to enter hours for an employee in hours worked table if he did not enter amount for same employee and same project in hourly wages table.(in another word amount should be enterd before hours)I want the trigger sends a massage and tells the user to enter amount before entering hours.Thanks.

http://i5.photobucket.com/albums/y18...nstraint.jpg

=====> table reletionships pic
 
Old June 27th, 2005, 09:35 PM
SQLScott's Avatar
Wrox Author
 
Join Date: Dec 2004
Posts: 338
Thanks: 0
Thanked 2 Times in 2 Posts
Default

You will want to do something like this:

If (SELECT hw.amount FROM [hourly wages] hw, [Hours Worked] hrw WHERE hw.employee = hrw.employee AND Employee = 10) = 0
BEGIN
        Select @ErrNo = 30001,
               @ErrMsg = 'Insert Denied! The corresponding Hourly Wages has not been entered.'

    RaisError @ErrNo @ErrMsg
    Rollback Transaction
end


 
Old June 28th, 2005, 08:19 AM
Friend of Wrox
 
Join Date: Mar 2005
Posts: 264
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Many thanks to u reply. how to define this trigger ? should i
just paste it in query analyzer ?could u be a little more specific ?Furthermore,could u tell me how i can trap the tirigger error in the client site which is access 2000 in my case and display the erro massage. Thanks
 
Old June 28th, 2005, 06:21 PM
SQLScott's Avatar
Wrox Author
 
Join Date: Dec 2004
Posts: 338
Thanks: 0
Thanked 2 Times in 2 Posts
Default

In Enterprise Manager, right mouse click on the table in which you want to create the trigger (hours worked, i think) and from the context menu select All Tasks -> Manage Triggers. In the Trigger Properties windows, replace the [TRIGGER NAME] with an actual name for the trigger, such as CheckHourlyWages, and then on the FOR line, delete the UPDATE and DELETE words. Then below the AS, paste my code above.

Now, for the client question, what are you using to develop the client application?

 
Old June 29th, 2005, 04:13 AM
Friend of Wrox
 
Join Date: Mar 2005
Posts: 264
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Quote:
quote:Originally posted by SQLScott
 In Enterprise Manager, right mouse click on the table in which you want to create the trigger (hours worked, i think) and from the context menu select All Tasks -> Manage Triggers. In the Trigger Properties windows, replace the [TRIGGER NAME] with an actual name for the trigger, such as CheckHourlyWages, and then on the FOR line, delete the UPDATE and DELETE words. Then below the AS, paste my code above.

Now, for the client question, what are you using to develop the client application?

Many Many thanks for your helpfull reply. Well i followed what u told me. When i click on check syntax in trigger properties after i pasted
the code i get the following error:


Error 137: Must declare the variable @ErrNo.Must Declare the variable @erNo . I be happy if u help me fix this error. I use MS access 2000 forms for the client site and i want to trap the error that get generated by the trigger and prompt the user with the massage that he must first enter amount in the hourly wages table. Thanks
 
Old June 29th, 2005, 06:17 PM
SQLScott's Avatar
Wrox Author
 
Join Date: Dec 2004
Posts: 338
Thanks: 0
Thanked 2 Times in 2 Posts
Default

I am sorry, i forgot that piece of code. YOu need to declare all variables, so just before the

If (SELECT hw.amount FROM [hourly wages] hw, [Hours Worked] hrw WHERE hw.employee = hrw.employee AND Employee = 10) = 0
BEGIN
    Declare @Errno int,
             @ErrMsg varchar(255)
    Select @ErrNo = 30001,
           @ErrMsg = 'Insert Denied! The corresponding Hourly Wages has not been entered.'

    RaisError @ErrNo @ErrMsg
    Rollback Transaction
end

OK, now for the client, since you are using MS Access 2000, are you using a form to enter data? What does the code you are using to add teh record look like? Or, is it a bound form?

 
Old June 30th, 2005, 02:57 AM
Friend of Wrox
 
Join Date: Mar 2005
Posts: 264
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Many thanks for u reply. I tried to run it again this time i get this error :

Error 209: Ambiguous column name Émployee. It is very strange!Furthermore, u used employee 10 in the select statement of trigger. Is there a way to change that so it holds the current employee number that we want to enter hours worked for?
Well for entering data i am using bounded forms so i do not now how i can trap the error casued by the triggers once it workes.I be happy if u help me fix these erros and issues.Thanks





Similar Threads
Thread Thread Starter Forum Replies Last Post
Create trigger on update bvpsekhar MySQL 23 May 4th, 2007 01:01 AM
need help w/create trigger by an insert operation jnt SQL Server 2000 2 December 14th, 2006 07:31 PM
create trigger on a view?? timbal25 Oracle 1 July 26th, 2005 08:14 PM
How to create trigger in StoreProcedure shailu SQL Server 2000 5 December 22nd, 2004 12:23 AM
Create Insert Trigger byron SQL Server 2000 6 November 19th, 2003 11:30 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.