[x]
Posted via EE Mobile

Search, ask, and monitor your questions on the go with EE Mobile. Visit Experts Exchange from your mobile device and never be out of touch again.

Question
[x]
Attachment Details
[x]
The Solution Rating System

With so many solutions, how can you tell which solutions are most likely to help you and which ones are not? To provide you with a tool to use, we rate our solutions based on various elements that most accurately determine if a solution is a quality solution. To explain what factors affect the solution rating, here are the elements we take into consideration when formulating our solution rating.

  • The Grade of the Solution
  • The Zone Rank of the Expert Providing the Solution
  • The Number of Author and Expert Comments
  • The Number of Experts Contributing
  • The Feedback of the Community

Your Input Matters
Because of the way the system is set up, the most important variable in this equation is you. As a member of Experts Exchange, you are able to cast your vote on the quality of the solutions in regard to how complete, accurate, helpful and easy to understand each solution is. When you provide your feedback, each rating is adjusted accordingly. So, if you see a solution that has a poor rating that you think is a good solution, let us know by rating it. As you do, the rating will be adjusted and will become more accurate for other members of our site.

If you have any suggestions that you would like to make for our rating system, please ask a question in the Suggestions Zone of Community Support.

Thank you!

9.9

Setting up my first SQL Server 2005 Update/Insert Trigger

Asked by agrarian in SQL Server 2005, MS SQL Server

Tags: Microsoft SQL Server 2005 Trigger

I am trying to create a trigger that will set the value of one field based on the values of two other fields. I first try to determine if I'm in an Insert or Update situation, then I make sure one of the two fields I'm interested in has actually changed, then I set calculate the third field's value.

I would appreciate feedback on whether I'm doing this the right way or not. I got most of the ideas from this forum. In addition, I'm currently getting errors "Incorrect syntax near 'TRY' in both locations.

My table structure (partial):
CREATE TABLE [dbo].[PairPalWatch](
      [WatchId] [int] IDENTITY(1,1) NOT NULL,
      [CheckFrequency] [smallint] NOT NULL DEFAULT (3),
      [DateLastChecked] [datetime] NULL,
      [DateNextCheck] [datetime] NULL)

And my trigger:
CREATE TRIGGER PairPalWatch_Update_trg
   ON  PairPalWatch
   AFTER UPDATE, INSERT
AS
BEGIN
      -- SET NOCOUNT ON added to prevent extra result sets from
      -- interfering with SELECT statements.
      SET NOCOUNT ON;

    -- Insert statements for trigger here
      declare @DateLastChecked datetime
      declare @CheckFrequency smallint
      declare @DateLastCheckedOld datetime
      declare @CheckFrequencyOld smallint
      declare @WatchId int

BEGIN TRY

      select  @WatchId = ins.WatchId,
                  @DateLastChecked = ins.DateLastChecked,
                  @CheckFrequency = ins.CheckFrequency
      from INSERTED ins

      IF EXISTS(SELECT WatchId FROM deleted)
            BEGIN
            select  @DateLastCheckedOld = del.DateLastChecked,
                        @CheckFrequencyOld = del.CheckFrequency
      from DELETED del

            IF ((@DateLastChecked <> @DateLastCheckedOld) or (@CheckFrequency <> @CheckFrequencyOld))
                  BEGIN
                  UPDATE PairPalWatch
                        SET DateNextCheck = CASE @CheckFrequency
                              WHEN 0 THEN DateAdd(dd , 1 , @DateLastChecked)
                              WHEN 1 THEN DateAdd(ww , 1 , @DateLastChecked)
                              WHEN 2 THEN DateAdd(ww , 2 , @DateLastChecked)
                              WHEN 3 THEN DateAdd(mm , 1 , @DateLastChecked)
                              ELSE DateAdd(mm , 1 , DateLastChecked) END
                        WHERE WatchId = @WatchId
                  END
            END
      ELSE
            BEGIN
            UPDATE PairPalWatch
                  SET DateNextCheck = CASE @CheckFrequency
                        WHEN 0 THEN DateAdd(dd , 1 , @DateLastChecked)
                        WHEN 1 THEN DateAdd(ww , 1 , @DateLastChecked)
                        WHEN 2 THEN DateAdd(ww , 2 , @DateLastChecked)
                        WHEN 3 THEN DateAdd(mm , 1 , @DateLastChecked)
                        ELSE DateAdd(mm , 1 , DateLastChecked) END
                  WHERE WatchId = @WatchId
            END
END TRY
BEGIN CATCH
    PRINT 'ERROR'
END CATCH  
END
GO

CheckFrequency translates to:
    0    Daily
    1    Weekly
    2    BiMonthly (every two weeks)
    3    Monthly

ADDED: When I comment out the BEGIN TRY, END TRY and CATCH lines, the Trigger works fine.
[+][-]11/06/09 03:24 AM, ID: 25758194Expert Comment

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 30-day free trial to view this Expert Comment or ask the Experts your question.

 
[+][-]11/06/09 03:24 AM, ID: 25758195Expert Comment

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 30-day free trial to view this Expert Comment or ask the Experts your question.

 
[+][-]11/06/09 07:48 AM, ID: 25760099Author Comment

Often, when Experts are collaborating with members who have asked questions, they will request additional information about the problem. Askers respond with an author comment like this one.

Start your 30-day free trial to view this Author Comment or ask the Experts your question.

 
[+][-]11/06/09 10:25 AM, ID: 25761638Accepted Solution

View this solution now by starting your 30-day free trial. Setting up your free trial is quick, easy, and secure. We will return you to this solution, unlocked, when you're done.

About this solution

Zones: SQL Server 2005, MS SQL Server
Tags: Microsoft SQL Server 2005 Trigger
Sign Up Now!
Solution Provided By: acperkins
Participating Experts: 2
Solution Grade: A
 
[+][-]11/06/09 12:12 PM, ID: 25762635Expert Comment

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 30-day free trial to view this Expert Comment or ask the Experts your question.

 
[+][-]11/06/09 12:18 PM, ID: 25762685Expert Comment

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 30-day free trial to view this Expert Comment or ask the Experts your question.

 
[+][-]11/06/09 12:33 PM, ID: 25762792Author Comment

Often, when Experts are collaborating with members who have asked questions, they will request additional information about the problem. Askers respond with an author comment like this one.

Start your 30-day free trial to view this Author Comment or ask the Experts your question.

 
[+][-]11/06/09 12:47 PM, ID: 25762900Expert Comment

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 30-day free trial to view this Expert Comment or ask the Experts your question.

 
 
Loading Advertisement...
20091021-EE-VQP-81 - Hierarchy / EE_QW_3_20080625