473,387 Members | 1,669 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

Instead of INSERT trigger!!

2
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 6088
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
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
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),...
3
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...
2
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...
5
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...
2
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...
3
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...
3
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...
3
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...
0
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...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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,...
0
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...
0
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...

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.