Solved

SQL Trigger: After Insert - Updates Table Multiple times -

Posted on 2008-10-30
13
715 Views
Last Modified: 2012-05-05
I created an After Insert Trigger. Therefore after I update  Table A with a new ID, it performs an insert on Table B. That great, however each new insert after the first one on Table A causes Table B to get inserted again. In other words, when I update Table A with record ID = 1, Table B gets updated with ID 1 (perfect). However, when I then update Table A with record ID = 2, Table B gets updated with ID 2 and another or second ID = 1. So now I have 3 records (2 ID = 1, and 1 ID = 2). And if I did an ID = 3 in Table A, Table B would now have (3 ID = 1, 2 ID = 2, and 1 ID = 3 or 6 records) and I want (1 ID =3, I ID =2, and I ID = 1 or 3 records). I only want a single insert for each record inserted.
0
Comment
Question by:SeTech
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 4
13 Comments
 
LVL 8

Expert Comment

by:tiagosalgado
ID: 22843495
Post here your trigger code please.
0
 

Author Comment

by:SeTech
ID: 22843887
USE [PeteTestXYZ]
GO
/****** Object:  Trigger [dbo].[Updatetable]    Script Date: 10/30/2008 14:27:47 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
ALTER Trigger [dbo].[Updatetable]
On [dbo].[tblAgtUpdate]
After Insert
AS
Insert Into PeteTestXYZ.dbo.tblUpdate (AgtNo, GA, AL, NC, SC)
 Select dbo.tblAgtUpdate.AgtNo, Case When State = 'GA' Then 'True' Else 'False' END AS GA,
Case When State = 'AL' Then 'True' Else 'False' End as AL,
Case When State = 'NC' Then 'True' Else 'False' End as NC,
Case When State = 'SC' Then 'True' Else 'False' End AS SC
from dbo.tblAgtUpdate Inner Join dbo.vwDataAgt On dbo.tblAgtUpdate.AgtNo = dbo.vwDataAgt.AgtNo
0
 
LVL 5

Accepted Solution

by:
Cvijo123 earned 125 total points
ID: 22844041
in your trigger you are not limiting anything what you actually do after insert, in other words you dont use inserted or deleted table that should be used in triggers.

so insted you using from dbo.tblAgtUpdate  you need to use from inserted

something like this
 
Insert Into PeteTestXYZ.dbo.tblUpdate (AgtNo, GA, AL, NC, SC)
 Select Inserted.AgtNo, Case When State = 'GA' Then 'True' Else 'False' END AS GA,
Case When State = 'AL' Then 'True' Else 'False' End as AL,
Case When State = 'NC' Then 'True' Else 'False' End as NC,
Case When State = 'SC' Then 'True' Else 'False' End AS SC
from dbo.Inserted Inner Join dbo.vwDataAgt On Inserted.AgtNo = dbo.vwDataAgt.AgtNo
	 

Open in new window

0
Online Training Solution

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action. Forget about retraining and skyrocket knowledge retention rates.

 
LVL 5

Expert Comment

by:Cvijo123
ID: 22844232
mybe to clerify it more so you understand how triggers work.

Trigger statements use two special tables, the deleted table and the inserted tables and SQL automaticlly manage and create those tables after your delete, update, insert on table that you create trigger on.

The deleted table stores copies of the affected rows during delete or update statements. During the execution of those statements, rows are deleted from the trigger table and transferred to the deleted table.

The inserted table stores copies of the affected rows during insert or update statements. During those transaction, new rows are added to both the inserted table and the trigger table. The rows in the inserted table are copies of the new rows in the trigger table.

So when you are using your trigger you must reference your inserts with inserted table.

Hope it is a bit clearer now :)


0
 

Author Comment

by:SeTech
ID: 22844376
Perfect and thanks !!!
0
 

Author Comment

by:SeTech
ID: 22849094
Well I understood what was written above and applied it to my trigger. However, when I add the Select Insert and Insert in the join I get a message that table does not exist and can not bound. It was my understanding from what was written on how triggers work that insert and delete tables are created where records are added and deleted as the trigger is fired. I also took that to mean that these insert and delete tables would be created on a virtual basis?. That does not seem to be the case. And it does not make sense to literally add these tables into the database as that would be like just renaming hte tables I am already using. Please advise again.
0
 
LVL 5

Expert Comment

by:Cvijo123
ID: 22850969
inserted and deleted table are always created when you do any action to row: update, insert or delete.

Keep in mind it is called inserted not insert and deleted not delete.
If u can put your new trigger code mybe i can see why are you getting that table doesnt exists error.
0
 

Author Comment

by:SeTech
ID: 22889317
USE [PeteTestXYZ]
GO
/****** Object:  Trigger [dbo].[Updatetable]    Script Date: 11/05/2008 15:18:03 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
ALTER Trigger [dbo].[Updatetable]
On [dbo].[tblAgtUpdate]
After Insert
AS
Insert Into PeteTestXYZ.dbo.tblUpdate (AgtNo, GA, AL, NC, SC)
 Select Inserted.AgtNo, Case When State = 'GA' Then 'True' Else 'False' END AS GA,
Case When State = 'AL' Then 'True' Else 'False' End as AL,
Case When State = 'NC' Then 'True' Else 'False' End as NC,
Case When State = 'SC' Then 'True' Else 'False' End AS SC
from dbo.vwDataAgt Inner Join dbo.Inserted On dbo.Inserted.AgtNo = dbo.vwDataAgt.AgtNo


0
 
LVL 5

Expert Comment

by:Cvijo123
ID: 23128266

i think solution that i gave is true and exectly what he asked in question, at least imo.
0

Featured Post

Back Up Your Microsoft Windows Server®

Back up all your Microsoft Windows Server – on-premises, in remote locations, in private and hybrid clouds. Your entire Windows Server will be backed up in one easy step with patented, block-level disk imaging. We achieve RTOs (recovery time objectives) as low as 15 seconds.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
MS SQL page split per second is high 19 144
Impove long SQL Stored Procedure Performance 14 82
Delete from table 6 49
Help Required 2 58
Introduction: When running hybrid database environments, you often need to query some data from a remote db of any type, while being connected to your MS SQL Server database. Problems start when you try to combine that with some "user input" pass…
In this article I will describe the Backup & Restore method as one possible migration process and I will add the extra tasks needed for an upgrade when and where is applied so it will cover all.
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…

752 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question