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 2005 Forums
 Transact-SQL (2005)
 Need help with simple trigger

Author  Topic 

icw
Constraint Violating Yak Guru

378 Posts

Posted - 2010-06-29 : 16:39:13
Hi
Can anyone point out what's wrong with my trigger.
It seemed to install OK but doens't seem to do what i want it to.

What i want is that everytime I insert a new entry in the "Journal" table it copies the value from a field in the journal called "Entrytext2" in to a field called "LatestJournal" in a table called "Calllog" (there is a 1:m relationship between calllog and journal)
my trigger is below.

-- Create date: <20/09/09>
-- Description: <Trigger to Copy the latest Journal into the "LatestJournal" field in calllog Table>
-- =============================================
CREATE TRIGGER [dbo].[NewJournalCopy] ON Nuneaton.dbo.[Journal]
FOR INSERT

AS

Declare @CalliD Varchar(8)
Declare @NewJournalStr Varchar (8000)

--Select the Callid ofthe entry just inserted into Journal

Select @callid=Callid
FROM INserted


IF @Callid IS NOT NULL
Begin


--Update CallLog with the text

UPDATE Nuneaton.dbo.[Calllog]
SET LatestJournal = @NewJournalStr
Where CallID = @CallID


END

icw
Constraint Violating Yak Guru

378 Posts

Posted - 2010-06-29 : 16:48:00
I noticed i didn't populate one of my parameters. So I've doen that now but it still doesn't work.

see below for new code

Alter TRIGGER [dbo].[NewJournalCopy] ON Nuneaton.dbo.[Journal]
FOR INSERT

AS

Declare @CalliD Varchar(8)
Declare @NewJournalStr Varchar (8000)

--Select the Callid ofthe entry just inserted into Journal

Select @callid=Callid
FROM INserted

SELECT @NewJournalStr = Entrytext2
FROM Nuneaton.dbo.[Journal]
WHERE CallID = @CallID



IF @Callid IS NOT NULL
Begin


--Update CallLog with the text

UPDATE Nuneaton.dbo.[Calllog]
SET LatestJournal = @NewJournalStr
Where CallID = @CallID


END

Go to Top of Page

icw
Constraint Violating Yak Guru

378 Posts

Posted - 2010-06-29 : 16:49:59
Actually it is working, i just needed to refresh my screen
Go to Top of Page

X002548
Not Just a Number

15586 Posts

Posted - 2010-06-29 : 16:55:20
[code]
/*
What i want is that everytime I insert a new entry in the "Journal" table

Copy the value from a field in the journal called "Entrytext2"
in to a field called "LatestJournal" in a table called "Calllog"
*/

CREATE TRIGGER [dbo].[NewJournalCopy] ON Nuneaton.dbo.[Journal]
FOR INSERT
AS

INSERT INTO Calllog(LatestJournal)
SELECT Entrytext2 FROM inserted
GO
[/code]


Brett

8-)

Hint: Want your questions answered fast? Follow the direction in this link
http://weblogs.sqlteam.com/brettk/archive/2005/05/25/5276.aspx

Add yourself!
http://www.frappr.com/sqlteam



Go to Top of Page
   

- Advertisement -