Solved

Need help sql 2008 trigger

Posted on 2014-07-22
7
168 Views
Last Modified: 2014-07-23
Trigger.xlsxHello im not really familiar with triggers in sql 2008

Here is what ive been ask if i could do it

i need to create a trigger on insert and update

When the field "NOM_FRANCAIS" from table "ARBRES" change i need to update 4 others fields ("NOM_LATIN" + "TYPE" + " HAUTEUR_MAT" + "LARGEUR_MAT"

I have 85 differents case (look at my excel file)

Can someone please help me with this triggers

Thanks alot !!
0
Comment
Question by:jfguenet
  • 4
  • 3
7 Comments
 
LVL 142

Expert Comment

by:Guy Hengel [angelIII / a3]
ID: 40212770
if you are not familiar with triggers, you should first get familiar with them before trying to use them..

technical reference (which includes some examples on the bottom of the page):
http://msdn.microsoft.com/en-us/library/ms189799.aspx

after that, you would need to attach the excel file or better explain what the "issues" are...
0
 

Author Comment

by:jfguenet
ID: 40212782
Ok thanks ill check that

The excel file is attached...i see it in my question at the top

Trigger.xlsx
0
 
LVL 142

Accepted Solution

by:
Guy Hengel [angelIII / a3] earned 500 total points
ID: 40212891
I would not use that with "cases", but fill that excel data into a table, and use that in the trigger code for lookup and the update.
again: you should be able to use the basic trigger samples from the tech link above, combine with this article:
http://www.experts-exchange.com/Database/Miscellaneous/A_1517-UPDATES-with-JOIN-for-everybody.html

in short (I don't have a sql box now availble to test any code):
UPDATE T
  set t."NOM_LATIN" =  isnull(l."NOM_LATIN", t."NOM_LATIN")
  , t."TYPE" =  isnull(l."TYPE", t."TYPE")
  , t."HAUTEUR_MAT" =  isnull(l."HAUTEUR_MAT", t."HAUTEUR_MAT")
  , t."LARGEUR_MAT" =  isnull(l."LARGEUR_MAT", t."LARGEUR_MAT")
FROM INSERTED i
JOIN ARBRES t
   ON t.key_field = i.keyfield
LEFT JOIN excel_data_table l
   ON l.nom_francais = t.nom_francais

Open in new window


just fill in you primary key field of the table ARBRES, and put that inside of the normal trigger basic code, and you should be done
0
VMware Disaster Recovery and Data Protection

In this expert guide, you’ll learn about the components of a Modern Data Center. You will use cases for the value-added capabilities of Veeam®, including combining backup and replication for VMware disaster recovery and using replication for data center migration.

 

Author Comment

by:jfguenet
ID: 40214371
Hi thanks for your help

Here is what i did

I imported my excel file in sql management to create a new table called "ARBRES_SPEC" (see my attached file for the design of the 2 tables "ARBRES" and "ARBRES_SPECS"

Now here is the code

SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE TRIGGER dbo.UPDATE_TRIGGER_NOM_FRANCAIS
   ON  dbo.ARBRES
   AFTER INSERT,UPDATE
AS
BEGIN
      -- SET NOCOUNT ON added to prevent extra result sets from
      -- interfering with SELECT statements.
      SET NOCOUNT ON;

    -- Insert statements for trigger here
      IF (UPDATE(NOM_FRANCAIS))
      BEGIN
            UPDATE T
                  SET t."NOM_LATIN" =  isnull(l."NOM_LATIN", t."NOM_LATIN")
                        , t."TYPE" =  isnull(l."TYPE", t."TYPE")
                        , t."HAUTEUR_MAT" =  isnull(l."HAUTEUR_MAT", t."HAUTEUR_MAT")
                        , t."LARGEUR_MAT" =  isnull(l."LARGEUR_MAT", t."LARGEUR_MAT")
                  FROM INSERTED i
                  JOIN ARBRES t
                  ON t.JMAP_ID = i.keyfield
                  LEFT JOIN ARBRES_SPECS l
                  ON l.nom_francais = t.nom_francais
      END
END
GO


For this line

ON t.keyfield = i.keyfield

I need to put like this ?

ON t.JMAP_ID = i.NOM_FRANCAIS

Thanks alot you really helping me :)
ARBRES-DESIGN.png
ARBRES-SPEC-DESIGN.png
0
 

Author Comment

by:jfguenet
ID: 40214376
Ha i think i understand

IVE put

ON t.JMAP_ID = i.JMAP_ID
0
 
LVL 142

Expert Comment

by:Guy Hengel [angelIII / a3]
ID: 40214467
Yes that looks like it should work.
0
 

Author Closing Comment

by:jfguenet
ID: 40215027
Thanks it was a pleasure working with you.

You gave me really good explannation

Thanks alot !
0

Featured Post

Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

Question has a verified solution.

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

I wrote this interesting script that really help me find jobs or procedures when working in a huge environment. I could I have written it as a Procedure but then I would have to have it on each machine or have a link to a server-related search that …
Load balancing is the method of dividing the total amount of work performed by one computer between two or more computers. Its aim is to get more work done in the same amount of time, ensuring that all the users get served faster.
This video shows, step by step, how to configure Oracle Heterogeneous Services via the Generic Gateway Agent in order to make a connection from an Oracle session and access a remote SQL Server database table.
Via a live example, show how to extract information from SQL Server on Database, Connection and Server properties

776 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