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.

 All Forums
 SQL Server 2008 Forums
 Transact-SQL (2008)
 AFTER INSERTED TRIGGER ERROR...

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 ON
GO

SET QUOTED_IDENTIFIER ON
GO

-- =============================================
-- Author: <Author,,Name>
-- Create date: <Create Date,,>
-- Description: <Description,,>
-- =============================================
CREATE TRIGGER [dbo].[updateinsert_DONEPO]
ON [dbo].[PO_detail]
AFTER INSERT
AS
BEGIN

SET 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.prodcode
END

ELSE
BEGIN
INSERT 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 17
The multi-part identifier "INSERTED.prodcode" could not be bound.
Msg 4104, Level 16, State 1, Procedure updateinsert_DONEPO, Line 17
The multi-part identifier "INSERTED.statusx" could not be bound.
Msg 4104, Level 16, State 1, Procedure updateinsert_DONEPO, Line 20
The multi-part identifier "INSERTED.prodcode" could not be bound.


Thank you guys for giving time. God bless

tkizer
Almighty SQL Goddess

38200 Posts

Posted - 2010-04-06 : 22:08:30
You have to JOIN to the inserted trigger table. I'm pressed for time at the moment (screaming baby), but you need to add a JOIN to the SELECT, UPDATE, and INSERT. Also, you need to use INSERT/SELECT instead of INSERT/VALUES.

Here's an example of a trigger with a JOIN to the inserted trigger table (from my blog): http://weblogs.sqlteam.com/tarad/archive/2004/09/14/2077.aspx

Tara Kizer
Microsoft MVP for Windows Server System - SQL Server
http://weblogs.sqlteam.com/tarad/

Subscribe to my blog
Go to Top of Page

tkizer
Almighty SQL Goddess

38200 Posts

Posted - 2010-04-06 : 22:09:42
Also, I edited your post to add a carriage return to your INSERT statement for display purposes in a browser. I'm a moderator here. Lines that are too long inside of code tags cause horizontal bars in a browser which is highly undesirable.

Tara Kizer
Microsoft MVP for Windows Server System - SQL Server
http://weblogs.sqlteam.com/tarad/

Subscribe to my blog
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2010-04-07 : 00:31:12
it should be

CREATE TRIGGER [dbo].[updateinsert_DONEPO]
ON [dbo].[PO_detail]
AFTER INSERT
AS
BEGIN

SET NOCOUNT ON;

IF exists(SELECT 1 FROM tbl_product_list t
JOIN INSERTED i
ON t.productcode = i.prodcode
AND i.statusx='Done')

BEGIN
UPDATE t
SET t.purchased=t.purchased + i.qty
FROM tbl_product_list t
JOIN INSERTED i
ON t.productcode=i.prodcode
END

ELSE
BEGIN
INSERT 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.supplier
FROM INSERTED i
END
END

GO


------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/

Go to Top of Page

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.
Go to Top of Page

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
Go to Top of Page

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 MVP
http://visakhm.blogspot.com/

Go to Top of Page

tkizer
Almighty SQL Goddess

38200 Posts

Posted - 2010-04-07 : 16:52:46
visakh, please do not post such wide queries. Moderators have to fix them to avoid horizontal bars. I edited your post to add a carriage return to fix it.

Tara Kizer
Microsoft MVP for Windows Server System - SQL Server
http://weblogs.sqlteam.com/tarad/

Subscribe to my blog
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2010-04-08 : 01:11:40
quote:
Originally posted by tkizer

visakh, please do not post such wide queries. Moderators have to fix them to avoid horizontal bars. I edited your post to add a carriage return to fix it.

Tara Kizer
Microsoft MVP for Windows Server System - SQL Server
http://weblogs.sqlteam.com/tarad/

Subscribe to my blog


Oops Sorry...I will add carriage returns in future to make sure they're not too long

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/

Go to Top of Page

tkizer
Almighty SQL Goddess

38200 Posts

Posted - 2010-04-08 : 13:22:12
Thx! It's a real pain to edit people's posts.

Tara Kizer
Microsoft MVP for Windows Server System - SQL Server
http://weblogs.sqlteam.com/tarad/

Subscribe to my blog
Go to Top of Page

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 16
The 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))
AS
BEGIN
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

RETURN
END



but on a trigger how can it be used?


CREATE TRIGGER dbo.T_Upd_MapPath
ON dbo.FolderAttachment
AFTER INSERT, DELETE, UPDATE
AS
BEGIN
DELETE FROM dbo.MapPath
WHERE (SELECT Id FROM DELETED) = Id

INSERT INTO dbo.MapPath (Id, Path)
SELECT * FROM dbo.F_MapPath(INSERTED.ID)
END
GO
Go to Top of Page

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, UPDATE
AS
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) f
END
GO
[/code]

In future, please post questions as a new thread rather than hijacking an earlier thread

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/

Go to Top of Page

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, UPDATE
AS
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) f
END
GO


In future, please post questions as a new thread rather than hijacking an earlier thread

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/



Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2012-05-02 : 19:42:54
welcome

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/

Go to Top of Page
   

- Advertisement -