Trigger for inserting work order information into Inventory Transf

Dear All,

I just created two triggers inroder to insert the workorder info into the tables (Purchaseorder and purchaseorderentry). the triggers are created on order,orderentry tables respectively. After creating the work order when i check the database, i saw the integrity between order and orderentry tables lost. the orderid field of orderentry table refering to the the field id in order table is different. CREATE TRIGGER INSERT_PO ON [ORDER] FOR INSERT AS declare @tpId as integer declare @PoNm as integer declare @Potype as integer set @Potype=2 set @PoNm=(select isnull(max(ponumber),0)+1 from PurchaseOrder) insert into PurchaseOrder(potype,ponumber,storeid,[to],shipto,lastupdated,datecreated,remarks,otherstoreid) select @Potype,@PoNm,i.storeid,'WAREHOUSE','SHOWROOM',i.lastupdated,getdate(),i.id,@Potype from inserted i

CREATE TRIGGER INSER_PO_ENTRY ON ORDEREntry FOR INSERT AS declare @tpId as varchar(10) declare @tpId_C as integer set @tpID=(select orderid from inserted) set @tpId_C=(select id from purchaseorder where remarks=ltrim(rtrim(@tpID))) insert into purchaseorderentry(purchaseorderid,itemdescription,itemid,lastupdated,storeid,quantityordered,price) select @tpId_C,[description],itemid,lastupdated,storeid,quantityonorder,price from inserted i

Any Help will be appreciated, Thanks in Advance,

Ibrahim Zubair

---------------- This post is a suggestion for Microsoft, and Microsoft responds to the suggestions with the most votes. To vote for this suggestion, click the "I Agree" button in the message pane. If you do not see the button, follow this link to open the suggestion in the Microsoft Web-based Newsreader and then click "I Agree" in the message pane.

formatting link
5718d4-e784-44a2-910a-6b6a5f825624&dg=microsoft.public.pos

Reply to
Ibrahim Zubair
Loading thread data ...

I suspect that @tpID_C is not returning exactly one record. Try running that selection on it'sown in query analyzer and see if it gets what you expect... I think you are doing your trimming to the wrong side of the equation - Remarks is the text field that you may need to trim or cast..

Glenn Adams Tiber Creek C> Dear All,

PurchaseOrder(potype,ponumber,storeid,[to],shipto,lastupdated,datecreated,remarks,otherstoreid)

@Potype,@PoNm,i.storeid,'WAREHOUSE','SHOWROOM',i.lastupdated,getdate(),i.id,@Potype

purchaseorderentry(purchaseorderid,itemdescription,itemid,lastupdated,storeid,quantityordered,price)

formatting link
5718d4-e784-44a2-910a-6b6a5f825624&dg=microsoft.public.pos

Reply to
Glenn Adams [MVP - Retail Mgmt

Dear Glenn Adams, Thanks for reply, I removed the second trigger & I am facing the same error, Let's focus on the first trigger :

CREATE TRIGGER INSERT_PO ON [ORDER] after INSERT AS declare @tpId as integer declare @PoNm as integer declare @Potype as integer set @Potype=2 set @PoNm=(select isnull(max(ponumber),0)+1 from PurchaseOrder) insert into PurchaseOrder(potype,ponumber,storeid,[to],shipto,lastupdated,datecreated,remarks,otherstoreid) select @Potype,@PoNm,i.storeid,'WAREHOUSE','SHOWROOM',i.lastupdated,getdate(),i.id,@Potype from inserted i

Best Regards, Ibrahim Zubair

"Glenn Adams [MVP - Retail Mgmt]" wrote:

PurchaseOrder(potype,ponumber,storeid,[to],shipto,lastupdated,datecreated,remarks,otherstoreid)

@Potype,@PoNm,i.storeid,'WAREHOUSE','SHOWROOM',i.lastupdated,getdate(),i.id,@Potype

purchaseorderentry(purchaseorderid,itemdescription,itemid,lastupdated,storeid,quantityordered,price)

formatting link
5718d4-e784-44a2-910a-6b6a5f825624&dg=microsoft.public.pos>

Reply to
Ibrahim Zubair

You can't have the same error without the second trigger. No Purchase Order Entries are being created at all without the second trigger, so how can you have an integrity issue with them? Is the remarks column getting the wrong ID value? If so, try using @@IDENTITY instead of i.ID

Glenn Adams Tiber Creek C> Dear Glenn Adams,

PurchaseOrder(potype,ponumber,storeid,[to],shipto,lastupdated,datecreated,remarks,otherstoreid)

@Potype,@PoNm,i.storeid,'WAREHOUSE','SHOWROOM',i.lastupdated,getdate(),i.id,@Potype

PurchaseOrder(potype,ponumber,storeid,[to],shipto,lastupdated,datecreated,remarks,otherstoreid)

@Potype,@PoNm,i.storeid,'WAREHOUSE','SHOWROOM',i.lastupdated,getdate(),i.id,@Potype

purchaseorderentry(purchaseorderid,itemdescription,itemid,lastupdated,storeid,quantityordered,price)

formatting link
5718d4-e784-44a2-910a-6b6a5f825624&dg=microsoft.public.pos

Reply to
Glenn Adams [MVP - Retail Mgmt

Dear Glenn Actually we were trying only on header tables soon after we found this problem. That is why we removed the second trigger for Detail table. Now the problem is when we run the trigger it is inserting the id from purchase order table as the orderid into the orderid field of orderentry table . Actually it is supposed to take the id from order table. I tried with @@identity also instead of i.id. But the same problem. I tried to make the trigger order last using the command sp_settriggerorder. Still it is take the feilds from purchaseorder table. Hope you can help me. Thank you very much

Reply to
Ibrahim Zubair

Nope, can't help you based on what you've posted. Are you sure there are no other triggers, maybe on the PurchaseOrder or PurchaceOrderEntry Tables?

Glenn Adams Tiber Creek C> Dear Glenn

Reply to
Glenn Adams [MVP - Retail Mgmt

I presume that you want to order items that are on a work order. I should caution you doing it the way you are approaching the problem. Because:

-1- On the same WO you can have different items belonging to multiple suppliers. - 2- At the end of the day you will end up having lots of PO with a small quanity on them [suppliers do not like this and may charge you more] - 3 - what do you do if the customer changes WO, adds a line or deletes a line???

If you want to discuss this further contact me.

PurchaseOrder(potype,ponumber,storeid,[to],shipto,lastupdated,datecreated,remarks,otherstoreid)

@Potype,@PoNm,i.storeid,'WAREHOUSE','SHOWROOM',i.lastupdated,getdate(),i.id,@Potype

purchaseorderentry(purchaseorderid,itemdescription,itemid,lastupdated,storeid,quantityordered,price)

formatting link
5718d4-e784-44a2-910a-6b6a5f825624&dg=microsoft.public.pos

Reply to
Afshin Alikhani

BeanSmart website is not affiliated with any of the manufacturers or service providers discussed here. All logos and trade names are the property of their respective owners.