Solved

SQL Trigger: After Insert - Updates Table Multiple times -

Posted on 2008-10-30
13
704 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
  • 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
What is SQL Server and how does it work?

The purpose of this paper is to provide you background on SQL Server. It’s your self-study guide for learning fundamentals. It includes both the history of SQL and its technical basics. Concepts and definitions will form the solid foundation of your future DBA expertise.

 
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

Best Practices: Disaster Recovery Testing

Besides backup, any IT division should have a disaster recovery plan. You will find a few tips below relating to the development of such a plan and to what issues one should pay special attention in the course of backup planning.

Question has a verified solution.

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

Suggested Solutions

So every once in a while at work I am asked to export data from one table and insert it into another on a different server.  I hate doing this.  There's so many different tables and data types.  Some column data needs quoted and some doesn't.  What …
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…
This Micro Tutorial will give you a basic overview how to record your screen with Microsoft Expression Encoder. This program is still free and open for the public to download. This will be demonstrated using Microsoft Expression Encoder 4.
In this video I am going to show you how to back up and restore Office 365 mailboxes using CodeTwo Backup for Office 365. Learn more about the tool used in this video here: http://www.codetwo.com/backup-for-office-365/ (http://www.codetwo.com/ba…

813 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

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now