?
Solved

trigger sql server

Posted on 2009-04-29
2
Medium Priority
?
169 Views
Last Modified: 2012-05-06
Hi,
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!

Raf
0
Comment
Question by:ralph_rea
2 Comments
 

Author Comment

by:ralph_rea
ID: 24259892
sorry.....
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)
0
 
LVL 6

Accepted Solution

by:
openshac earned 2000 total points
ID: 24259908
Try this:

CREATE TRIGGER [afm].myTrigger
   ON  [afm].wo
   AFTER INSERT,UPDATE
AS 
	SET NOCOUNT ON;
 
	UPDATE wr
	SET ac_id = i.ac_id
	FROM wr mywr
	INNER JOIN 
		Inserted i ON mywr.wo_id = i.wo_id
	AND mywr.wo_id IS NOT NULL

Open in new window

0

Featured Post

[Webinar] Cloud and Mobile-First Strategy

Maybe you’ve fully adopted the cloud since the beginning. Or maybe you started with on-prem resources but are pursuing a “cloud and mobile first” strategy. Getting to that end state has its challenges. Discover how to build out a 100% cloud and mobile IT strategy in this webinar.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In this article I will describe the Copy Database Wizard method as one possible migration process and I will add the extra tasks needed for an upgrade when and where is applied so it will cover all.
In this article we will get to know that how can we recover deleted data if it happens accidently. We really can recover deleted rows if we know the time when data is deleted by using the transaction log.
Despite its rising prevalence in the business world, "the cloud" is still misunderstood. Some companies still believe common misconceptions about lack of security in cloud solutions and many misuses of cloud storage options still occur every day. …
With just a little bit of  SQL and VBA, many doors open to cool things like synchronize a list box to display data relevant to other information on a form.  If you have never written code or looked at an SQL statement before, no problem! ...  give i…

862 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