Solved

Need help sql 2008 trigger

Posted on 2014-07-22
7
172 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 3
7 Comments
 
LVL 143

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 143

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
Business Impact of IT Communications

What are the business impacts of how well businesses communicate during an IT incident? Targeting, speed, and transparency all matter. Find out more in this infographic.

 

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 143

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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Why is this different from all of the other step by step guides?  Because I make a living as a DBA and not as a writer and I lived through this experience. Defining the name: When I talk to people they say different names on this subject stuff l…
Recently we ran in to an issue while running some SQL jobs where we were trying to process the cubes.  We got an error saying failure stating 'NT SERVICE\SQLSERVERAGENT does not have access to Analysis Services. So this is a way to automate that wit…
Via a live example, show how to extract information from SQL Server on Database, Connection and Server properties
Viewers will learn how to use the SELECT statement in SQL and will be exposed to the many uses the SELECT statement has.

739 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