Solved

Need help sql 2008 trigger

Posted on 2014-07-22
7
164 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
What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

 

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

Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

Join & Write a Comment

Performance is the key factor for any successful data integration project, knowing the type of transformation that you’re using is the first step on optimizing the SSIS flow performance, by utilizing the correct transformation or the design alternat…
Nowadays, some of developer are too much worried about data. Who is using data, who is updating it etc. etc. Because, data is more costlier in term of money and information. So security of data is focusing concern in days. Lets' understand the Au…
Via a live example, show how to setup several different housekeeping processes for a SQL Server.
Using examples as well as descriptions, and references to Books Online, show the documentation available for datatypes, explain the available data types and show how data can be passed into and out of variables.

708 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

19 Experts available now in Live!

Get 1:1 Help Now