Results 1 to 6 of 6
  1. #1
    Join Date
    Apr 2009
    Posts
    20

    Red face Unanswered: Triggers!! Need help!

    I am using Microsoft SQL Server 2008 and I am a total triggers disaster!

    Ok so now I have a Applicant table. The applicant table has the following fields :

    Applicant_Name, DOB, Marital_Status, Race,
    Religion, Employment_Status, Employer_Name,
    Gross_Monthly_Income, Contact_No,
    Handphone_No, Email_Address,
    Highest_Educational_Level, Date_of_Application

    I need to write a trigger which will do the following.

    When the Employment_Status = Employed and when Gross_Monthly_Income = "4000"

    I need to update the field Status_Of_Application to "Approved" in another table Registration when the above condition is met. The fields in Registration table as follows,

    Registration_ID, Date_of_Application,
    Renovation_LoanRequested,
    Status_of_Application,Flat_ID

    How do I do it?

    Last but not least, when the Status_Of_Application in the registration table is "approved" i want to auto create a table called House_Owner and insert fields onto it.

    Any idea how the above can be achieved using triggers in MS SQL Server 2008? I am clueless and any help will be useful. thanks.


  2. #2
    Join Date
    Jan 2007
    Location
    UK
    Posts
    11,434
    Provided Answers: 10
    Triggers do not create objects. If you are creating objects on the fly, your design is flawed.

    As for your trigger:
    Code:
    CREATE TRIGGER dbo.trigger_name
      ON dbo.applicant
      AFTER INSERT, UPDATE
    AS
      BEGIN
        UPDATE dbo.registration
        SET    status_of_application = 'Approved'
        FROM   dbo.registration As r
         INNER
          JOIN inserted As i
            ON r.common_field = i.common_field
        WHERE  i.employment_status = 'Employed'
        AND    i.gross_monthly_income = 4000
      END
    GO
    George
    Home | Blog

  3. #3
    Join Date
    Apr 2009
    Posts
    20
    Hi

    thanks a lot of helping me out. But for some reason I am not able to update the status in the registration table to Approved using the trigger. Or am I doing something wrong here. I am supposed to save the trigger under applicant table, execute it and close it then go to registration table, refresh it and am supposed to see the updated status rite?

    But when I just do a simple update statement, the status is changed. What could I be doing wrongly here? Any help? thanks...

  4. #4
    Join Date
    Apr 2009
    Posts
    20
    ok i just realised that it works after update, and not for insert.. hmmm
    how do i make it also work for insert?

  5. #5
    Join Date
    Jan 2007
    Location
    UK
    Posts
    11,434
    Provided Answers: 10
    Quote Originally Posted by gvee
    Code:
    CREATE TRIGGER dbo.trigger_name
      ON dbo.applicant
      AFTER INSERT, UPDATE
    ? ?
    George
    Home | Blog

  6. #6
    Join Date
    Apr 2009
    Posts
    20
    Hi dats what i did.. but somehow its not working..
    anyway thanks!

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •