Wrox Programmer Forums
Go Back   Wrox Programmer Forums > SQL Server > SQL Server 2000 > SQL Server 2000
|
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 Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old April 20th, 2006, 05:06 AM
Friend of Wrox
 
Join Date: Jan 2006
Posts: 131
Thanks: 10
Thanked 0 Times in 0 Posts
Default Trigger & SP (Table Updates)

Hi,

When a field in Table SAMPLE is updated (i.e. field 'Status' is updated to 'C'), I need to populate various fields in Table EXPORT.

I have written a Trigger based on Table SAMPLE, as follows:

CREATE TRIGGER [ExportSampleXML] ON [dbo].[SAMPLE]
AFTER UPDATE

AS

Declare @SampleNumber Integer
Declare @Status VarChar(1)

SELECT * FROM SAMPLE

    --Populate the Variables
    BEGIN
    SELECT @SAMPLENUMBER = SAMPLE_NUMBER FROM UPDATED
    WHERE STATUS = C

    EXEC usp_ExportXML @SAMPLENUMBER, @STATUS

END

Next, I have written the following Stored Procedure to UPDATE the Table EXPORT on the previous findings:

CREATE PROCEDURE [usp_ExportXML]

(
@SAMPLENUMBER [int],
@STATUS [varchar (1)]
)

As

INSERT into EXPORT ([SAMPLE_NUMBER],[STATUS])

    VALUES (@SAMPLENUMBER, @STATUS)

GO

The Table EXPORT is NOT being populated. Are my commands correct?

can anyone please help?

Thanks in advance,

Neal

A Northern Soul
__________________
Neal

A Northern Soul
 
Old April 20th, 2006, 09:20 AM
SQLScott's Avatar
Wrox Author
 
Join Date: Dec 2004
Posts: 338
Thanks: 0
Thanked 2 Times in 2 Posts
Default

Neal, you are doing this the hard way. You are close, but using the UPDATED system table the correct way you can forgo the SP.

Your trigger will look something like this:


CREATE TRIGGER [ExportSampleXML] ON [dbo].[SAMPLE]
AFTER UPDATE

AS

    INSERT into EXPORT ([SAMPLE_NUMBER],[STATUS]) SELECT SAMPLE_NUMBER, STATUS FROM UPDATED

END


Scott


 
Old April 20th, 2006, 09:47 AM
Friend of Wrox
 
Join Date: Jan 2006
Posts: 131
Thanks: 10
Thanked 0 Times in 0 Posts
Default

Many Thanks,

Can I Delete all records from table EXPORT before I append the New record (in EXPORT), within the same trigger?

If so, how do I do this?

Neal

A Northern Soul
 
Old April 20th, 2006, 10:23 AM
SQLScott's Avatar
Wrox Author
 
Join Date: Dec 2004
Posts: 338
Thanks: 0
Thanked 2 Times in 2 Posts
Default

Sure, something like:

CREATE TRIGGER [ExportSampleXML] ON [dbo].[SAMPLE]
AFTER UPDATE

AS

    DELETE FROM EXPORT

    INSERT into EXPORT ([SAMPLE_NUMBER],[STATUS]) SELECT SAMPLE_NUMBER, STATUS FROM UPDATED

END


 
Old April 24th, 2006, 09:55 AM
Friend of Wrox
 
Join Date: Jan 2006
Posts: 131
Thanks: 10
Thanked 0 Times in 0 Posts
Default

Hi,

Can anyone please help with the following?

CREATE TRIGGER [ExportSampleXML] ON [dbo].[SAMPLE]
AFTER UPDATE

AS

BEGIN
    --DELETE FROM EXPORT
     INSERT into [dbo].[EXPORT] ([SAMPLE_NUMBER],[STATUS]) SELECT SAMPLE_NUMBER, STATUS FROM UPDATED
        WHERE STATUS = 'C'
END

When UPDATING a record using the aforementioned trigger, the following SQL error message is displayed:

[MicroSoft][ODBC SQL Server Driver][SQL Server]Invalid Object name 'UPDATED'.

I am using SQL Server Db 2000, is this a problem with early versions of SQL Server?

Neal

A Northern Soul
 
Old April 24th, 2006, 10:07 AM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 839
Thanks: 0
Thanked 1 Time in 1 Post
Default

There is no 'UPDATED' logical table in the context of a trigger. There are only the 'deleted' and 'inserted' tables.

In the context of a trigger, an update to the underlying table is considered a delete of the original row followed by an insert of the new, updated row.

It's better this way, since the combination of both tables gives you access to the data both before and after the update. Note that for a delete or insert operation, one or the other of these tables has no rows in it.

Jeff Mason
Custom Apps, Inc.
www.custom-apps.com
 
Old April 25th, 2006, 09:45 AM
Friend of Wrox
 
Join Date: Jan 2006
Posts: 131
Thanks: 10
Thanked 0 Times in 0 Posts
Default

Hi,

Can anyone please help?

If the field STATUS on table(SAMPLE) is UPDATED to Value 'C', I need to clear table(EXPORT) and then populate fields within table(EXPORT) with various values held on the current SAMPLE record.

I am having trouble executing the correct results from the following trigger. What is the correct method?

CREATE TRIGGER [ExportSampleXML] ON [dbo].[SAMPLE]
FOR INSERT

AS

BEGIN
    IF UPDATE(STATUS)
    DELETE FROM EXPORT
    INSERT into [dbo].[EXPORT] ([SAMPLE_NUMBER],[STATUS]) SELECT SAMPLE_NUMBER, STATUS FROM INSERTED
        WHERE STATUS = 'C'
END

It appears that the trigger is trying to update all rows with a value of 'C' (Message = 'Too many records will be affected....').

Thanks in advance,

Neal

A Northern Soul





Similar Threads
Thread Thread Starter Forum Replies Last Post
Stored Procedure That Updates Table GailCG Classic ASP Professional 1 January 22nd, 2006 01:11 PM
updates problem of db table abhit_kumar MySQL 0 December 17th, 2004 07:36 AM
DTS & Trigger rtha3 SQL Server DTS 1 November 4th, 2003 04:05 PM
Lists of All Inserts & Updates Tables in Database Walden SQL Server 2000 1 October 24th, 2003 09:47 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.