473,657 Members | 2,478 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Facing some problem in Instead of Trigger for MultiRow Insert

This is the Trigger which is not working properly during Update, no any
record is going to be updated so pls help.

I am updating the requisition table when any update in quantity in
podetails table

CREATE TRIGGER trig_updateRequ istion ON ERP.DBO.TranPur chaseOrderDetai l
INSTEAD OF UPDATE

AS

IF UPDATE(Quantity )

BEGIN

Update RequisitionSlip Detail
set RequisitionSlip Detail.PoQuanti ty =

(Select PoQuantity from RequisitionSlip Detail where
ItemCode=(Selec t CAST(i.ItemCode as nvarchar(20)) from inserted as i)
and
RSlip_No=(Selec t CAST(i.RSlip_No as int) from inserted as i)
)
-

((Select Quantity from TranPurchaseOrd erDetail where Purchase_OrderN o
=
(Select CAST(i.Purchase _OrderNo as nvarchar(20)) from inserted as i))

- (Select CAST(i.Quantity as int) from inserted as i))

where RequisitionSlip Detail.ItemCode = (Select CAST(i.ItemCode as
nvarchar(20)) from inserted as i) and RequisitionSlip Detail.RSlip_No =
(Select CAST(i.RSlip_No as int) from inserted as i)

Update TranPurchaseOrd erDetail set
TranPurchaseOrd erDetail.Quanti ty =
(Select CAST(i.Quantity as int) from inserted as i)
where TranPurchaseOrd erDetail.Purcha se_OrderNo = (Select
CAST(i.Purchase _OrderNo as nvarchar(20)) from inserted as i)
and
TranPurchaseOrd erDetail.ItemCo de = (Select CAST(i.ItemCode as
nvarchar(20)) from inserted as i)
and
TranPurchaseOrd erDetail.Purcha seDetailId =
(Select PurchaseDetailI d from TranPurchaseOrd erDetail where
Purchase_OrderN o = (Select CAST(i.Purchase _OrderNo as nvarchar(20))
from inserted as i))

END

Sep 5 '06 #1
1 1894
santoshborfalka r (sa************ ***@gmail.com) writes:
This is the Trigger which is not working properly during Update, no any
record is going to be updated so pls help.

I am updating the requisition table when any update in quantity in
podetails table
It's very difficult to tell what might be wrong without any knowledge of
your tables or the business rules.

But I noted a few things that appears ood.
CREATE TRIGGER trig_updateRequ istion ON ERP.DBO.TranPur chaseOrderDetai l
INSTEAD OF UPDATE
AS
IF UPDATE(Quantity )
So if the Quantity columns is not mentioned in the SET clause, then you
will not perform any update at all?
Update RequisitionSlip Detail
set RequisitionSlip Detail.PoQuanti ty =

