SQL Server : AFTER INSERT trigger not working properly, even after making sure FIRE_TRIGGERS was set in SSIS packageSolutions for INSERT OR UPDATE on SQL ServerInsert Update trigger how to determine if insert or updateSelect n random rows from SQL Server tableSET NOCOUNT ON usageSELECT INTO a table variable in T-SQLSQL Server trigger - SET NORESULTS ON?SQL Server Trigger not triggered after insert and updateHow to fire trigger after every insert in sql server table automatically?Insert trigger in SQL ServerSQL Server trigger failing for row inserts in quick succession

What is göddertrobbe?

Why was the battle set up *outside* Winterfell?

Airbnb - host wants to reduce rooms, can we get refund?

What is the temperature of the black night sky?

Why is B♯ higher than C♭ in 31-ET?

How can I close a gap between my fence and my neighbor's that's on his side of the property line?

What actually is the vector of angular momentum?

Upside-Down Pyramid Addition...REVERSED!

What does a yield inside a yield do?

SFINAE works with deduction but fails with substitution

What is the most remote airport from the center of the city it supposedly serves?

Why is `abs()` implemented differently?

Number of seconds in 6 weeks

How could a planet have most of its water in the atmosphere?

Pressure to defend the relevance of one's area of mathematics

Is there a legal ground for stripping the UK of its UN Veto if Scotland and/or N.Ireland split from the UK?

Manager is threatning to grade me poorly if I don't complete the project

What is the minimal installation possible in order to run a .jar Java file?

Has any spacecraft ever had the ability to directly communicate with civilian air traffic control?

Would glacier 'trees' be plausible?

What algorithms are considered reinforcement learning algorithms?

In Avengers 1, why does Thanos need Loki?

Why is Arya visibly scared in the library in S8E3?

In Endgame, why were these characters still around?



SQL Server : AFTER INSERT trigger not working properly, even after making sure FIRE_TRIGGERS was set in SSIS package


Solutions for INSERT OR UPDATE on SQL ServerInsert Update trigger how to determine if insert or updateSelect n random rows from SQL Server tableSET NOCOUNT ON usageSELECT INTO a table variable in T-SQLSQL Server trigger - SET NORESULTS ON?SQL Server Trigger not triggered after insert and updateHow to fire trigger after every insert in sql server table automatically?Insert trigger in SQL ServerSQL Server trigger failing for row inserts in quick succession






.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty height:90px;width:728px;box-sizing:border-box;








0















Unsure why this is not working properly. Let me give some context of what I want to happen.



I am inserting a large number of rows into the OpenCase table (about 35 K). This data is from the last 3 months. The Resolved Case table is already current and up to date.



Some of the open cases have since been resolved, so I want it to check each row from the inserted table and check to see if that SUB_QUERY_ID is in the Resolved Case table. If it is, I want to update the RESOLVED_ON date in the Open Case table with the SUB_QUERY_CLOSED_ON date in the Resolved Case table for this case.



An example below:



From Resolved Case table:



SUB_QUERY_ID SUB_QUERY_CLOSED_ON
----------------------------------
US12332-1 1/28/2019


From Open Case table:



SUB_QUERY_ID RESOLVED_ON
-------------------------------------
US12332-1 (The trigger should get the SUB_QUERY_CLOSED_ON date from
the Resolved Cases Table and update it here)


From the example I have seen, this should be working, but the RESOLVED_ON dates are all NULL, even when I manually check a few of the Open Case SUB_QUERY_ID's and see they are in the Resolved Case table.



ALTER TRIGGER [FindResolvedCaseDate] 
ON [OpenCases]
AFTER INSERT, UPDATE
AS
BEGIN
IF @@ROWCOUNT = 0
RETURN

SET NOCOUNT ON;

DECLARE @SUB_QUERY_ID VARCHAR(150)

SELECT @SUB_QUERY_ID = SUB_QUERY_ID
FROM INSERTED

