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
 General SQL Server Forums
 New to SQL Server Programming
 Help with a Trigger

Author  Topic 

aharvestofhealth
Yak Posting Veteran

52 Posts

Posted - 2010-01-12 : 17:12:31
I just accomplished my first trigger! No problem getting it to work, however I do seem to get an error every time I try to make a change to the table from within my software that accesses the database. Here is the trigger:

CREATE TRIGGER UpdatePreqBrand_trigger ON PO_PREQ_LIN
AFTER INSERT
AS
BEGIN
update PO_PREQ_LIN
set PO_PREQ_LIN.COMMNT_1 = IM_ITEM.ADDL_DESCR_2
from PO_PREQ_LIN, IM_ITEM
where PO_PREQ_LIN.ITEM_NO = IM_ITEM.ITEM_NO and PREQ_NO IN (SELECT PREQ_NO FROM INSERTED);
END;

And the error I get when I try to make any changes (e.g. deleting an item):

Row cannot be located for updating. Some values may have been changed since it was last read
Row cannot be located for updating. Some values may have been changed since it was last read
[Insert]
[SELECT * FROM [PO_PREQ_LIN] WHERE (1=0) ]

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2010-01-13 : 01:16:58
whats the primary key of your table? is it an identity field?
Go to Top of Page

aharvestofhealth
Yak Posting Veteran

52 Posts

Posted - 2010-01-13 : 09:32:29
There are two primary keys in this table, and both are numbers. I'm not sure what an identity field is or how to find that out.
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2010-01-13 : 09:37:39
quote:
Originally posted by aharvestofhealth

There are two primary keys in this table, and both are numbers. I'm not sure what an identity field is or how to find that out.


there cant be 2 pks so i guess its composite pk that you meant

use below to see if any of column is an identity field in table


SELECT
c.TABLE_NAME
,c.COLUMN_NAME
,c.TABLE_CATALOG
,c.TABLE_SCHEMA
FROM
INFORMATION_SCHEMA.COLUMNS c
WHERE COLUMNPROPERTY(OBJECT_ID(c.TABLE_NAME)
,c.COLUMN_NAME,'IsIdentity') = 1
AND c.TABLE_NAME = 'yourtable'
Go to Top of Page

aharvestofhealth
Yak Posting Veteran

52 Posts

Posted - 2010-01-13 : 10:01:18
Ok, I replaced 'yourtable' with 'PO_PREQ_LIN' and the script ran successfully, however it returned no results.
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2010-01-13 : 10:35:32
quote:
Originally posted by aharvestofhealth

Ok, I replaced 'yourtable' with 'PO_PREQ_LIN' and the script ran successfully, however it returned no results.



that means you dont have identity field. did you code by any chance tried to update any of involved pk columns?
Go to Top of Page

Kristen
Test

22859 Posts

Posted - 2010-01-13 : 11:06:59
"And the error I get when I try to make any changes (e.g. deleting an item):"

This is an INSERT trigger, so won't fire on a DELETE (unless there is a Delete Trigger there which is trying to INSERT back again )

