Wrox Programmer Forums
|
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 Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old December 24th, 2007, 07:37 AM
Authorized User
 
Join Date: Oct 2007
Posts: 46
Thanks: 1
Thanked 0 Times in 0 Posts
Send a message via MSN to debbiecoates
Default 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



 
Old December 24th, 2007, 07:42 AM
Authorized User
 
Join Date: Oct 2007
Posts: 46
Thanks: 1
Thanked 0 Times in 0 Posts
Send a message via MSN to debbiecoates
Default

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?

 
Old December 24th, 2007, 07:50 AM
Imar's Avatar
Wrox Author
 
Join Date: Jun 2003
Posts: 17,089
Thanks: 80
Thanked 1,576 Times in 1,552 Posts
Default

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.
 
Old December 24th, 2007, 08:00 AM
Authorized User
 
Join Date: Oct 2007
Posts: 46
Thanks: 1
Thanked 0 Times in 0 Posts
Send a message via MSN to debbiecoates
Default

i have sorted it, I had single quotes around my @ProjectID, I have no brain today its Christmas eve !!

Many Thanks






Similar Threads
Thread Thread Starter Forum Replies Last Post
trigger to insert current date on insert kev_79 SQL Server 2000 3 January 23rd, 2006 05:58 PM
insert,update trigger rolle SQL Server 2000 3 September 29th, 2005 08:41 PM
Insert Trigger Arsi SQL Server 2000 2 February 25th, 2005 05:41 PM
Create Insert Trigger byron SQL Server 2000 6 November 19th, 2003 11:30 AM
Insert Update Trigger mstuart60 SQL Server 2000 10 September 30th, 2003 06:54 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.