Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

SQL Trigger: After Insert - Updates Table Multiple times -

Posted on 2008-10-30
13
Medium Priority
?
723 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 500 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 does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
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

NEW Veeam Backup for Microsoft Office 365 1.5

With Office 365, it’s your data and your responsibility to protect it. NEW Veeam Backup for Microsoft Office 365 eliminates the risk of losing access to your Office 365 data.

Question has a verified solution.

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

There are some very powerful Dynamic Management Views (DMV's) introduced with SQL 2005. The two in particular that we are going to discuss are sys.dm_db_index_usage_stats and sys.dm_db_index_operational_stats.   Recently, I was involved in a di…
In SQL Server, when rows are selected from a table, does it retrieve data in the order in which it is inserted?  Many believe this is the case. Let us try to examine for ourselves with an example. To get started, use the following script, wh…
This course is ideal for IT System Administrators working with VMware vSphere and its associated products in their company infrastructure. This course teaches you how to install and maintain this virtualization technology to store data, prevent vuln…
Are you ready to place your question in front of subject-matter experts for more timely responses? With the release of Priority Question, Premium Members, Team Accounts and Qualified Experts can now identify the emergent level of their issue, signal…

963 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