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?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Ryan ChongBusiness Systems Analyst , ex-Senior Application EngineerCommented:
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
jspcAuthor Commented:
Oh, can that trigger run on INSERT and UPDATE?
If a Sales Order is amended the Trigger can fire?
0
10 Tips to Protect Your Business from Ransomware

Did you know that ransomware is the most widespread, destructive malware in the world today? It accounts for 39% of all security breaches, with ransomware gangsters projected to make $11.5B in profits from online extortion by 2019.

Ryan ChongBusiness Systems Analyst , ex-Senior Application EngineerCommented:
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
Ryan ChongBusiness Systems Analyst , ex-Senior Application EngineerCommented:
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

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
jspcAuthor Commented:
Awesome!
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Query Syntax

From novice to tech pro — start learning today.