The error should indicate the line number but also the name of the object where the error occurred. Is that showing the name of the trigger (it will, helpfully, if that's where the error is occurring)
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2010-01-13 : 11:11:33
quote:
Originally posted by Kristen

"And the error I get when I try to make any changes (e.g. deleting an item):"

This is an INSERT trigger, so won't fire on a DELETE (unless there is a Delete Trigger there which is trying to INSERT back again )

The error should indicate the line number but also the name of the object where the error occurred. Is that showing the name of the trigger (it will, helpfully, if that's where the error is occurring)


error suggests
Row cannot be located for updating
which i suspect might be due to some update action
Go to Top of Page

vijayisonly
Master Smack Fu Yak Hacker

1836 Posts

Posted - 2010-01-13 : 11:11:35
My opinion is... this error is independent of the trigger..Can you disable the trigger and try and make a change to the table.
Go to Top of Page

Kristen
Test

22859 Posts

Posted - 2010-01-13 : 11:16:50
"error suggests
Row cannot be located for updating
which i suspect might be due to some update action
"

I reckon you are hedging on an Insert action then
Go to Top of Page

aharvestofhealth
Yak Posting Veteran

52 Posts

Posted - 2010-01-13 : 11:38:09
@visakh16
No, the script doesn't update any of the primary keys.

@vijayisonly
Yes, after disabling the trigger, I can make changes to the table just fine.

Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2010-01-13 : 11:39:43
quote:
Originally posted by aharvestofhealth

@visakh16
No, the script doesn't update any of the primary keys.

@vijayisonly
Yes, after disabling the trigger, I can make changes to the table just fine.





whats datatype of PO_PREQ_LIN.COMMNT_1 column?

SQL Server MVP
Go to Top of Page

vijayisonly
Master Smack Fu Yak Hacker

1836 Posts

Posted - 2010-01-13 : 11:45:20
quote:
Originally posted by aharvestofhealth

@visakh16
No, the script doesn't update any of the primary keys.

@vijayisonly
Yes, after disabling the trigger, I can make changes to the table just fine.





One other question, you are updating the same table in which you are inserting a value. Why not get the value from IM_ITEM during insert itself. Why do you need a trigger to update it after the INSERT has been done?
Go to Top of Page

Kristen
Test

22859 Posts

Posted - 2010-01-13 : 11:53:22
@vijayisonly: I'd be inclined to have that logic in a trigger, myself. Then nothing can get data into the data which isn't "valid" for the associated IM_ITEM column.

@aharvestofhealth: Although I would ask the questions:

What should happen if ADDL_DESCR_2 (in IM_ITEM) is subsequently changed? Does PO_PREQ_LIN.COMMNT_1 remain unchanged? (Given that the column is called "Comment" I reckon you are only trying to get a "This is the initial ADDL_DESCR_2" into it)

What about if there is no corresponding row in IM_ITEM when a row is inserted into PO_PREQ_LIN? and in that case what about when the first corresponding row is subsequently added in IM_ITEM?

What about if there are multiple corresponding rows in IM_ITEM when PO_PREQ_LIN is inserted? I reckon SQL will choose which one to use to update PO_PREQ_LIN.COMMNT_1 at "random"

Picky points only, I'm sure you've considered them.
Go to Top of Page

aharvestofhealth
Yak Posting Veteran

52 Posts

Posted - 2010-01-13 : 12:00:19
quote:

whats datatype of PO_PREQ_LIN.COMMNT_1 column?

SQL Server MVP



AlphaNumeric - basically the brand name of an item.

quote:

Why not get the value from IM_ITEM during insert itself? Why do you need a trigger to update it after the INSERT has been done?



The software that I use does not use the IM_ITEM table in the purchasing area. I need the field IM_ITEM.ADDL_DESCR_2 and the only way I found to do this is to copy it to the PO_PREQ_LIN.COMMNT_1. It works fine when I run the script by itself, but I want it to run every time I create a new purchase order.

This script works just fine alone just not with the trigger:

update PO_PREQ_LIN
set PO_PREQ_LIN.COMMNT_1 = IM_ITEM.ADDL_DESCR_2
from PO_PREQ_LIN, IM_ITEM
where PO_PREQ_LIN.ITEM_NO = IM_ITEM.ITEM_NO
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2010-01-13 : 12:04:59
quote:
Originally posted by aharvestofhealth

quote:

whats datatype of PO_PREQ_LIN.COMMNT_1 column?

SQL Server MVP



AlphaNumeric - basically the brand name of an item.

quote:

Why not get the value from IM_ITEM during insert itself? Why do you need a trigger to update it after the INSERT has been done?



The software that I use does not use the IM_ITEM table in the purchasing area. I need the field IM_ITEM.ADDL_DESCR_2 and the only way I found to do this is to copy it to the PO_PREQ_LIN.COMMNT_1. It works fine when I run the script by itself, but I want it to run every time I create a new purchase order.

This script works just fine alone just not with the trigger:

update PO_PREQ_LIN
set PO_PREQ_LIN.COMMNT_1 = IM_ITEM.ADDL_DESCR_2
from PO_PREQ_LIN, IM_ITEM
where PO_PREQ_LIN.ITEM_NO = IM_ITEM.ITEM_NO



do you mean varchar when you say it is AlphaNumeric?

SQL Server MVP
Go to Top of Page

aharvestofhealth
Yak Posting Veteran

52 Posts

Posted - 2010-01-13 : 12:10:11
quote:
Originally posted by Kristen

@vijayisonly: I'd be inclined to have that logic in a trigger, myself. Then nothing can get data into the data which isn't "valid" for the associated IM_ITEM column.

@aharvestofhealth: Although I would ask the questions:

What should happen if ADDL_DESCR_2 (in IM_ITEM) is subsequently changed? Does PO_PREQ_LIN.COMMNT_1 remain unchanged? (Given that the column is called "Comment" I reckon you are only trying to get a "This is the initial ADDL_DESCR_2" into it)

What about if there is no corresponding row in IM_ITEM when a row is inserted into PO_PREQ_LIN? and in that case what about when the first corresponding row is subsequently added in IM_ITEM?

What about if there are multiple corresponding rows in IM_ITEM when PO_PREQ_LIN is inserted? I reckon SQL will choose which one to use to update PO_PREQ_LIN.COMMNT_1 at "random"

Picky points only, I'm sure you've considered them.



I don't think anything would happen if ADDL_DESCR_2 would be changed. I need to see a brand name for my items, and the ADDL_DESCR_2 is the field that holds that information. I just figured the only way to see it would be to get it into the PO_PREQ_LIN, and the PO_PREQ_LIN.COMMNT_1 should hold that information just fine. I'm a little new to all of this, so I'm not sure how to exactly answer the questions about the corresponding rows in IM_ITEM.
Go to Top of Page

Kristen
Test

22859 Posts

Posted - 2010-01-13 : 12:12:06
OK, clearly no need to worry about that just now then. Keep it in the back of your mind for a future refinement, if the need arises.
Go to Top of Page

aharvestofhealth
Yak Posting Veteran

52 Posts

Posted - 2010-01-13 : 12:13:09
quote:
Originally posted by visakh16
do you mean varchar when you say it is AlphaNumeric?



Yes, sorry, I mean varchar.
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2010-01-13 : 12:25:30
were you trying to do data changes from application?
see below it seems like error with ado recordset

http://support.microsoft.com/kb/193515

SQL Server MVP
Go to Top of Page

aharvestofhealth
Yak Posting Veteran

52 Posts

Posted - 2010-01-13 : 12:34:46
quote:
Originally posted by visakh16

were you trying to do data changes from application?
see below it seems like error with ado recordset

http://support.microsoft.com/kb/193515

SQL Server MVP



Yes, I am trying to make changes from within the application. I don't get the error until I save the changes. The document you linked to is confusing for me as I am not that good with SQL yet.
Go to Top of Page
    Next Page

- Advertisement -