(Select PoQuantity from RequisitionSlip Detail where
ItemCode=(Selec t CAST(i.ItemCode as nvarchar(20)) from inserted as i)
This may work, if only one row at a time is updated, but it will fail
with an error if many rows are updated. Recall that triggers fire once
per statement, not once per row.
Update TranPurchaseOrd erDetail set
TranPurchaseOrd erDetail.Quanti ty =
(Select CAST(i.Quantity as int) from inserted as i)
where TranPurchaseOrd erDetail.Purcha se_OrderNo = (Select
CAST(i.Purchase _OrderNo as nvarchar(20)) from inserted as i)
and
TranPurchaseOrd erDetail.ItemCo de = (Select CAST(i.ItemCode
as
nvarchar(20)) from inserted as i)
and
TranPurchaseOrd erDetail.Purcha seDetailId =
(Select PurchaseDetailI d from TranPurchaseOrd erDetail where
Purchase_OrderN o = (Select CAST(i.Purchase _OrderNo as nvarchar(20))
from inserted as i))
I don't understand this casting business. Why cast the columns of "inserted"
when they are the same as in the target table? A simplified version of
the above could be:

Update TranPurchaseOrd erDetail
set Quantity = i.Quantity
FROM TranPurchaseOrd erDetail T
JOIN inserted i ON T.Purchase_Orde rNo = i.Purchase_Orde rNo
AND T.ItemCode = i.ItemCode
and T.PurchaseDetai lId =
(Select PurchaseDetailI d from TranPurchaseOrd erDetail where
Purchase_OrderN o = (Select CAST(i.Purchase _OrderNo as nvarchar(20))
from inserted as i))
I did not rewrite the last bit, because, frankly, I don't understand what
it's supposed to mean. It just looks strange.

--
Erland Sommarskog, SQL Server MVP, es****@sommarsk og.se

Books Online for SQL Server 2005 at
http://www.microsoft.com/technet/pro...ads/books.mspx
Books Online for SQL Server 2000 at
http://www.microsoft.com/sql/prodinf...ons/books.mspx
Sep 5 '06 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
14238
by: Hank | last post by:
I have two SQL Server 2000 machines (server_A and server_B). I've used sp_addlinkedserver to link them both, the link seems to behave fine. I can execute remote queries and do all types of neat things from one while logged onto the other. I'm working on a project to keep the data in the two systems synchronized, so I'm using triggers on both sides to update each other. For testing, I've created a simple, one-column table on both...
8
8769
by: joe | last post by:
hi i am trying to write a insted of insert trigger to create a unique id when i insert a record in my database. can anyone give me an example with out using identity. thanks
6
6546
by: Scott CM | last post by:
I have a multi-part question regarding trigger performance. First of all, is there performance gain from issuing the following within a trigger: SELECT PrimaryKeyColumn FROM INSERTED opposed to: SELECT * FROM INSERTED
8
6515
by: Stuart McGraw | last post by:
Is Microsoft full of #*$#*% (again) or am I badly misunderstanding something? Quote from Microsoft's T-SQL doc: > INSTEAD OF triggers are executed instead of the triggering action. > These triggers are executed after the inserted and deleted tables > reflecting the changes to the base table are created, but before any > other actions are taken. They are executed before any constraints, ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ > so can perform...
4
1955
by: Dan | last post by:
I am loading data from table A into table B. Certain columns in B have check constraints. I'd like for any rows from A, which violate constraints, to be inserted into a third table, C. When the process is finished, I'll have only good rows in B, and exeption rows in C. I am investigating INSTEAD OF triggers, however my question to the group is, is there a better or best practice for this scenario? This must be common. Any high-level tips...
1
3112
by: Alex | last post by:
Hi, I need to create a trigger that will trap the insert commands on a table and if the row already exists, it updates the information. I started with this exemple but im getting syntax error "An unexpected token "CREATE TRIGGER Test_INS INSTEAD OF" was found following "BEGIN-OF-STATEMENT"."
8
7872
by: Benzine | last post by:
Hi, I have an issue with my replication at the moment. I will try to describe the scenario accurately. I am using MS SQL 2000 SP4 with Merge Replication. Subscribers connect to the publisher to upload/download changes. I have a trigger set up on one table which updates another, here is an example of the trigger: "CREATE TRIGGER qt_t_projTotal ON dbo.qt_quotes
11
4070
by: Ed Dror | last post by:
Hi there, I'm using ASP.NET 2.0 and SQL Server 2005 with VS 2005 Pro. I have a Price page (my website require login) with GridView with the following columns PriceID, Amount, Approved, CrtdUser and Date And Edit and Delete buttons
1
3193
by: asf93555 | last post by:
Running under SQL2000 I can not get an INSTEAD trigger to function. I've even copied the example directl from books online - no joy . . . Server: Msg 170, Level 15, State 1, Procedure IO_Trig_INS_Employee, Line 2 Line 2: Incorrect syntax near 'INSTEAD'. CREATE TABLE Person ( SSN char(11) PRIMARY KEY, Name nvarchar(100), Address nvarchar(100),
0
8842
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8740
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
8516
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
8617
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
4173
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
4330
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2743
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
1970
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
2
1733
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.