473,804 Members | 3,607 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Instead of INSERT trigger!!

2 New Member
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:





Expand|Select|Wrap|Line Numbers
  1. set ANSI_NULLS ON
  2. set QUOTED_IDENTIFIER ON
  3. GO
  4. ALTER TRIGGER [dbo].[dbo_fwdcalls]
  5. ON [dbo].[calls]
  6.  
  7. INSTEAD OF INSERT
  8.  
  9. AS
  10.  
  11. BEGIN
  12. SET NOCOUNT ON;
  13. IF EXISTS (SELECT joinonbehalfof FROM INSERTED
  14. WHERE
  15. ((joinonbehalfof='10')
  16. AND (duration > 0)
  17. AND(LEFT(callingpartynumber,1)='8' OR LEFT(callingpartynumber,1)='9' )
  18. AND (lastredirectdn BETWEEN '4020' AND '4024' OR lastredirectdn BETWEEN '5020' AND '5024')
  19. ))
  20.  
  21.  
  22.  
  23. INSERT INTO calls
  24. SELECT [dateTimeOrigination]
  25. ,[origNodeId]
  26. ,[origSpan]
  27. ,[origIpAddr]
  28. ,[finalCalledPartyNumber]--Mark
  29. ,[origCause_location]
  30. ,[origCause_value]
  31. ,[origMediaTransportAddress_IP]
  32. ,[origMediaTransportAddress_Port]
  33. ,[destLegIdentifier]
  34. ,[destNodeId]
  35. ,[destSpan]
  36. ,[destIpAddr]
  37. ,[originalCalledPartyNumber]
  38. ,[callingPartyNumber]--Mark
  39. ,[destCause_location]
  40. ,[destCause_value]
  41. ,[destMediaTransportAddress_IP]
  42. ,[destMediaTransportAddress_Port]
  43. ,[dateTimeConnect]
  44. ,[dateTimeDisconnect]
  45. ,[lastRedirectDn]
  46. ,newid() as Pkid
  47. ,[originalCalledPartyNumberPartition]
  48. ,[finalCalledPartyNumberPartition]--Mark
  49. ,[callingPartyNumberPartition]--Mark
  50. ,[lastRedirectDnPartition]
  51. ,[duration]
  52. ,[origDeviceName]
  53. ,[destDeviceName]
  54. ,[origCalledPartyRedirectReason]
  55. ,[lastRedirectRedirectReason]
  56. ,[destConversationId]
  57. ,[origCallTerminationOnBehalfOf]
  58. ,[destCallTerminationOnBehalfOf]
  59. ,[lastRedirectRedirectOnBehalfOf]
  60. ,[joinOnBehalfOf]
  61. FROM INSERTED
  62.  
  63.  
  64. ELSE
  65.  
  66.  
  67. IF EXISTS
  68. (SELECT joinonbehalfof FROM INSERTED
  69. WHERE
  70. (joinonbehalfof='4')
  71. AND (duration > 0)
  72. AND(LEFT(callingpartynumber,1)='8' OR LEFT(callingpartynumber,1)='9' )
  73. AND NOT (lastRedirectDn BETWEEN '4020' AND '4024' OR lastRedirectDn BETWEEN '5020' AND '5024')
  74. AND (LEFT(lastRedirectDn,1) <> 'b')
  75. )
  76.  
  77.  
  78. INSERT INTO calls
  79. SELECT [dateTimeOrigination]
  80. ,[origNodeId]
  81. ,[origSpan]
  82. ,[origIpAddr]
  83. ,[lastRedirectDn]--Mark
  84. ,[origCause_location]
  85. ,[origCause_value]
  86. ,[origMediaTransportAddress_IP]
  87. ,[origMediaTransportAddress_Port]
  88. ,[destLegIdentifier]
  89. ,[destNodeId]
  90. ,[destSpan]
  91. ,[destIpAddr]
  92. ,[originalCalledPartyNumber]
  93. ,[callingPartyNumber]--Mark
  94. ,[destCause_location]
  95. ,[destCause_value]
  96. ,[destMediaTransportAddress_IP]
  97. ,[destMediaTransportAddress_Port]
  98. ,[dateTimeConnect]
  99. ,[dateTimeDisconnect]
  100. ,[finalCalledPartyNumber]--Mark
  101. ,newid() as Pkid
  102. ,[originalCalledPartyNumberPartition]
  103. ,[callingPartyNumberPartition]
  104. ,[finalCalledPartyNumberPartition]
  105. ,[lastRedirectDnPartition]
  106. ,[duration]
  107. ,[origDeviceName]
  108. ,[destDeviceName]
  109. ,[origCalledPartyRedirectReason]
  110. ,[lastRedirectRedirectReason]
  111. ,[destConversationId]
  112. ,[origCallTerminationOnBehalfOf]
  113. ,[destCallTerminationOnBehalfOf]
  114. ,[lastRedirectRedirectOnBehalfOf]
  115. ,[joinOnBehalfOf]
  116. FROM INSERTED
  117.  
  118. ELSE
  119.  
  120. IF EXISTS
  121. (SELECT joinonbehalfof FROM INSERTED
  122. WHERE
  123. ((joinonbehalfof='10')
  124. AND (duration > 0)
  125. AND(LEFT(callingpartynumber,1)='8' OR LEFT(callingpartynumber,1)='9' )
  126. AND NOT (lastRedirectDn BETWEEN '4020' AND '4024' OR lastRedirectDn BETWEEN '5020' AND '5024')
  127. AND (LEFT(lastRedirectDn,1) <> 'b'))
  128. )
  129.  
  130. INSERT INTO calls
  131. SELECT [dateTimeOrigination]
  132. ,[origNodeId]
  133. ,[origSpan]
  134. ,[origIpAddr]
  135. ,[lastRedirectDn]--Mark
  136. ,[origCause_location]
  137. ,[origCause_value]
  138. ,[origMediaTransportAddress_IP]
  139. ,[origMediaTransportAddress_Port]
  140. ,[destLegIdentifier]
  141. ,[destNodeId]
  142. ,[destSpan]
  143. ,[destIpAddr]
  144. ,[originalCalledPartyNumber]
  145. ,[callingPartyNumber]--Mark
  146. ,[destCause_location]
  147. ,[destCause_value]
  148. ,[destMediaTransportAddress_IP]
  149. ,[destMediaTransportAddress_Port]
  150. ,[dateTimeConnect]
  151. ,[dateTimeDisconnect]
  152. ,[finalCalledPartyNumber]--Mark
  153. ,NEWID() AS Pkid
  154. ,[originalCalledPartyNumberPartition]
  155. ,[callingPartyNumberPartition]
  156. ,[finalCalledPartyNumberPartition]
  157. ,[lastRedirectDnPartition]
  158. ,[duration]
  159. ,[origDeviceName]
  160. ,[destDeviceName]
  161. ,[origCalledPartyRedirectReason]
  162. ,[lastRedirectRedirectReason]
  163. ,[destConversationId]
  164. ,[origCallTerminationOnBehalfOf]
  165. ,[destCallTerminationOnBehalfOf]
  166. ,[lastRedirectRedirectOnBehalfOf]
  167. ,[joinOnBehalfOf]
  168. FROM INSERTED
  169.  
  170.  
  171.  
  172. ELSE
  173.  
  174.  
  175. INSERT INTO calls SELECT * From Inserted
  176.  
  177.  
  178.  
  179.  
  180. END
