Solved

insert update triger Ms SQL 2005

Posted on 2010-08-12
16
288 Views
Last Modified: 2012-05-10
i can not get this trigger to update on an insert or update on MS SQL 2005

can anybody see what is wrong with it.


edited for further explination

the database is updayed via a .net web service, the data base updates correctly, but is not firing the trigger that should update the status column on all records in the table where [name] = inserted record name.
/****** Object:  Trigger [dbo].[status_update]    Script Date: 08/12/2010 17:57:38 ******/

SET ANSI_NULLS ON

GO

SET QUOTED_IDENTIFIER ON

GO



ALTER TRIGGER [dbo].[status_update] ON [dbo].[Planets] 

FOR INSERT,UPDATE

AS 

UPDATE dbo.Planets 

SET planets.[status] = inserted.[status] 

FROM planets join inserted  on planets.Coordinates = inserted.Coordinates

WHERE planets.[name] = inserted.[name]

Open in new window

0
Comment
Question by:QuinnDester
  • 6
  • 6
  • 2
  • +1
16 Comments
 
LVL 5

Expert Comment

by:tvPrasad
ID: 33423355
/****** Object:  Trigger [dbo].[status_update]    Script Date: 08/12/2010 17:57:38 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO

ALTER TRIGGER [dbo].[status_update] ON [dbo].[Planets]
AFTER INSERT,UPDATE
AS
UPDATE dbo.Planets
SET planets.[status] = inserted.[status]
FROM planets join inserted  on planets.Coordinates = inserted.Coordinates
WHERE planets.[name] = inserted.[name]

USE AFTER INSTEAD OF FOR
0
 
LVL 1

Expert Comment

by:nickrawlins
ID: 33423371
hmm, do you need to do a join? can you not do this:

SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO

ALTER TRIGGER [dbo].[status_update] ON [dbo].[Planets]
FOR INSERT,UPDATE
AS
UPDATE dbo.Planets
SET planets.[status] = inserted.[status]
FROM planets where planets.Coordinates = inserted.Coordinates
AND planets.[name] = inserted.[name]
0
 
LVL 5

Expert Comment

by:tvPrasad
ID: 33423545
One more thing you need seperate insert from update since this Trigger is for both

for updates use UPDATE table to find updates

Hope it helps
0
 
LVL 58

Expert Comment

by:cyberkiwi
ID: 33423580
Your web service may not be reporting errors to you.
If you have recursive triggers turned on, it will perpetually try to update the status until it aborts.

See modification below.
/****** Object:  Trigger [dbo].[status_update]    Script Date: 08/12/2010 17:57:38 ******/

SET ANSI_NULLS ON

GO

SET QUOTED_IDENTIFIER ON

GO



ALTER TRIGGER [dbo].[status_update] ON [dbo].[Planets] 

FOR INSERT,UPDATE

AS 

UPDATE dbo.Planets 

SET planets.[status] = inserted.[status] 

FROM planets join inserted  on planets.Coordinates = inserted.Coordinates

WHERE planets.[name] = inserted.[name]

  AND planets.[status] <> inserted.[status] 

Open in new window

0
 
LVL 58

Expert Comment

by:cyberkiwi
ID: 33423608
> for updates use UPDATE table to find updates

Don't be silly, there are only 2 virtual tables INSERTED and DELETED.
0
 
LVL 3

Author Comment

by:QuinnDester
ID: 33423745
@nickrawlins:

ALTER TRIGGER [dbo].[status_update] ON [dbo].[Planets]
FOR INSERT,UPDATE
AS
UPDATE dbo.Planets
SET planets.[status] = inserted.[status]
FROM planets where planets.Coordinates = inserted.Coordinates
AND planets.[name] = inserted.[name]

this wouldnt work, the Coordinates only match the one record.


@tvPrasad: that isnt working either....
0
 
LVL 3

Author Comment

by:QuinnDester
ID: 33424017
@cyberkiwi:

sorry that isnt working either, the table is inserting and updating correctly, but the trigger isnt firing to update other records,
0
 
