SQL Clone
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


SQL trigger


SQL trigger

Author
Message
wat3575
wat3575
Forum Newbie
Forum Newbie (7 reputation)Forum Newbie (7 reputation)Forum Newbie (7 reputation)Forum Newbie (7 reputation)Forum Newbie (7 reputation)Forum Newbie (7 reputation)Forum Newbie (7 reputation)Forum Newbie (7 reputation)

Group: General Forum Members
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.
Luis Cazares
Luis Cazares
SSC-Forever
SSC-Forever (43K reputation)SSC-Forever (43K reputation)SSC-Forever (43K reputation)SSC-Forever (43K reputation)SSC-Forever (43K reputation)SSC-Forever (43K reputation)SSC-Forever (43K reputation)SSC-Forever (43K reputation)

Group: General Forum Members
Points: 43346 Visits: 19859
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
wat3575
wat3575
Forum Newbie
Forum Newbie (7 reputation)Forum Newbie (7 reputation)Forum Newbie (7 reputation)Forum Newbie (7 reputation)Forum Newbie (7 reputation)Forum Newbie (7 reputation)Forum Newbie (7 reputation)Forum Newbie (7 reputation)

Group: General Forum Members
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
Lowell
Lowell
SSC Guru
SSC Guru (75K reputation)SSC Guru (75K reputation)SSC Guru (75K reputation)SSC Guru (75K reputation)SSC Guru (75K reputation)SSC Guru (75K reputation)SSC Guru (75K reputation)SSC Guru (75K reputation)

Group: General Forum Members
Points: 75247 Visits: 40985
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!
wat3575
wat3575
Forum Newbie
Forum Newbie (7 reputation)Forum Newbie (7 reputation)Forum Newbie (7 reputation)Forum Newbie (7 reputation)Forum Newbie (7 reputation)Forum Newbie (7 reputation)Forum Newbie (7 reputation)Forum Newbie (7 reputation)

Group: General Forum Members
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.
Keith Tate
Keith Tate
SSCrazy
SSCrazy (2.4K reputation)SSCrazy (2.4K reputation)SSCrazy (2.4K reputation)SSCrazy (2.4K reputation)SSCrazy (2.4K reputation)SSCrazy (2.4K reputation)SSCrazy (2.4K reputation)SSCrazy (2.4K reputation)

Group: General Forum Members
Points: 2400 Visits: 979
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
Sigerson
Sigerson
Ten Centuries
Ten Centuries (1.1K reputation)Ten Centuries (1.1K reputation)Ten Centuries (1.1K reputation)Ten Centuries (1.1K reputation)Ten Centuries (1.1K reputation)Ten Centuries (1.1K reputation)Ten Centuries (1.1K reputation)Ten Centuries (1.1K reputation)

Group: General Forum Members
Points: 1099 Visits: 1237
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
wat3575
wat3575
Forum Newbie
Forum Newbie (7 reputation)Forum Newbie (7 reputation)Forum Newbie (7 reputation)Forum Newbie (7 reputation)Forum Newbie (7 reputation)Forum Newbie (7 reputation)Forum Newbie (7 reputation)Forum Newbie (7 reputation)

Group: General Forum Members
Points: 7 Visits: 22
thank you
wat3575
wat3575
Forum Newbie
Forum Newbie (7 reputation)Forum Newbie (7 reputation)Forum Newbie (7 reputation)Forum Newbie (7 reputation)Forum Newbie (7 reputation)Forum Newbie (7 reputation)Forum Newbie (7 reputation)Forum Newbie (7 reputation)

Group: General Forum Members
Points: 7 Visits: 22
.
Lowell
Lowell
SSC Guru
SSC Guru (75K reputation)SSC Guru (75K reputation)SSC Guru (75K reputation)SSC Guru (75K reputation)SSC Guru (75K reputation)SSC Guru (75K reputation)SSC Guru (75K reputation)SSC Guru (75K reputation)

Group: General Forum Members
Points: 75247 Visits: 40985
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!
Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum

































































































































































SQLServerCentral


Search