Click here to monitor SSC
SQLServerCentral is supported by Red Gate Software Ltd.
 
Log in  ::  Register  ::  Not logged in
 
 
 
        
Home       Members    Calendar    Who's On


Add to briefcase 12»»

SQL trigger Expand / Collapse
Author
Message
Posted Friday, November 22, 2013 9:47 AM
Forum Newbie

Forum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum Newbie

Group: General Forum Members
Last Login: Tuesday, February 11, 2014 12:03 PM
Points: 7, Visits: 22
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.
Post #1516865
Posted Friday, November 22, 2013 9:58 AM


SSCrazy

SSCrazySSCrazySSCrazySSCrazySSCrazySSCrazySSCrazySSCrazy

Group: General Forum Members
Last Login: Yesterday @ 5:48 PM
Points: 2,774, Visits: 5,949
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.
I am a great believer in luck, and I find the harder I work the more I have of it. Stephen Leacock

Forum Etiquette: How to post data/code on a forum to get the best help
Post #1516867
Posted Friday, November 22, 2013 11:37 AM
Forum Newbie

Forum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum Newbie

Group: General Forum Members
Last Login: Tuesday, February 11, 2014 12:03 PM
Points: 7, Visits: 22
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
Post #1516886
Posted Friday, November 22, 2013 11:42 AM


SSChampion

SSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampion

Group: General Forum Members
Last Login: Today @ 4:16 AM
Points: 12,749, Visits: 31,115
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

--There is no spoon, and there's no default ORDER BY in sql server either.
Actually, Common Sense is so rare, it should be considered a Superpower. --my son
Post #1516887
Posted Friday, November 22, 2013 11:53 AM
Forum Newbie

Forum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum Newbie

Group: General Forum Members
Last Login: Tuesday, February 11, 2014 12:03 PM
Points: 7, Visits: 22
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.
Post #1516893
Posted Friday, November 22, 2013 1:43 PM


Mr or Mrs. 500

Mr or Mrs. 500Mr or Mrs. 500Mr or Mrs. 500Mr or Mrs. 500Mr or Mrs. 500Mr or Mrs. 500Mr or Mrs. 500Mr or Mrs. 500

Group: General Forum Members
Last Login: Yesterday @ 3:43 PM
Points: 544, Visits: 806
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
Post #1516929
Posted Thursday, December 05, 2013 2:11 PM


SSC Veteran

SSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC Veteran

Group: General Forum Members
Last Login: Monday, April 21, 2014 9:08 AM
Points: 243, Visits: 788
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
Post #1520327
Posted Friday, December 06, 2013 6:58 AM
Forum Newbie

Forum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum Newbie

Group: General Forum Members
Last Login: Tuesday, February 11, 2014 12:03 PM
Points: 7, Visits: 22
thank you
Post #1520569
Posted Tuesday, December 17, 2013 11:49 AM
Forum Newbie

Forum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum Newbie

Group: General Forum Members
Last Login: Tuesday, February 11, 2014 12:03 PM
Points: 7, Visits: 22
.
Post #1523837
Posted Tuesday, December 17, 2013 11:58 AM


SSChampion

SSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampion

Group: General Forum Members
Last Login: Today @ 4:16 AM
Points: 12,749, Visits: 31,115
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

--There is no spoon, and there's no default ORDER BY in sql server either.
Actually, Common Sense is so rare, it should be considered a Superpower. --my son
Post #1523841
« Prev Topic | Next Topic »

Add to briefcase 12»»

Permissions Expand / Collapse