SQL Trigger

Hello, could someone help me, please?
I am wanting to create a SQL Trigger for the following.

When a new sales order is created in the TRANSHEADERS Table where AccountCode = ‘Customer’
I want the following script to trigger.

Update Customers
Set Customers.ZCustref = Transheaders.ExtraText
FROM CUSTOMERS INNER JOIN
                      TRANSHEADERS ON CUSTOMERS.UniqueID = TRANSHEADERS.AccountID
Where Customers.CustomerCode = 'Customer'

I have tested the script and it works as I want.

Thank you.
jspcAsked:
Who is Participating?
 
Ryan ChongConnect With a Mentor Commented:
I have just realized it needs to equal ‘TransactionType SO (For sales orders only, not invoice transactions).
Is there a way I can incorporate that too?
of course you can... do you think can inner join that table in your update statement?

I have also noticed that if you create another sales order (so now I have 2) that the field is not updated with the most recent data.
because there was an issue within your update statement. should try something like this instead (using old example):

ALTER TRIGGER TRANSHEADERS_Insert  
ON Transheaders  
FOR INSERT, UPDATE AS  
BEGIN  
    Update Customers
	Set Customers.ZCustref = Transheaders.ExtraText
	FROM CUSTOMERS INNER JOIN
	(
		select *, row_number() over (partition by AccountID order by id /* or transaction date, etc*/ desc) idx from TRANSHEADERS
	) TRANSHEADERS ON CUSTOMERS.UniqueID = TRANSHEADERS.AccountID
	Where TRANSHEADERS.idx = 1 and Customers.CustomerCode = 'Customer' and Customers.ZCustref <> Transheaders.ExtraText

END 

Open in new window

0
 
Ryan ChongCommented:
in general, try this as you claimed that your Update statement is working fine:

CREATE TRIGGER TRANSHEADERS_Insert  
ON Transheaders  
FOR INSERT AS  
BEGIN  
    Update Customers
	Set Customers.ZCustref = Transheaders.ExtraText
	FROM CUSTOMERS INNER JOIN
						  TRANSHEADERS ON CUSTOMERS.UniqueID = TRANSHEADERS.AccountID
	Where Customers.CustomerCode = 'Customer' 

END 

Open in new window

0
 
jspcAuthor Commented:
Yes great, that does work, thank you!

You would know why the data is half concatenating would you?
The data on the sales order is this:
Jeremy
44 Smith Street
Drouin

After the Trigger runs it appears in the custom field as this: Jeremy44 Smith StreetDrouin

I have had a look at the data field and it appears to be putting a double space between the y and 4 and the t and D
0
The 14th Annual Expert Award Winners

The results are in! Meet the top members of our 2017 Expert Awards. Congratulations to all who qualified!

 
jspcAuthor Commented:
Oh, can that trigger run on INSERT and UPDATE?
If a Sales Order is amended the Trigger can fire?
0
 
Ryan ChongCommented:
Oh, can that trigger run on INSERT and UPDATE?
If a Sales Order is amended the Trigger can fire?
sure you can.

ALTER TRIGGER TRANSHEADERS_Insert  
ON Transheaders  
FOR INSERT , UPDATE AS  
BEGIN  
    Update Customers
      Set Customers.ZCustref = Transheaders.ExtraText
      FROM CUSTOMERS INNER JOIN
                                      TRANSHEADERS ON CUSTOMERS.UniqueID = TRANSHEADERS.AccountID
      Where Customers.CustomerCode = 'Customer'

END
0
 
jspcAuthor Commented:
Thanks for your help.

Sorry, I have just realized it needs to equal ‘TransactionType SO (For sales orders only, not invoice transactions).
Is there a way I can incorporate that too?

I have also noticed that if you create another sales order (so now I have 2) that the field is not updated with the most recent data.
0
 
jspcAuthor Commented:
Awesome!
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.