UPDATE OpenCases
SET RESOLVED_ON = (SELECT TOP 1 SUB_QUERY_CLOSED_ON
FROM ResolvedCases
WHERE SUB_QUERY_ID = @SUB_QUERY_ID
ORDER BY SUB_QUERY_CLOSED_ON DESC)
END









share|improve this question



















  • 3





    Your trigger has MAJOR flaw in that you seem to assume it'll be called once per row - that is not the case. The trigger will fire once per statement, so if your INSERT that causes this trigger to fire inserts 25 rows, you'll get the trigger fired once and the Inserted pseudo table will contain 25 rows. Which of those 25 rows will your code select from Inserted? It's non-deterministic, you'll get one arbitrary row and you will be ignoring all other rows. You need to rewrite your trigger to take this into account!

    – marc_s
    Mar 22 at 21:25











  • It is not safe to rely on @@rowcount in a trigger. Why? In a merge situation, this will be set to the total number of rows affected by the merge statement (inserted, updated, deleted). This slow loading link to web archive explains.

    – SMor
    Mar 22 at 22:52

















0















Unsure why this is not working properly. Let me give some context of what I want to happen.



I am inserting a large number of rows into the OpenCase table (about 35 K). This data is from the last 3 months. The Resolved Case table is already current and up to date.



Some of the open cases have since been resolved, so I want it to check each row from the inserted table and check to see if that SUB_QUERY_ID is in the Resolved Case table. If it is, I want to update the RESOLVED_ON date in the Open Case table with the SUB_QUERY_CLOSED_ON date in the Resolved Case table for this case.



An example below:



From Resolved Case table:



SUB_QUERY_ID SUB_QUERY_CLOSED_ON
----------------------------------
US12332-1 1/28/2019


From Open Case table:



SUB_QUERY_ID RESOLVED_ON
-------------------------------------
US12332-1 (The trigger should get the SUB_QUERY_CLOSED_ON date from
the Resolved Cases Table and update it here)


From the example I have seen, this should be working, but the RESOLVED_ON dates are all NULL, even when I manually check a few of the Open Case SUB_QUERY_ID's and see they are in the Resolved Case table.



ALTER TRIGGER [FindResolvedCaseDate] 
ON [OpenCases]
AFTER INSERT, UPDATE
AS
BEGIN
IF @@ROWCOUNT = 0
RETURN

SET NOCOUNT ON;

DECLARE @SUB_QUERY_ID VARCHAR(150)

SELECT @SUB_QUERY_ID = SUB_QUERY_ID
FROM INSERTED

UPDATE OpenCases
SET RESOLVED_ON = (SELECT TOP 1 SUB_QUERY_CLOSED_ON
FROM ResolvedCases
WHERE SUB_QUERY_ID = @SUB_QUERY_ID
ORDER BY SUB_QUERY_CLOSED_ON DESC)
END









share|improve this question



















  • 3





    Your trigger has MAJOR flaw in that you seem to assume it'll be called once per row - that is not the case. The trigger will fire once per statement, so if your INSERT that causes this trigger to fire inserts 25 rows, you'll get the trigger fired once and the Inserted pseudo table will contain 25 rows. Which of those 25 rows will your code select from Inserted? It's non-deterministic, you'll get one arbitrary row and you will be ignoring all other rows. You need to rewrite your trigger to take this into account!

    – marc_s
    Mar 22 at 21:25











  • It is not safe to rely on @@rowcount in a trigger. Why? In a merge situation, this will be set to the total number of rows affected by the merge statement (inserted, updated, deleted). This slow loading link to web archive explains.

    – SMor
    Mar 22 at 22:52













0












0








0








Unsure why this is not working properly. Let me give some context of what I want to happen.



I am inserting a large number of rows into the OpenCase table (about 35 K). This data is from the last 3 months. The Resolved Case table is already current and up to date.



Some of the open cases have since been resolved, so I want it to check each row from the inserted table and check to see if that SUB_QUERY_ID is in the Resolved Case table. If it is, I want to update the RESOLVED_ON date in the Open Case table with the SUB_QUERY_CLOSED_ON date in the Resolved Case table for this case.



