Please start any new threads on our new
site at https://forums.sqlteam.com. We've got lots of great SQL Server
experts to answer whatever question you can come up with.
Author |
Topic |
blocker
Yak Posting Veteran
89 Posts |
Posted - 2010-04-06 : 22:06:48
|
This is an update of my post for trigger creation. I am having problem regarding on after insert trigger. I want to get the data being inserted like the prodcode, If prodcode is equal to prodcode to productlist table, the qty of the product in productlist will be added. If the prodcode inserted is not found in prodoct list table, i will inserted the product info in productlist table. Here is my trigger.USE [MFR_Merchandise]GO/****** Object: Trigger [dbo].[markstatus_PO] Script Date: 04/07/2010 08:38:30 ******/SET ANSI_NULLS ONGOSET QUOTED_IDENTIFIER ONGO-- =============================================-- Author: <Author,,Name>-- Create date: <Create Date,,>-- Description: <Description,,>-- =============================================CREATE TRIGGER [dbo].[updateinsert_DONEPO] ON [dbo].[PO_detail] AFTER INSERTAS BEGINSET NOCOUNT ON; IF exists(SELECT tbl_product_list.* FROM tbl_product_list WHERE tbl_product_list.productcode = INSERTED.prodcode AND INSERTED.statusx='Done') BEGIN UPDATE tbl_product_list SET purchased=(purchased + i.qty) WHERE productcode=i.prodcodeEND ELSEBEGININSERT INTO tbl_product_list([productcode],[description],[category],[packtype],[supplierprice],[srp],[begininvent],[onhand],[datetimeentered],[enteredby],[status],[supplierid],[suppliername]) VALUES (INSERTED.prodcode,INSERTED.proddesc,INSERTED.unit,INSERTED.packtype,INSERTED.price,INSERTED.srp,INSERTED.qty,INSERTED.qty,INSERTED.podate,INSERTED.addedby,INSERTED.statusx,INSERTED.supplierid,INSERTED.supplier)END END GO===================================================After compilation it error ouccurs like below.Msg 4104, Level 16, State 1, Procedure updateinsert_DONEPO, Line 17The multi-part identifier "INSERTED.prodcode" could not be bound.Msg 4104, Level 16, State 1, Procedure updateinsert_DONEPO, Line 17The multi-part identifier "INSERTED.statusx" could not be bound.Msg 4104, Level 16, State 1, Procedure updateinsert_DONEPO, Line 20The multi-part identifier "INSERTED.prodcode" could not be bound.Thank you guys for giving time. God bless |
|
tkizer
Almighty SQL Goddess
38200 Posts |
|
tkizer
Almighty SQL Goddess
38200 Posts |
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2010-04-07 : 00:31:12
|
it should beCREATE TRIGGER [dbo].[updateinsert_DONEPO] ON [dbo].[PO_detail]AFTER INSERTAS BEGINSET NOCOUNT ON;IF exists(SELECT 1 FROM tbl_product_list t JOIN INSERTED i ON t.productcode = i.prodcode AND i.statusx='Done')BEGINUPDATE t SET t.purchased=t.purchased + i.qtyFROM tbl_product_list tJOIN INSERTED iON t.productcode=i.prodcodeENDELSEBEGININSERT INTO tbl_product_list([productcode],[description],[category],[packtype],[supplierprice],[srp],[begininvent],[onhand],[datetimeentered],[enteredby],[status],[supplierid],[suppliername])SELECT i.prodcode,i.proddesc,i.unit,i.packtype,i.price,i.srp,i.qty,i.qty,i.podate,i.addedby,i.statusx,i.supplierid,i.supplierFROM INSERTED iEND END GO ------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/ |
|
|
blocker
Yak Posting Veteran
89 Posts |
Posted - 2010-04-07 : 02:09:04
|
thank you very much guys as in.this works. How should i give credit.?God bless us all. |
|
|
Kristen
Test
22859 Posts |
Posted - 2010-04-07 : 03:51:40
|
"How should i give credit."Hahaha .... this is a stone-age forum and we don't have / need such new fangled widgets, but its a nice thought, thanks |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2010-04-07 : 04:03:32
|
quote: Originally posted by blocker thank you very much guys as in.this works. How should i give credit.?God bless us all.
Your thanks post itself is the credit ------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/ |
|
|
tkizer
Almighty SQL Goddess
38200 Posts |
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
|
tkizer
Almighty SQL Goddess
38200 Posts |
|
chalton
Starting Member
2 Posts |
Posted - 2012-04-29 : 20:01:12
|
Hi!I have a user defined function that returns a complete path (using a recursive query) based on a ID. The function returns a table with that ID on a column and a path on another.This function works ok when used with a direct integer on the parameters.Now I`m trying to get results based on IDs from INSERTED table.But show this error:Msg 4104, Level 16, State 1, Procedure dbo.T_Upd_MapPath, Line 16The multi-part identifier "INSERTED.ID" could not be bound.I understood explanation about Join at this case, and can`t figure it out how put that with a function. The many ways tried to rewrite I`ve only got errors.Below is the function that works:CREATE FUNCTION dbo.F_MapPath]( @ID int = 1)RETURNS @MyTable TABLE (ID INT, CompletePath nvarchar(1024))ASBEGIN DECLARE @Parent INT SET @Parent = (SELECT PARENT FROM dbo.FolderAttachment WHERE ID = @ID); WITH Tabela(ID, Parent, Name, Path) AS (SELECT ID, Parent, Name, CAST(Name AS nvarchar(1024)) FROM dbo.FolderAttachement WHERE ID = @ID UNION ALL SELECT tb.ID, PA.Parent, PA.Name, CAST(PA.Name + '\' + tb.CompletePath AS nvarchar(1024)) FROM dbo.FolderAttachment AS PA INNER JOIN Tabela AS tb ON PA.ID = tb.Parent WHERE PA.Parent <= @Parent) INSERT @MYTable SELECT ID, Path FROM Tabela AS Tabela_1 WHERE Parent = 1 RETURNEND but on a trigger how can it be used?CREATE TRIGGER dbo.T_Upd_MapPath ON dbo.FolderAttachment AFTER INSERT, DELETE, UPDATEAS BEGIN DELETE FROM dbo.MapPath WHERE (SELECT Id FROM DELETED) = Id INSERT INTO dbo.MapPath (Id, Path) SELECT * FROM dbo.F_MapPath(INSERTED.ID)ENDGO |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2012-04-29 : 21:57:56
|
[code]CREATE TRIGGER dbo.T_Upd_MapPath ON dbo.FolderAttachment AFTER INSERT, DELETE, UPDATEAS BEGIN DELETE FROM dbo.MapPath WHERE (SELECT Id FROM DELETED) = Id INSERT INTO dbo.MapPath (Id, Path) SELECT f.* FROM INSERTED i CROSS APPLY dbo.F_MapPath(i.ID) fENDGO[/code]In future, please post questions as a new thread rather than hijacking an earlier thread------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/ |
|
|
chalton
Starting Member
2 Posts |
Posted - 2012-05-02 : 02:36:27
|
I`m sorry about that, and Very Thank you!! quote: Originally posted by visakh16
CREATE TRIGGER dbo.T_Upd_MapPath ON dbo.FolderAttachment AFTER INSERT, DELETE, UPDATEAS BEGIN DELETE FROM dbo.MapPath WHERE (SELECT Id FROM DELETED) = Id INSERT INTO dbo.MapPath (Id, Path) SELECT f.* FROM INSERTED i CROSS APPLY dbo.F_MapPath(i.ID) fENDGO In future, please post questions as a new thread rather than hijacking an earlier thread------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/
|
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2012-05-02 : 19:42:54
|
welcome------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/ |
|
|
|
|
|
|
|