Solved

SQL Trigger: After Insert - Updates Table Multiple times -

Posted on 2008-10-30
13
694 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
 
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
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.

 

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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

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 …
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.
Here's a very brief overview of the methods PRTG Network Monitor (https://www.paessler.com/prtg) offers for monitoring bandwidth, to help you decide which methods you´d like to investigate in more detail.  The methods are covered in more detail in o…
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

757 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

18 Experts available now in Live!

Get 1:1 Help Now