LVL 3

Author Comment

by:QuinnDester
ID: 33424176
More information, i have tried a manual update and this shows that the trigger is firing but not updating the table..

this is the result i am getting..


(0 row(s) affected)

(1 row(s) affected)
0
Complete VMware vSphere® ESX(i) & Hyper-V Backup

Capture your entire system, including the host, with patented disk imaging integrated with VMware VADP / Microsoft VSS and RCT. RTOs is as low as 15 seconds with Acronis Active Restore™. You can enjoy unlimited P2V/V2V migrations from any source (even from a different hypervisor)

 
LVL 58

Expert Comment

by:cyberkiwi
ID: 33424251
That can only mean that it has found no matching record (by name) to update...
Can you try inserting the same name twice in a row?
0
 
LVL 3

Author Comment

by:QuinnDester
ID: 33424501
there is actualy 8 records it should match with, i have cecked and they are all there
0
 
LVL 58

Expert Comment

by:cyberkiwi
ID: 33424522
Regardless, could you please just try it?

insert planets(name, coordinates, status)
values ('easilyidentifiable', <whatever type>, 1)
go
insert planets(name, coordinates, status)
values ('easilyidentifiable', <whatever type>, 1)
go
0
 
LVL 3

Author Comment

by:QuinnDester
ID: 33424683
i did this

insert into planets ( [name], [status]) values ('test','active')
insert into planets ( [name], [status]) values ('test','off')
(0 row(s) affected)

(1 row(s) affected)
(0 row(s) affected)

(1 row(s) affected)

select [name], [status] from planets where [name] like 'test'

test      active
test      off

the trigger should have changed the first record to off when the seconed record was inserted
0
 
LVL 58

Accepted Solution

by:
cyberkiwi earned 500 total points
ID: 33424724
Since you don't care about the coordinates, why join by that condition?
/****** Object:  Trigger [dbo].[status_update]    Script Date: 08/12/2010 17:57:38 ******/

SET ANSI_NULLS ON

GO

SET QUOTED_IDENTIFIER ON

GO



ALTER TRIGGER [dbo].[status_update] ON [dbo].[Planets] 

FOR INSERT,UPDATE

AS 

UPDATE dbo.Planets 

SET planets.[status] = inserted.[status] 

FROM inserted

WHERE planets.[name] = inserted.[name]

Open in new window

0
 
LVL 3

Author Comment

by:QuinnDester
ID: 33424798
that did it...

i was joining on the cords because that was the only unique identifier i had, but i see now that it was limiting the results to those it could join on, and that would be 1 record


thanks for your help
0
 
LVL 1

Expert Comment

by:nickrawlins
ID: 33424926
In all due respect, my answer above was identical to cyberkiwi's and was the second comment yet the points were assigned to him... ?
0
 
LVL 58

Expert Comment

by:cyberkiwi
ID: 33424948
Nick,
With all due respect, your query is no different from the question.  Putting that in the JOIN vs WHERE still enforces the filter.
0

Featured Post

What is SQL Server and how does it work?

The purpose of this paper is to provide you background on SQL Server. It’s your self-study guide for learning fundamentals. It includes both the history of SQL and its technical basics. Concepts and definitions will form the solid foundation of your future DBA expertise.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

There have been several questions about Large Transaction Log Files in SQL Server 2008, and how to get rid of them when disk space has become critical. This article will explain how to disable full recovery and implement simple recovery that carries…
In this article I will describe the Backup & Restore method as one possible migration process and I will add the extra tasks needed for an upgrade when and where is applied so it will cover all.
This Micro Tutorial will give you a basic overview how to record your screen with Microsoft Expression Encoder. This program is still free and open for the public to download. This will be demonstrated using Microsoft Expression Encoder 4.
This Micro Tutorial demonstrates using Microsoft Excel pivot tables, how to reverse engineer competitors' marketing strategies through backlinks.

863 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

26 Experts available now in Live!

Get 1:1 Help Now