Link to home
Create AccountLog in
Microsoft SQL Server

Microsoft SQL Server

--

Questions

--

Followers

Top Experts

Avatar of sleiman
sleiman🇺🇸

SQL Trigger to update column with a value from another table
Hi experts! I need to have an Insert/update trigger that can upate a column in a table2 that is based on a value of a column from another table1.

Example:
Table1
CustID, Account#, name, etc.
Table2
CustID, Account#, Order, etc.

When a new record is inserted into table2, I need to get the CustID value in Table1 for the Account# that matches the Account# from the Inserted record.

Zero AI Policy

We believe in human intelligence. Our moderation policy strictly prohibits the use of LLM content in our Q&A threads.


Avatar of EyalEyal🇮🇱

alter TRIGGER trgTable2 
   ON  Table2
   AFTER INSERT,UPDATE
AS 
BEGIN
	-- SET NOCOUNT ON added to prevent extra result sets from
	-- interfering with SELECT statements.
	SET NOCOUNT ON;

	declare @CustID int = 0
    select @CustID =CustID from inserted

-- do whatever you want with @CustID

GO

Open in new window


ASKER CERTIFIED SOLUTION
Avatar of DerZaubererDerZauberer

Link to home
membership
Log in or create a free account to see answer.
Signing up is free and takes 30 seconds. No credit card required.
Create Account

Avatar of sleimansleiman🇺🇸

ASKER

Hi DerZauberer, it looks like solution cout work. Is there a way to use a nested select to only return one record value?

So, on each Insert, I need to get a unique Account# (single record) from Table1.

Table1 and Table2 have one to many relationship. So, table2 can have multiple records to be update.

Hope that makes sense.

 

This trigger will work even if you have inserted more than one row into Table2.
I don't know if you have a unique id on Table2 so i can exactly identify the inserted records.
That's why i used Account# and Order for the join on the "inserted" table to keep the number of affected rows as small as possible.

Reward 1Reward 2Reward 3Reward 4Reward 5Reward 6

EARN REWARDS FOR ASKING, ANSWERING, AND MORE.

Earn free swag for participating on the platform.


Avatar of sleimansleiman🇺🇸

ASKER

I do have a recid that is unique for each record in table2.
I tried using recid instead of [order] (which I used as an example) and still not working.
Here is what I am using:

UPDATE Table2 SET Table2.CustID = Table1.CustID
      FROM Table2
      INNER JOIN INSERTED
      ON INSERTED.Account# = Table2.Account# AND INSERTED.RecID = Table2.RecID
      INNER JOIN Table1
      ON Table1.Account# = Table2.Account#

this is the error that I am getting.
The data in row 1 was not committed.
Error Source: .Net SqiClient Data Provider.
Error Message: Maximum stored procedure, Function, trigger, or
view nesting level exceeded (limit 32).


SOLUTION
Avatar of Guy Hengel [angelIII / a3]Guy Hengel [angelIII / a3]🇱🇺

Link to home
membership
Log in or create a free account to see answer.
Signing up is free and takes 30 seconds. No credit card required.

Hmm, that's strange, i thought 'IF UPDATE(Account#)' should prevent recursive execution ...
Do you already have other triggers on the table that update Account#?
The suggested change of angelIII will make the trigger not work correctly on updates i think.

If you have unique id 'RecID' on the table join like this will do:
...
INNER JOIN INSERTED
ON INSERTED.RecID = Table2.RecID
...

It is possible to prevent recursion by additionally checking the nestlevel of a trigger like this:
...
IF @@NESTLEVEL <= 1
BEGIN
...
END
...

And i think the 'SET NOCOUNT ON' option from suggestion #1 is a good idea as well, as it might disturb some systems if there is more than one reply saying 'X rows affected.'. This option will prevent this. Add this right before the UPDATE-command.

Avatar of sleimansleiman🇺🇸

ASKER

Well...some god new. I used the NOCOUNT and used the RecID in the join and now it works only for Insert action. If I try to update the record, it get the same error. My main goal was to handle inserted records and this should work. I am wondering though if we ever need to update the record, we will get the error.

I do have another tirgger that does a similar thing but updates other columns from table3 for example. I am not sure if that could be causing the error with the update. When I added a new rescord whiel testing, it seemed like both tirggers did what they were supposed to do.

I am wondering now if I should combine them.
Can the trigger hanled this by appeding the addition update code to the bottom of the existing trigger?

Free T-shirt

Get a FREE t-shirt when you ask your first question.

We believe in human intelligence. Our moderation policy strictly prohibits the use of LLM content in our Q&A threads.


You should be able to combine the triggers, but be aware of the fact that @@ROWCOUNT for example will be reset on each command within the trigger, so it only works on first line.
Microsoft SQL Server

Microsoft SQL Server

--

Questions

--

Followers

Top Experts

Microsoft SQL Server is a suite of relational database management system (RDBMS) products providing multi-user database access functionality.SQL Server is available in multiple versions, typically identified by release year, and versions are subdivided into editions to distinguish between product functionality. Component services include integration (SSIS), reporting (SSRS), analysis (SSAS), data quality, master data, T-SQL and performance tuning.