An example below:



From Resolved Case table:



SUB_QUERY_ID SUB_QUERY_CLOSED_ON
----------------------------------
US12332-1 1/28/2019


From Open Case table:



SUB_QUERY_ID RESOLVED_ON
-------------------------------------
US12332-1 (The trigger should get the SUB_QUERY_CLOSED_ON date from
the Resolved Cases Table and update it here)


From the example I have seen, this should be working, but the RESOLVED_ON dates are all NULL, even when I manually check a few of the Open Case SUB_QUERY_ID's and see they are in the Resolved Case table.



ALTER TRIGGER [FindResolvedCaseDate] 
ON [OpenCases]
AFTER INSERT, UPDATE
AS
BEGIN
IF @@ROWCOUNT = 0
RETURN

SET NOCOUNT ON;

DECLARE @SUB_QUERY_ID VARCHAR(150)

SELECT @SUB_QUERY_ID = SUB_QUERY_ID
FROM INSERTED

UPDATE OpenCases
SET RESOLVED_ON = (SELECT TOP 1 SUB_QUERY_CLOSED_ON
FROM ResolvedCases
WHERE SUB_QUERY_ID = @SUB_QUERY_ID
ORDER BY SUB_QUERY_CLOSED_ON DESC)
END









share|improve this question
















Unsure why this is not working properly. Let me give some context of what I want to happen.



I am inserting a large number of rows into the OpenCase table (about 35 K). This data is from the last 3 months. The Resolved Case table is already current and up to date.



Some of the open cases have since been resolved, so I want it to check each row from the inserted table and check to see if that SUB_QUERY_ID is in the Resolved Case table. If it is, I want to update the RESOLVED_ON date in the Open Case table with the SUB_QUERY_CLOSED_ON date in the Resolved Case table for this case.



An example below:



From Resolved Case table:



SUB_QUERY_ID SUB_QUERY_CLOSED_ON
----------------------------------
US12332-1 1/28/2019


From Open Case table:



SUB_QUERY_ID RESOLVED_ON
-------------------------------------
US12332-1 (The trigger should get the SUB_QUERY_CLOSED_ON date from
the Resolved Cases Table and update it here)


From the example I have seen, this should be working, but the RESOLVED_ON dates are all NULL, even when I manually check a few of the Open Case SUB_QUERY_ID's and see they are in the Resolved Case table.



ALTER TRIGGER [FindResolvedCaseDate] 
ON [OpenCases]
AFTER INSERT, UPDATE
AS
BEGIN
IF @@ROWCOUNT = 0
RETURN

SET NOCOUNT ON;

DECLARE @SUB_QUERY_ID VARCHAR(150)

SELECT @SUB_QUERY_ID = SUB_QUERY_ID
FROM INSERTED

UPDATE OpenCases
SET RESOLVED_ON = (SELECT TOP 1 SUB_QUERY_CLOSED_ON
FROM ResolvedCases
WHERE SUB_QUERY_ID = @SUB_QUERY_ID
ORDER BY SUB_QUERY_CLOSED_ON DESC)
END






sql-server triggers






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Mar 22 at 21:25









marc_s

587k13011281274




587k13011281274










asked Mar 22 at 21:03









MattEMattE

360517




