Solved

Need help sql 2008 trigger

Posted on 2014-07-22
7
169 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 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
Best Practices: Disaster Recovery Testing

Besides backup, any IT division should have a disaster recovery plan. You will find a few tips below relating to the development of such a plan and to what issues one should pay special attention in the course of backup planning.

 

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

What is SQL Server and how does it work?

The purpose of this paper is to provide you background on SQL Server. It’s your self-study guide for learning fundamentals. It includes both the history of SQL and its technical basics. Concepts and definitions will form the solid foundation of your future DBA expertise.

Question has a verified solution.

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

Everyone has problem when going to load data into Data warehouse (EDW). They all need to confirm that data quality is good but they don't no how to proceed. Microsoft has provided new task within SSIS 2008 called "Data Profiler Task". It solve th…
JSON is being used more and more, besides XML, and you surely wanted to parse the data out into SQL instead of doing it in some Javascript. The below function in SQL Server can do the job for you, returning a quick table with the parsed data.
Via a live example combined with referencing Books Online, show some of the information that can be extracted from the Catalog Views in SQL Server.
Via a live example, show how to extract insert data into a SQL Server database table using the Import/Export option and Bulk Insert.

792 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