Microsoft SQL Server
--
Questions
--
Followers
Top Experts
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.
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
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.
Â
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.






EARN REWARDS FOR ASKING, ANSWERING, AND MORE.
Earn free swag for participating on the platform.
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).
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.
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?

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.
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.