Solved

SQL Trigger: After Insert - Updates Table Multiple times -

Posted on 2008-10-30
13
710 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
Optimizing Cloud Backup for Low Bandwidth

With cloud storage prices going down a growing number of SMBs start to use it for backup storage. Unfortunately, business data volume rarely fits the average Internet speed. This article provides an overview of main Internet speed challenges and reveals backup best practices.

 
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

Use Case: Protecting a Hybrid Cloud Infrastructure

Microsoft Azure is rapidly becoming the norm in dynamic IT environments. This document describes the challenges that organizations face when protecting data in a hybrid cloud IT environment and presents a use case to demonstrate how Acronis Backup protects all data.

Question has a verified solution.

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

Suggested Solutions

If you having speed problem in loading SQL Server Management Studio, try to uncheck these options in your internet browser (IE -> Internet Options / Advanced / Security):    . Check for publisher's certificate revocation    . Check for server ce…
In this article we will get to know that how can we recover deleted data if it happens accidently. We really can recover deleted rows if we know the time when data is deleted by using the transaction log.
Email security requires an ever evolving service that stays up to date with counter-evolving threats. The Email Laundry perform Research and Development to ensure their email security service evolves faster than cyber criminals. We apply our Threat…
Nobody understands Phishing better than an anti-spam company. That’s why we are providing Phishing Awareness Training to our customers. According to a report by Verizon, only 3% of targeted users report malicious emails to management. With compan…

749 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