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
 SQL Server 2005 Forums
 Transact-SQL (2005)
 I dont think i know triggers

Author  Topic 

jhermiz

3564 Posts

Posted - 2008-10-02 : 10:54:57
Or im going crazy cause I tried every combination.
The company I work for bought some software called sugarcrm.
Well of course they wanted to make a modification and we cant change
the interface and the only thing I can add to it is triggers...(havent done this in quite some time). Anyhow I cant figure out why a second update is not working in a trigger but it works in a seperate SSMS "New Query" window.

Basically our business dev folks wanted a way to add a quote number value and store it in a field. Then they want that value to be stored in another field in another table. So I thought ok I guess I can do it via a trigger. The first update works fine, the trigger fires and it updates the table I want it to update. Here was the original trigger:


ALTER TRIGGER [dbo].[upTest]
ON [dbo].[opportunities_cstm]
AFTER UPDATE, INSERT
AS
BEGIN

SET NOCOUNT ON;

DECLARE @TheMax integer
SELECT @TheMax = MAX (quote_id_c) + 1 FROM opportunities_cstm

UPDATE O
SET O.quote_id_c=@TheMax
FROM
opportunities_cstm O
join
inserted i
on
O.id_c = i.id_c
WHERE O.quote_id_c IS NULL
END


Simple straight forward, dirty hack but worked. Then they said they wanted it to update a field in another table with the same value. Well I decided to do a test and hardcode it for a row that I know exists. So I added to this code:


ALTER TRIGGER [dbo].[upTest]
ON [dbo].[opportunities_cstm]
AFTER UPDATE, INSERT
AS
BEGIN

SET NOCOUNT ON;

DECLARE @TheMax integer
SELECT @TheMax = MAX (quote_id_c) + 1 FROM opportunities_cstm

UPDATE O
SET O.quote_id_c=@TheMax
FROM
opportunities_cstm O
join
inserted i
on
O.id_c = i.id_c
WHERE O.quote_id_c IS NULL

UPDATE opportunities SET name='tryme2'
WHERE id = 'd6204c18-c33d-5f61-362e-48e4d51a1628'


END



But it never does the update at least not in the trigger. If I take that same update (the second one in bold above) in a new query window it works just fine. Remind me is my syntax correct for a trigger? What am I missing here. Sorry for being stupid, I know its not monday!@

Weblog -- [url]http://weblogs.sqlteam.com/jhermiz[/url]

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2008-10-02 : 13:57:22
you mean only last update or both updates? even with hardcoded id?
Go to Top of Page

jhermiz

3564 Posts

Posted - 2008-10-02 : 14:26:52
quote:
Originally posted by visakh16

you mean only last update or both updates? even with hardcoded id?



The first update works fine, the second one does not execute in a trigger. If I place it in a seperate SSMS query window and execute it it runs fine...

No clue why it isnt running. Is there anything wrong with my syntax?

Weblog -- [url]http://weblogs.sqlteam.com/jhermiz[/url]
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2008-10-02 : 14:32:26
quote:
Originally posted by jhermiz

quote:
Originally posted by visakh16

you mean only last update or both updates? even with hardcoded id?



The first update works fine, the second one does not execute in a trigger. If I place it in a seperate SSMS query window and execute it it runs fine...

No clue why it isnt running. Is there anything wrong with my syntax?

Weblog -- [url]http://weblogs.sqlteam.com/jhermiz[/url]


cant find anything wrong with syntax
Go to Top of Page

SwePeso
Patron Saint of Lost Yaks

30421 Posts

Posted - 2008-10-02 : 15:07:29
Have you tried SQL Profiler to see exactly what statements that are run?
Can you see the second update in the trigger?



E 12°55'05.63"
N 56°04'39.26"
Go to Top of Page

SwePeso
Patron Saint of Lost Yaks

30421 Posts

Posted - 2008-10-02 : 15:10:23
Is there another trigger on opportunities table?
If so, do you have cascading triggers disabled?



E 12°55'05.63"
N 56°04'39.26"
Go to Top of Page

X002548
Not Just a Number

15586 Posts

Posted - 2008-10-02 : 15:31:14
Why null instead of the key?

Heck the whole table could have null in that column, not just the inserted value...and what's with the hard coded id...you sure it's there for the second update?

Never mind the transactional timing thing for the MAX value

Put out some sample data, ddl and expect results



Brett

8-)

Hint: Want your questions answered fast? Follow the direction in this link
http://weblogs.sqlteam.com/brettk/archive/2005/05/25/5276.aspx

Add yourself!
http://www.frappr.com/sqlteam



Go to Top of Page

jhermiz

3564 Posts

Posted - 2008-10-03 : 09:52:35
quote:
Originally posted by Peso

Have you tried SQL Profiler to see exactly what statements that are run?
Can you see the second update in the trigger?



E 12°55'05.63"
N 56°04'39.26"




