|
|
 |
| SQL Server 2000 General discussion of Microsoft SQL Server -- for topics that don't fit in one of the more specific SQL Server forums. version 2000 only. There's a new forum for SQL Server 2005. |
Welcome to the p2p.wrox.com Forums.
You are currently viewing the SQL Server 2000 section of the Wrox p2p Programmer to Programmer discussion community. This is a community of more than 40,000 computer programmers including Wrox book authors and readers. As a guest, you can read any forum posting. By joining our free Wrox p2p community you can post your own programming questions and respond to other programmers’ questions. Registered users also don't have to see the ads that are displayed to guests. Registration is fast, simple and absolutely free so please, join today!
Join today and post to win prizes! Post more to increase your chances of being Wrox’s top poster of the month.
|
 |

December 24th, 2007, 07:37 AM
|
|
Authorized User
|
|
Join Date: Oct 2007
Location: doncaster, , United Kingdom.
Posts: 41
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
Insert Trigger
I have created a trigger that i want to fire everytime a new record is inserted,
this is the trigger
CREATE TRIGGER NewProjectInserted
ON project
FOR INSERT
AS
DECLARE @PROJECTID BIGINT,
@COUNTRYID INT
SELECT @PROJECTID = i.ProjectID,
@COUNTRYID = i.CountryID
FROM inserted i
IF (@COUNTRYID = 1)
BEGIN
SET NOCOUNT ON;INSERT INTO Project (ProjectID, Organisation, ProjectName, ProjectTypeID, UserID) VALUES INSERT INTO
FUNDALLOCATION
(FundSourceID, ProjectID, Allocation )
VALUES
('1', '@PROJECTID', '100')
END
IF (@COUNTRYID = 2)
BEGIN
INSERT INTO
FUNDALLOCATION
(FundSourceID, ProjectID, Allocation )
VALUES
('2', '@PROJECTID', '100')
END
IF (@COUNTRYID = 3)
BEGIN
INSERT INTO
FUNDALLOCATION
(FundSourceID, ProjectID, Allocation )
VALUES
('3', '@PROJECTID', '100')
END
However, when I run it i am getting the following error
Server: Msg 156, Level 15, State 1, Procedure NewProjectInserted, Line 16
Incorrect syntax near the keyword 'INSERT'.
and I cant see what is the problem. Can anyone spot what I am doing wrong?
many thanks
|

December 24th, 2007, 07:42 AM
|
|
Authorized User
|
|
Join Date: Oct 2007
Location: doncaster, , United Kingdom.
Posts: 41
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
Sorry, Ignore that, I have just spotted the obvious, I had pasted a bit of earlier code into mine without noticing, My code now looks like this
CREATE TRIGGER NewProjectInserted
ON project
FOR INSERT
AS
DECLARE @PROJECTID bigint,
@COUNTRYID INT
SELECT @PROJECTID = i.ProjectID,
@COUNTRYID = i.CountryID
FROM inserted i
IF (@COUNTRYID = 1)
BEGIN
INSERT INTO
FUNDALLOCATION
(FundSourceID, ProjectID, Allocation )
VALUES
('1', '@PROJECTID', '100')
END
IF (@COUNTRYID = 2)
BEGIN
INSERT INTO
FUNDALLOCATION
(FundSourceID, ProjectID, Allocation )
VALUES
('2', '@PROJECTID', '100')
END
IF (@COUNTRYID = 3)
BEGIN
INSERT INTO
FUNDALLOCATION
(FundSourceID, ProjectID, Allocation )
VALUES
('3', '@PROJECTID', '100')
END
Which seems to work fine, only when i have set the trigger and insert a new line into my projects table, i am getting this
error convertind datatype varchar to bigint, I can only presume this is happening within the trigger, as when i take the trigger off, it doesn't happen. Does anyone know why? I think I have declared my variables in the trigger OK?
|

December 24th, 2007, 07:50 AM
|
 |
Wrox Author
Points: 33,554, Level: 80 |
|
|
Join Date: Jun 2003
Location: Utrecht, Netherlands.
Posts: 10,228
Thanks: 7
Thanked 203 Times in 201 Posts
|
|
You're inserting @PROJECTID as a string literal; not as a variable. Drop the quotes from '@PROJECTID' and you should be good to go...
Imar
---------------------------------------
Imar Spaanjaars
http://Imar.Spaanjaars.Com
Everyone is unique, except for me.
Author of ASP.NET 2.0 Instant Results and Beginning Dreamweaver MX / MX 2004
Want to be my colleague? Then check out this post.
|

December 24th, 2007, 08:00 AM
|
|
Authorized User
|
|
Join Date: Oct 2007
Location: doncaster, , United Kingdom.
Posts: 41
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
i have sorted it, I had single quotes around my @ProjectID, I have no brain today its Christmas eve !!
Many Thanks
|
| Thread Tools |
Search this Thread |
|
|
|
| Display Modes |
Linear Mode
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
|
 |