360517







  • 3





    Your trigger has MAJOR flaw in that you seem to assume it'll be called once per row - that is not the case. The trigger will fire once per statement, so if your INSERT that causes this trigger to fire inserts 25 rows, you'll get the trigger fired once and the Inserted pseudo table will contain 25 rows. Which of those 25 rows will your code select from Inserted? It's non-deterministic, you'll get one arbitrary row and you will be ignoring all other rows. You need to rewrite your trigger to take this into account!

    – marc_s
    Mar 22 at 21:25











  • It is not safe to rely on @@rowcount in a trigger. Why? In a merge situation, this will be set to the total number of rows affected by the merge statement (inserted, updated, deleted). This slow loading link to web archive explains.

    – SMor
    Mar 22 at 22:52












  • 3





    Your trigger has MAJOR flaw in that you seem to assume it'll be called once per row - that is not the case. The trigger will fire once per statement, so if your INSERT that causes this trigger to fire inserts 25 rows, you'll get the trigger fired once and the Inserted pseudo table will contain 25 rows. Which of those 25 rows will your code select from Inserted? It's non-deterministic, you'll get one arbitrary row and you will be ignoring all other rows. You need to rewrite your trigger to take this into account!

    – marc_s
    Mar 22 at 21:25











  • It is not safe to rely on @@rowcount in a trigger. Why? In a merge situation, this will be set to the total number of rows affected by the merge statement (inserted, updated, deleted). This slow loading link to web archive explains.

    – SMor
    Mar 22 at 22:52







3




3





Your trigger has MAJOR flaw in that you seem to assume it'll be called once per row - that is not the case. The trigger will fire once per statement, so if your INSERT that causes this trigger to fire inserts 25 rows, you'll get the trigger fired once and the Inserted pseudo table will contain 25 rows. Which of those 25 rows will your code select from Inserted? It's non-deterministic, you'll get one arbitrary row and you will be ignoring all other rows. You need to rewrite your trigger to take this into account!

– marc_s
Mar 22 at 21:25





Your trigger has MAJOR flaw in that you seem to assume it'll be called once per row - that is not the case. The trigger will fire once per statement, so if your INSERT that causes this trigger to fire inserts 25 rows, you'll get the trigger fired once and the Inserted pseudo table will contain 25 rows. Which of those 25 rows will your code select from Inserted? It's non-deterministic, you'll get one arbitrary row and you will be ignoring all other rows. You need to rewrite your trigger to take this into account!

– marc_s
Mar 22 at 21:25













It is not safe to rely on @@rowcount in a trigger. Why? In a merge situation, this will be set to the total number of rows affected by the merge statement (inserted, updated, deleted). This slow loading link to web archive explains.

– SMor
Mar 22 at 22:52





It is not safe to rely on @@rowcount in a trigger. Why? In a merge situation, this will be set to the total number of rows affected by the merge statement (inserted, updated, deleted). This slow loading link to web archive explains.

– SMor
Mar 22 at 22:52












0






active

oldest

votes












Your Answer






StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");

StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);

else
createEditor();

);

function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);



);













draft saved

draft discarded


















StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55307744%2fsql-server-after-insert-trigger-not-working-properly-even-after-making-sure-f%23new-answer', 'question_page');

);

Post as a guest















Required, but never shown

























0






active

oldest

votes








0






active

oldest

votes









active

oldest

votes






active

oldest

votes















draft saved

draft discarded
















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid


  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55307744%2fsql-server-after-insert-trigger-not-working-properly-even-after-making-sure-f%23new-answer', 'question_page');

);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

SQL error code 1064 with creating Laravel foreign keysForeign key constraints: When to use ON UPDATE and ON DELETEDropping column with foreign key Laravel error: General error: 1025 Error on renameLaravel SQL Can't create tableLaravel Migration foreign key errorLaravel php artisan migrate:refresh giving a syntax errorSQLSTATE[42S01]: Base table or view already exists or Base table or view already exists: 1050 Tableerror in migrating laravel file to xampp serverSyntax error or access violation: 1064:syntax to use near 'unsigned not null, modelName varchar(191) not null, title varchar(191) not nLaravel cannot create new table field in mysqlLaravel 5.7:Last migration creates table but is not registered in the migration table

용인 삼성생명 블루밍스 목차 통계 역대 감독 선수단 응원단 경기장 같이 보기 외부 링크 둘러보기 메뉴samsungblueminx.comeh선수 명단용인 삼성생명 블루밍스용인 삼성생명 블루밍스ehsamsungblueminx.comeheheheh

155 수학 과학 기타 둘러보기 메뉴eh추가해eh문서를 완성해