Jul 28 '07 #1
2 6105
bwestover
39 New Member
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.

Expand|Select|Wrap|Line Numbers
  1. Create TRIGGER trg_addkey
  2. ON  tblCompositeKey
  3. Instead of INSERT
  4. AS 
  5. Declare @seq int
  6. Declare @key int
  7. Declare @Data char(15)
  8.  
  9. Declare cur_0 scroll cursor for
  10. Select keyfield, data
  11. From inserted
  12.  
  13. open cur_0
  14.  
  15. FETCH NEXT FROM cur_0 into @Key, @Data
  16.  
  17. While @@fetch_status = 0
  18. Begin
  19.  
  20. Set @Seq = (Select isnull((select max(seqfield) from tblCompositeKey Where keyfield = @key),0)+1)
  21.  
  22. Insert into tblCompositeKey
  23. (keyfield, seqfield,data)
  24. Values
  25. (@key,@seq,@Data)
  26.  
  27. FETCH NEXT FROM cur_0 into @Key, @Data
  28. End
  29.  
  30. Close cur_0
  31. Deallocate cur_0
I dont know if that helps at all.
Aug 1 '07 #2
Irasimus
1 New Member
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
Aug 2 '07 #3

Sign in to post your reply or Sign up for a free account.

Similar topics

2
21832
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...
3
7284
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...
2
1957
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)
5
4527
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.
2
6436
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
3
3456
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?
3
7370
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...
3
3731
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
0
1296
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 ...
0
9706
marktang
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...
0
9579
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,...
0
10326
jinu1996
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...
1
10317
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,...
0
6851
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();...
0
5520
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...
0
5651
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4295
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
2
3815
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.