trigger sql server

Posted on 2009-04-29
Last Modified: 2012-05-06
I've these tables:

CREATE TABLE [afm].[wr](
      [ac_id] [char](32) NULL DEFAULT (null),
      [wo_id] [int] NULL DEFAULT (null),
      [wr_id] [int] IDENTITY(1,1) NOT NULL)
CREATE TABLE [afm].[wo](
      [ac_id] [char](32) NULL DEFAULT (null),
      [wo_id] [int] IDENTITY(1,1) NOT NULL)

I'd like to create a trigger that fire when:

- update or insert into wr

- wr.wo_id is not null and wr.wo_id=wo.wo_id

this trigger have to update wo.ac_id with wr.ac_id values

How can I write this trigger?

Thanks in advance!

Question by:ralph_rea

    Author Comment

    the steps are:
    insert into wr (trigger not file because wo_id is null)
    update wr.wo_id with one value (here trigger must fire and update wo.ac_id with wr.ac_id values)
    LVL 6

    Accepted Solution

    Try this:

    CREATE TRIGGER [afm].myTrigger
       ON  [afm].wo
    	UPDATE wr
    	SET ac_id = i.ac_id
    	FROM wr mywr
    		Inserted i ON mywr.wo_id = i.wo_id
    	AND mywr.wo_id IS NOT NULL

    Open in new window


    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    How your wiki can always stay up-to-date

    Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
    - Increase transparency
    - Onboard new hires faster
    - Access from mobile/offline

    Suggested Solutions

    Title # Comments Views Activity
    spx for moving values to new table 5 45
    Divide by Zero Error in SQL 7 25
    SQL PIVOT Table 21 30
    SQL Select - AVG 3 36
    After restoring a Microsoft SQL Server database (.bak) from backup or attaching .mdf file, you may run into "Error '15023' User or role already exists in the current database" when you use the "User Mapping" SQL Management Studio functionality to al…
    Use this article to create a batch file to backup a Microsoft SQL Server database to a Windows folder.  The folder can be on the local hard drive or on a network share.  This batch file will query the SQL server to get the current date & time and wi…
    To add imagery to an HTML email signature, you have two options available to you. You can either add a logo/image by embedding it directly into the signature or hosting it externally and linking to it. The vast majority of email clients display l…
    Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…

    761 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    13 Experts available now in Live!

    Get 1:1 Help Now