Hi,
I am almost going crazy with this. I have a table that I bulk insert into from another database using VS2005.
I need to change the data in the records before committing the values in the fields. To do that I created an (Instead of Insert) trigger.
The trigger basically check conditions using IF statements and accordingly does the appropriate action, which is simply switching values between two fields (swapping).
My problem is that when the trigger fires, the logic within the if statements seem to overlap or something. The whole table fields are swapped rather than only the targeted ones.
Please tell me what is the proper syntax for having multiple (IF THEN ELSE) statements within a trigger.
Here is the code to be more specific: - set ANSI_NULLS ON
-
set QUOTED_IDENTIFIER ON
-
GO
-
ALTER TRIGGER [dbo].[dbo_fwdcalls]
-
ON [dbo].[calls]
-
-
INSTEAD OF INSERT
-
-
AS
-
-
BEGIN
-
SET NOCOUNT ON;
-
IF EXISTS (SELECT joinonbehalfof FROM INSERTED
-
WHERE
-
((joinonbehalfof='10')
-
AND (duration > 0)
-
AND(LEFT(callingpartynumber,1)='8' OR LEFT(callingpartynumber,1)='9' )
-
AND (lastredirectdn BETWEEN '4020' AND '4024' OR lastredirectdn BETWEEN '5020' AND '5024')
-
))
-
-
-
-
INSERT INTO calls
-
SELECT [dateTimeOrigination]
-
,[origNodeId]
-
,[origSpan]
-
,[origIpAddr]
-
,[finalCalledPartyNumber]--Mark
-
,[origCause_location]
-
,[origCause_value]
-
,[origMediaTransportAddress_IP]
-
,[origMediaTransportAddress_Port]
-
,[destLegIdentifier]
-
,[destNodeId]
-
,[destSpan]
-
,[destIpAddr]
-
,[originalCalledPartyNumber]
-
,[callingPartyNumber]--Mark
-
,[destCause_location]
-
,[destCause_value]
-
,[destMediaTransportAddress_IP]
-
,[destMediaTransportAddress_Port]
-
,[dateTimeConnect]
-
,[dateTimeDisconnect]
-
,[lastRedirectDn]
-
,newid() as Pkid
-
,[originalCalledPartyNumberPartition]
-
,[finalCalledPartyNumberPartition]--Mark
-
,[callingPartyNumberPartition]--Mark
-
,[lastRedirectDnPartition]
-
,[duration]
-
,[origDeviceName]
-
,[destDeviceName]
-
,[origCalledPartyRedirectReason]
-
,[lastRedirectRedirectReason]
-
,[destConversationId]
-
,[origCallTerminationOnBehalfOf]
-
,[destCallTerminationOnBehalfOf]
-
,[lastRedirectRedirectOnBehalfOf]
-
,[joinOnBehalfOf]
-
FROM INSERTED
-
-
-
ELSE
-
-
-
IF EXISTS
-
(SELECT joinonbehalfof FROM INSERTED
-
WHERE
-
(joinonbehalfof='4')
-
AND (duration > 0)
-
AND(LEFT(callingpartynumber,1)='8' OR LEFT(callingpartynumber,1)='9' )
-
AND NOT (lastRedirectDn BETWEEN '4020' AND '4024' OR lastRedirectDn BETWEEN '5020' AND '5024')
-
AND (LEFT(lastRedirectDn,1) <> 'b')
-
)
-
-
-
INSERT INTO calls
-
SELECT [dateTimeOrigination]
-
,[origNodeId]
-
,[origSpan]
-
,[origIpAddr]
-
,[lastRedirectDn]--Mark
-
,[origCause_location]
-
,[origCause_value]
-
,[origMediaTransportAddress_IP]
-
,[origMediaTransportAddress_Port]
-
,[destLegIdentifier]
-
,[destNodeId]
-
,[destSpan]
-
,[destIpAddr]
-
,[originalCalledPartyNumber]
-
,[callingPartyNumber]--Mark
-
,[destCause_location]
-
,[destCause_value]
-
,[destMediaTransportAddress_IP]
-
,[destMediaTransportAddress_Port]
-
,[dateTimeConnect]
-
,[dateTimeDisconnect]
-
,[finalCalledPartyNumber]--Mark
-
,newid() as Pkid
-
,[originalCalledPartyNumberPartition]
-
,[callingPartyNumberPartition]
-
,[finalCalledPartyNumberPartition]
-
,[lastRedirectDnPartition]
-
,[duration]
-
,[origDeviceName]
-
,[destDeviceName]
-
,[origCalledPartyRedirectReason]
-
,[lastRedirectRedirectReason]
-
,[destConversationId]
-
,[origCallTerminationOnBehalfOf]
-
,[destCallTerminationOnBehalfOf]
-
,[lastRedirectRedirectOnBehalfOf]
-
,[joinOnBehalfOf]
-
FROM INSERTED
-
-
ELSE
-
-
IF EXISTS
-
(SELECT joinonbehalfof FROM INSERTED
-
WHERE
-
((joinonbehalfof='10')
-
AND (duration > 0)
-
AND(LEFT(callingpartynumber,1)='8' OR LEFT(callingpartynumber,1)='9' )
-
AND NOT (lastRedirectDn BETWEEN '4020' AND '4024' OR lastRedirectDn BETWEEN '5020' AND '5024')
-
AND (LEFT(lastRedirectDn,1) <> 'b'))
-
)
-
-
INSERT INTO calls
-
SELECT [dateTimeOrigination]
-
,[origNodeId]
-
,[origSpan]
-
,[origIpAddr]
-
,[lastRedirectDn]--Mark
-
,[origCause_location]
-
,[origCause_value]
-
,[origMediaTransportAddress_IP]
-
,[origMediaTransportAddress_Port]
-
,[destLegIdentifier]
-
,[destNodeId]
-
,[destSpan]
-
,[destIpAddr]
-
,[originalCalledPartyNumber]
-
,[callingPartyNumber]--Mark
-
,[destCause_location]
-
,[destCause_value]
-
,[destMediaTransportAddress_IP]
-
,[destMediaTransportAddress_Port]
-
,[dateTimeConnect]
-
,[dateTimeDisconnect]
-
,[finalCalledPartyNumber]--Mark
-
,NEWID() AS Pkid
-
,[originalCalledPartyNumberPartition]
-
,[callingPartyNumberPartition]
-
,[finalCalledPartyNumberPartition]
-
,[lastRedirectDnPartition]
-
,[duration]
-
,[origDeviceName]
-
,[destDeviceName]
-
,[origCalledPartyRedirectReason]
-
,[lastRedirectRedirectReason]
-
,[destConversationId]
-
,[origCallTerminationOnBehalfOf]
-
,[destCallTerminationOnBehalfOf]
-
,[lastRedirectRedirectOnBehalfOf]
-
,[joinOnBehalfOf]
-
FROM INSERTED
-
-
-
-
ELSE
-
-
-
INSERT INTO calls SELECT * From Inserted
-
-
-
-
-
END
2 6105
Disclaimer: Im not an expert.
I wrote an INSTEAD of INSERT trigger a while back and I found my conditions and actions worked just great...
Until I tried to do multi-line inserts that is. Then what was supposed to happen didn't, or happened when it wasn't supposed to. Much like you describe.
The solution I found was to declare the INSERTED table in a cursor thereby handling each record separately. Then my conditions and actions all seemed to work correctly, as they were evaluated and handled independantly.
Example:
In this example I simply wanted to add a surrogate sequential key to my table before insert. - Create TRIGGER trg_addkey
-
ON tblCompositeKey
-
Instead of INSERT
-
AS
-
Declare @seq int
-
Declare @key int
-
Declare @Data char(15)
-
-
Declare cur_0 scroll cursor for
-
Select keyfield, data
-
From inserted
-
-
open cur_0
-
-
FETCH NEXT FROM cur_0 into @Key, @Data
-
-
While @@fetch_status = 0
-
Begin
-
-
Set @Seq = (Select isnull((select max(seqfield) from tblCompositeKey Where keyfield = @key),0)+1)
-
-
Insert into tblCompositeKey
-
(keyfield, seqfield,data)
-
Values
-
(@key,@seq,@Data)
-
-
FETCH NEXT FROM cur_0 into @Key, @Data
-
End
-
-
Close cur_0
-
Deallocate cur_0
I dont know if that helps at all.
You have to encapsulate multi-line IF-ELSE statements in BEGIN/END blocks.
IF (1 = 1)
Single Line Command;
ELSE
Single Line Command;
IF (1 = 1)
BEGIN
Multi-Line Commands;
Multi-Line Commands;
END
ELSE
BEGIN
Multi-Line Commands;
Multi-Line Commands;
END
Sign in to post your reply or Sign up for a free account.
Similar topics |
by: Jenny |
last post by:
Hi!
I wonder how to use conditions in the inserted table(in a
insert/update) trigger? The inserted table contain all the rows that
have been updated or inserted (for an update/insert trigger), but out
of all these rows in inserted table, I only want the rows where a
particular field have been updated, for example if idkey have been
updated it would be in inserted BUT I only want this row if the field
amount have been updated. Can a use...
|
by: takilroy |
last post by:
Hi,
Does anyone know of a simple way to do this? I want to create an
insert trigger for a table and if the record already exists based on
some criteria, I want to update the table with the values that are
passed in via the insert trigger without having to use all the 'set'
statements for each field (so if we add fields in the future I won't
have to update the trigger). In other words, I want the trigger code
to look something like...
|
by: Bruce |
last post by:
I have an application written for version 8 that uses an INSTEAD OF
trigger, and I need to convert it to support someone that needs it for
v7. Is there a way to achieve similar functionality in v7. Here's an
example -
View on Base Table -
CREATE VIEW BASE_TABLE_VIEW AS SELECT
col1,
myUDF(col2),
col3)
|
by: William of Ockham |
last post by:
Hi,
I was asked to recreate a new clean database for our developers because
the current one they use is not entirely up to date. So I created a new
database and I run into the followin strange problem. First some facts:
System: DB2 V8.1 Fixpack5 Redhat Linux 8.0 dual processor
Database A is the current database and all DDL I currently have executes
fine. Database B is the new one and is created on the same instance as A.
|
by: 73blazer |
last post by:
Perhaps my thinking is wrong but this is what I have:
1 table (Tab1) with 1 attribute (Attr1)
Attr1 char(16) for bit data
-----------------------------------------------
create trigger check
no cascade before insert on Tab1
referencing new as N
| |
by: tomtailor |
last post by:
Hello!
I have a before insert Trigger and I want to catch if there is a
duplicate Key Error. If the Key already exists I'd like to update else
insert the row.
OK I am at the point I did the updates but if I raise an Error the
update get rolled back.
What do I have to define in the Trigger after the updates?
|
by: V T |
last post by:
Hello all,
SQL Server 2000 documentation
http://www.microsoft.com/technet/prodtechnol/sql/2000/reskit/part10/c3761.mspx
states that if view is using "NOT NULL" columns of a base table, then
insert/update performed on a view must provide dummy values for those
columns, and code of the trigger should ignore them.
But I cannot reproduce this restriction. Code below pasted to QueryAnalyser
shows that I can not supply dummy values for...
|
by: teddysnips |
last post by:
I need a trigger (well, I don't *need* one, but it would be optimal!)
but I can't get it to work because it references ntext fields.
Is there any alternative? I could write it in laborious code in the
application, but I'd rather not!
DDL for table and trigger below.
TIA
|
by: Maryan |
last post by:
Hi everybody,
i have a view, which contains some columns of two tables. I would like to insert informtions to this view but since this view is created through a join, it is impossible to insert informations to it.
So i decided to use an instead of trigger to do that but i don't know how?
CREATE TABLE Person (
PersonID integer NOT NULL,
Lastname varchar(255) NOT NULL,
Firstname ...
|
by: marktang |
last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look !
Part I. Meaning of...
|
by: Hystou |
last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it.
First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
| |
by: jinu1996 |
last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth.
The Art of Business Website Design
Your website is...
|
by: Hystou |
last post by:
Overview:
Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
|
by: conductexam |
last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one.
At the time of converting from word file to html my equations which are in the word document file was convert into image.
Globals.ThisAddIn.Application.ActiveDocument.Select();...
|
by: TSSRALBI |
last post by:
Hello
I'm a network technician in training and I need your help.
I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs.
The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols.
I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
|
by: 6302768590 |
last post by:
Hai team
i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
| |
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |