November 22, 2013 at 9:47 am
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.
November 22, 2013 at 9:58 am
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.
November 22, 2013 at 11:37 am
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
November 22, 2013 at 11:42 am
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
November 22, 2013 at 11:53 am
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.
November 22, 2013 at 1:43 pm
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?
December 5, 2013 at 2:11 pm
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
December 6, 2013 at 6:58 am
thank you
December 17, 2013 at 11:49 am
.
December 17, 2013 at 11:58 am
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
December 17, 2013 at 2:04 pm
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
Change is inevitable... Change for the better is not.
Viewing 11 posts - 1 through 10 (of 10 total)
You must be logged in to reply to this topic. Login to reply