p2p.wrox.com Forums

Need to download code?

View our list of code downloads.


Go Back   p2p.wrox.com Forums > SQL Server > SQL Server 2000 > SQL Server 2000
I forgot my password Register Now
Register | FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read
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.

Reply
 
Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old December 24th, 2007, 07:37 AM
Authorized User
Points: 188, Level: 3
Points: 188, Level: 3 Points: 188, Level: 3 Points: 188, Level: 3
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Oct 2007
Location: doncaster, , United Kingdom.
Posts: 41
Thanks: 0
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



Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Reddit!
Reply With Quote
  #2 (permalink)  
Old December 24th, 2007, 07:42 AM
Authorized User
Points: 188, Level: 3
Points: 188, Level: 3 Points: 188, Level: 3 Points: 188, Level: 3
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Oct 2007
Location: doncaster, , United Kingdom.
Posts: 41
Thanks: 0
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?

Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Reddit!
Reply With Quote
  #3 (permalink)  
Old December 24th, 2007, 07:50 AM
Imar's Avatar
Wrox Author
Points: 33,554, Level: 80
Points: 33,554, Level: 80 Points: 33,554, Level: 80 Points: 33,554, Level: 80
Activity: 100%
Activity: 100% Activity: 100% Activity: 100%
 
Join Date: Jun 2003
Location: Utrecht, Netherlands.
Posts: 10,228
Thanks: 7
Thanked 203 Times in 201 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.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Reddit!
Reply With Quote
  #4 (permalink)  
Old December 24th, 2007, 08:00 AM
Authorized User
Points: 188, Level: 3
Points: 188, Level: 3 Points: 188, Level: 3 Points: 188, Level: 3
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Oct 2007
Location: doncaster, , United Kingdom.
Posts: 41
Thanks: 0
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

Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Reddit!
Reply With Quote
Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off
Forum Jump

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 09: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 07:54 AM



All times are GMT -4. The time now is 12:18 AM.


Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
© 2008 Wiley Publishing, Inc