SQL trigger

  • We are using a CRM system and I have had trouble creating a trigger that would automatically update another field on a record. The first table I am working with is Sales that has a key3 field. Within this field we insert Sales Rep names from a user table. Within this users table we have another column with the users ID. So what I would like to do is create a trigger that would recognize when a Sales Rep name is added to this key3 field and then autoupdate another field with the associated USERID. Thank you for the help.

  • Could you post DDL, sample data in the form of INSERT INTO statements and expected results based on that sample data.

    I'm not sure if you have the best design idea according to the relational theory.

    Luis C.
    General Disclaimer:
    Are you seriously taking the advice and code from someone from the internet without testing it? Do you at least understand it? Or can it easily kill your server?

    How to post data/code on a forum to get the best help: Option 1 / Option 2
  • I cant post the DDL sorry, but this is the structure of the tables that I am look at.

    Sales(table) has Column key3

    Users(table) has Columns UserID,UserName

    Department(table) has column Account

    When someone enters a UserName for key3. I then want a trigger to be able to pick up that associated UserID and put this into Account.

    So lets say a row in Users table had UserID: 87 UserName: Robin M

    When someone enters Robin M into the key3 field

    I would like her UserID inserted into the Account field.

    Thank you very much for the help

  • my best guess, which sucks and makes no sense woithout the sample DDL of the tables, and the trigger you were trying to build:

    CREATE TRIGGER TR_Sales_CloneToDepartment On Sales

    FOR INSERT

    AS

    INSERT INTO Department(Account)

    SELECT Users.UserID

    FROM Users

    INNER JOIN INSERTED

    ON Users.UserName= INSERTED.key3

    Lowell


    --help us help you! If you post a question, make sure you include a CREATE TABLE... statement and INSERT INTO... statement into that table to give the volunteers here representative data. with your description of the problem, we can provide a tested, verifiable solution to your question! asking the question the right way gets you a tested answer the fastest way possible!

  • Sorry I am really new to triggers. Now when in SQL Server Managment Studio and when attaching a trigger like this. What table would you attach the trigger too? You are basing the trigger on a field that is in Sales. Using data from table Users and updating a field that is in table Department.

  • Have you tired creating a trigger and it isn't working or are you looking for someone to provide you a complete trigger? These forums are usually to help people with problems they are having and not providing complete works.

    First off do you use stored procedures to insert the data into the Users table? If so, can you add your logic there instead of adding a trigger? I would recommend this approach to using a trigger if you can help it.

    If it has to be done using a trigger (you will know this better than I) what are your specific questions about creating the trigger?



    Microsoft Certified Master - SQL Server 2008
    Follow me on twitter: @keith_tate

    Forum Etiquette: How to post data/code on a forum to get the best help[/url]

  • wat3575,

    When you're in SSMS, click View --> Template Explorer. Go to the Triggers section to see 4 different example templates for building a new trigger.

    Lots of good education in the Templates Explorer, enough to get you started to where you can ask a more specific question.

    Also, check Help. Click the search tab and enter 'CREATE TRIGGER'

    Hth,

    Sigerson

    "No pressure, no diamonds." - Thomas Carlyle

  • thank you

  • .

  • i'd say it's a bad trigger.

    it does not handle multiple rows at all.

    there may be a logic hole here as well: it updates the entire contact2 table for any row with the same account number.

    a trigger should be updating based on a join from the inserted table.

    I'd think it needs to look like tis:

    CREATE TRIGGER [Db_Officer_Filler]

    ON dbo.CONTACT1

    FOR UPDATE, INSERT

    AS

    UPDATE MyTarget

    SET MyTarget.uacnt = users.name

    FROM contact2

    INNER JOIN INSERTED

    ON MyTarget.accountno = INSERTED.Accountno

    INNER JOIN users

    ON users.name = inserted.u_key3

    WHERE ISNULL(inserted.u_key3,'') <> ''

    Lowell


    --help us help you! If you post a question, make sure you include a CREATE TABLE... statement and INSERT INTO... statement into that table to give the volunteers here representative data. with your description of the problem, we can provide a tested, verifiable solution to your question! asking the question the right way gets you a tested answer the fastest way possible!

  • wat3575 (11/22/2013)


    We are using a CRM system and I have had trouble creating a trigger that would automatically update another field on a record. The first table I am working with is Sales that has a key3 field. Within this field we insert Sales Rep names from a user table. Within this users table we have another column with the users ID. So what I would like to do is create a trigger that would recognize when a Sales Rep name is added to this key3 field and then autoupdate another field with the associated USERID. Thank you for the help.

    I'm going to recommend NOT using a trigger to do this.

    I'm also going to recommend NOT using a stored procedure to do this.

    In fact, I'm going to recommend NOT doing this at all.

    It's a form of denormalization AND unnecessary duplication of data. Further, if there's ever a name change for the given ID, you'll need a separate one-off process to change it. Even if that never happens, it's still an unnecessary duplication of data and should be avoided because of all of the rules of normalization.

    --Jeff Moden


    RBAR is pronounced "ree-bar" and is a "Modenism" for Row-By-Agonizing-Row.
    First step towards the paradigm shift of writing Set Based code:
    ________Stop thinking about what you want to do to a ROW... think, instead, of what you want to do to a COLUMN.

    Change is inevitable... Change for the better is not.


    Helpful Links:
    How to post code problems
    How to Post Performance Problems
    Create a Tally Function (fnTally)

Viewing 11 posts - 1 through 10 (of 10 total)

You must be logged in to reply to this topic. Login to reply