dbTalk Databases Forums  

Error when saving a trigger

comp.databases.ms-sqlserver comp.databases.ms-sqlserver


Discuss Error when saving a trigger in the comp.databases.ms-sqlserver forum.



Reply
 
Thread Tools Display Modes
  #1  
Old   
Markei54545
 
Posts: n/a

Default Error when saving a trigger - 08-09-2010 , 04:54 AM






Hi all,

I have created the following Trigger. It is a test as I have not had
to do this before. What I am trying to achieve is to trigger when a
record is inserted, and then to display a value from that inserted row
(in this case the document number). However, I get the error

"The multi-part identifier "SOPOrderReturn.DocumentNo" could not be
bound"

Any ideas? Any help gratefully received.

Mark


Create TRigger Test

On SOPOrderReturn

For Insert

as

DECLARE @DocNo nvarchar(20)

SET @DocNo = SOPOrderReturn.DocumentNo

RAISERROR ('Test Error Message', 16, 1)

Reply With Quote
  #2  
Old   
Erland Sommarskog
 
Posts: n/a

Default Re: Error when saving a trigger - 08-09-2010 , 06:46 AM






Markei54545 (mark.blackall (AT) gmail (DOT) com) writes:
Quote:
I have created the following Trigger. It is a test as I have not had
to do this before. What I am trying to achieve is to trigger when a
record is inserted, and then to display a value from that inserted row
(in this case the document number). However, I get the error

"The multi-part identifier "SOPOrderReturn.DocumentNo" could not be
bound"

Any ideas? Any help gratefully received.

Mark


Create TRigger Test

On SOPOrderReturn

For Insert

as

DECLARE @DocNo nvarchar(20)

SET @DocNo = SOPOrderReturn.DocumentNo

RAISERROR ('Test Error Message', 16, 1)
That's what you get for making up your syntax, rather than using Books
Online.

A trigger is no different from anything else, you cannot refer to column
values without a FROM clause. What is special in a trigger you have access
to two virtual tables: inserted and deleted. The first holds the inserted
rows, and in case of an UPDATE trigger, the afterimage of the updated rows.
The table deleted holds the rows by a DELETE statement and in an UPDATE
trigger, the before-image of the updated rows.

Note the use of plural in the previous parapgrah. A trigger fires once per
statement, and thus these table can hold mutiple rows, and reading a value
into variable is probably an error.

As for you want to do, triggers are intended for integrity checks and
cascading updates. While you can produce a result set from a trigger,
this is deprecated, and it may not work at all depending on a configuration
parameter.

--
Erland Sommarskog, SQL Server MVP, esquel (AT) sommarskog (DOT) se

Books Online for SQL Server 2005 at
http://www.microsoft.com/technet/pro...ads/books.mspx
Books Online for SQL Server 2000 at
http://www.microsoft.com/sql/prodinf...ons/books.mspx

Reply With Quote
  #3  
Old   
Iain Sharp
 
Posts: n/a

Default Re: Error when saving a trigger - 08-09-2010 , 06:52 AM



1. The inserted trigger operates after all the inserts are done, so if
you insert more than one document, this will fail.

2. Use the 'special' table name 'inserted' to represent the new data.
See below

Iain


Create TRigger Test

On SOPOrderReturn

For Insert

as

DECLARE @DocNo nvarchar(20)

SET @DocNo = (select top 1 DocumentNo from inserted)

RAISERROR ('Test Error Message', 16, 1)


On Mon, 9 Aug 2010 02:54:44 -0700 (PDT), Markei54545
<mark.blackall (AT) gmail (DOT) com> wrote:

Quote:
Hi all,

I have created the following Trigger. It is a test as I have not had
to do this before. What I am trying to achieve is to trigger when a
record is inserted, and then to display a value from that inserted row
(in this case the document number). However, I get the error

"The multi-part identifier "SOPOrderReturn.DocumentNo" could not be
bound"

Any ideas? Any help gratefully received.

Mark


Create TRigger Test

On SOPOrderReturn

For Insert

as

DECLARE @DocNo nvarchar(20)

SET @DocNo = SOPOrderReturn.DocumentNo

RAISERROR ('Test Error Message', 16, 1)


Reply With Quote
  #4  
Old   
Markei54545
 
Posts: n/a

Default Re: Error when saving a trigger - 08-09-2010 , 07:28 AM



On 9 Aug, 12:52, Iain Sharp <ia... (AT) pciltd (DOT) co.uk> wrote:
Quote:
1. The inserted trigger operates after all the inserts are done, so if
you insert more than one document, this will fail.

2. Use the 'special' table name 'inserted' to represent the new data.
See below

Iain

Create TRigger Test

On SOPOrderReturn

For Insert

as

DECLARE @DocNo nvarchar(20)

SET @DocNo = (select top 1 DocumentNo from inserted)

RAISERROR ('Test Error Message', 16, 1)

On Mon, 9 Aug 2010 02:54:44 -0700 (PDT), Markei54545



mark.black... (AT) gmail (DOT) com> wrote:

Hi all,

I have created the following Trigger. It is a test as I have not had
to do this before. What I am trying to achieve is to trigger when a
record is inserted, and then to display a value from that inserted row
(in this case the document number). However, I get the error

"The multi-part identifier "SOPOrderReturn.DocumentNo" could not be
bound"

Any ideas? Any help gratefully received.

Mark

Create TRigger Test

On SOPOrderReturn

For Insert

as

DECLARE @DocNo nvarchar(20)

SET @DocNo = SOPOrderReturn.DocumentNo

RAISERROR ('Test Error Message', 16, 1)- Hide quoted text -

- Show quoted text -
Thanks guys, that worked well.

Reply With Quote
Reply




Thread Tools
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 On
HTML code is Off



Powered by vBulletin Version 3.5.3
Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.