Yes I have tried profiler and it does display both update statements.
If I place that update in a "New Query" window it works fine...


Weblog -- [url]http://weblogs.sqlteam.com/jhermiz[/url]
Go to Top of Page

jhermiz

3564 Posts

Posted - 2008-10-03 : 09:58:32
quote:
Originally posted by Peso

Is there another trigger on opportunities table?
If so, do you have cascading triggers disabled?



E 12°55'05.63"
N 56°04'39.26"




There is no other triggers in this entire db.
So no.

Weblog -- [url]http://weblogs.sqlteam.com/jhermiz[/url]
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2008-10-03 : 09:59:47
quote:
Originally posted by jhermiz

quote:
Originally posted by Peso

Have you tried SQL Profiler to see exactly what statements that are run?
Can you see the second update in the trigger?



E 12°55'05.63"
N 56°04'39.26"




Yes I have tried profiler and it does display both update statements.
If I place that update in a "New Query" window it works fine...


Weblog -- [url]http://weblogs.sqlteam.com/jhermiz[/url]


and you're sure no other triggers exists on oppurtunities table for UPDATE action -either AFTER or INSTEAD OF?
Go to Top of Page

jhermiz

3564 Posts

Posted - 2008-10-03 : 10:00:34
quote:
Originally posted by X002548

Why null instead of the key?

Heck the whole table could have null in that column, not just the inserted value...and what's with the hard coded id...you sure it's there for the second update?

Never mind the transactional timing thing for the MAX value

Put out some sample data, ddl and expect results



Brett

8-)

Hint: Want your questions answered fast? Follow the direction in this link
http://weblogs.sqlteam.com/brettk/archive/2005/05/25/5276.aspx

Add yourself!
http://www.frappr.com/sqlteam





They purchased this CRM system so I cant change the fields. The only thing I can do is write up some triggers / procedures to help them get where they want to be. Yes that ID is in there, as I said if I run the update in a new query window it works fine in SSMS.
But the issue is I need it to fire right after that first update. I even went as far as calling a stored procedure to do the update there and it doesn't do the update. But if I execute that stored procedure in a "new query" window it works fine. Profile shows that the trigger executes...

A mystery.


Weblog -- [url]http://weblogs.sqlteam.com/jhermiz[/url]
Go to Top of Page

jhermiz

3564 Posts

Posted - 2008-10-03 : 10:03:06
quote:
Originally posted by visakh16

quote:
Originally posted by jhermiz

quote:
Originally posted by Peso

Have you tried SQL Profiler to see exactly what statements that are run?
Can you see the second update in the trigger?



E 12°55'05.63"
N 56°04'39.26"




Yes I have tried profiler and it does display both update statements.
If I place that update in a "New Query" window it works fine...


Weblog -- [url]http://weblogs.sqlteam.com/jhermiz[/url]


and you're sure no other triggers exists on oppurtunities table for UPDATE action -either AFTER or INSTEAD OF?



No triggers whatsoever...

Weblog -- [url]http://weblogs.sqlteam.com/jhermiz[/url]
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2008-10-03 : 10:22:02
And are you also sure you're looking at table in right schema and not in same table existing in some other schema..

Go to Top of Page

TG
Master Smack Fu Yak Hacker

6065 Posts

Posted - 2008-10-03 : 10:31:21
the schema is a good thought Visakh. another shot in the dark:
Have you tried inserting (or updating) the table upTest in a query window? What I'm going after is maybe the account your application runs under doesn't have permission to update the [name] column of the [opportunities] table. And the error is being swallowed by the appl.

EDIT:
I guess that error would abort the transaction so...probably not.

Be One with the Optimizer
TG
Go to Top of Page

jhermiz

3564 Posts

Posted - 2008-10-03 : 13:21:07
No its definately the right schema.
Im puzzled about this...I dont know of another way to update that table as the only options I have is a trigger or a sproc...and it cant be a sproc because I cant call the sproc from the front end...all that is left is a trigger. What makes me more upset is even calling a sproc from within the trigger fires but the update doesnt happen. I am thinking this is some sort of timing issue or something to that effect...

Weblog -- [url]http://weblogs.sqlteam.com/jhermiz[/url]
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2008-10-03 : 13:40:10
can you post your exact second update statement?one without hardcoded id?
Go to Top of Page

TG
Master Smack Fu Yak Hacker

6065 Posts

Posted - 2008-10-03 : 13:45:28
This has got to work - it must be someting silly.

- look at the current trigger definition to confirm the current version is the one with your test update statement.

- add a couple test statements in there: select * from opportunities WHERE id = 'd6204c18-c33d-5f61-362e-48e4d51a1628' before AND after the update

- change the SET NOCOUNT to OFF for some additional info

- run an UPDATE statement in a query window to test the trigger output: UPDATE opportunities_cstm set <something> WHERE...

Of course if the real code is different than what you posted, post the real code :)

Be One with the Optimizer
TG
Go to Top of Page
   

- Advertisement -