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 August 27th, 2004, 08:35 AM
Authorized User
 
Join Date: Mar 2004
Posts: 17
Thanks: 0
Thanked 0 Times in 0 Posts
Default Triggers and declare cursor question

Hi all

I'm trying to get my head around triggers in SQL2k. What I need to do is hold an audit table of database changes so I wrote this: -

CREATE TRIGGER [tr_allocs_u] ON [dbo].[allocations]
FOR update
AS
set nocount on
declare @duser nvarchar(255)
declare @iuser nvarchar(255)
declare @istart nvarchar(255)
declare @iend nvarchar(255)
declare @idriver_name nvarchar(255)
declare @ivehicle_id nvarchar(255)
declare @icostcentre nvarchar(255)
declare @idepot_id nvarchar(255)
declare @dstart nvarchar(255)
declare @dend nvarchar(255)
declare @ddriver_name nvarchar(255)
declare @dvehicle_id nvarchar(255)
declare @dcostcentre nvarchar(255)
declare @ddepot_id nvarchar(255)
declare @iarchive nvarchar(255)
declare @darchive nvarchar(255)


declare @rn int
declare @user nVarChar(10)
declare @datetime datetime
select @datetime=getdate()

-- Declare the audit cursor

declare audit_cursor cursor local for
     select
         i.record_number,i.start_date as istart,i.end_date as iend,i.driver_name as idriver_name,i.vehicle_id as ivehicle_id,i.changed_by as iuser,i.costcentre as icostcentre,i.depot_id as idepot_id,i.archive_status as iarchive,
        d.start_date as dstart,d.end_date as dend,d.driver_name as ddriver_name,d.vehicle_id as dvehicle_id,d.changed_by as duser,d.costcentre as dcostcentre,d.depot_id as ddepot_id,d.archive_status as darchive
         from inserted i inner join deleted d on i.record_number=d.record_number

open audit_cursor
fetch next from audit_cursor into @rn,@istart,@iend,@idriver_name,@ivehicle_id,@iuse r,@icostcentre,@idepot_id,@iarchive,@dstart,@dend, @ddriver_name,@dvehicle_id,@duser,@dcostcentre,@dd epot_id,@darchive
while @@fetch_status=0
begin
        select @user=@iuser

-- Need a statement block for each entry to check

    if @istart<>@dstart
    begin
        insert into audit (audit_comment,host_record,audit_old_value,audit_n ew_value,audit_user,audit_date) values ('Allocation Start Date',@rn,@dstart,@istart,@user,@datetime)
    end
    if @iend<>@dend
    begin
        insert into audit (audit_comment,host_record,audit_old_value,audit_n ew_value,audit_user,audit_date) values ('Allocation End Date',@rn,@dEnd,@iEnd,@user,@datetime)
    end
    if @idriver_name<>@ddriver_name
    begin
        insert into audit (audit_comment,host_record,audit_old_value,audit_n ew_value,audit_user,audit_date) values ('Allocation driver_name',@rn,@dDriver_name,@idriver_name,@user ,@datetime)
    end
    if @ivehicle_id<>@dvehicle_id
    begin
        insert into audit (audit_comment,host_record,audit_old_value,audit_n ew_value,audit_user,audit_date) values ('Allocation Vehicle ID',@rn,@dVehicle_id,@ivehicle_id,@user,@datetime)
    end
    if @icostcentre<>@dcostcentre
    begin
        insert into audit (audit_comment,host_record,audit_old_value,audit_n ew_value,audit_user,audit_date) values ('Allocation Costcentre',@rn,@dcostcentre,@icostcentre,@user,@d atetime)
    end
    if @iarchive<>@darchive
    begin
        insert into audit (audit_comment,host_record,audit_old_value,audit_n ew_value,audit_user,audit_date) values ('Allocation Archive Status',@rn,@darchive,@iarchive,@user,@datetime)
    end

-- loop

fetch next from audit_cursor into @rn,@istart,@iend,@idriver_name,@ivehicle_id,@iuse r,@icostcentre,@idepot_id,@iarchive,@dstart,@dend, @ddriver_name,@dvehicle_id,@duser,@dcostcentre,@dd epot_id,@darchive
end

-- tidy

close audit_cursor
deallocate audit_cursor

Am I on the right track with this, I don't know whether i'm over complicating things. Also are there any concurrency style issues with generating a cursor to combine the inserted and deleted tables?

Appreciate any pointers.

Regards
Graham


 
Old September 3rd, 2004, 06:20 PM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 2,480
Thanks: 0
Thanked 1 Time in 1 Post
Default

Yes, you are complicating things here. Using cursors within Triggers going to make things really tough for the server. You can see that in the performance. Try to avoid cursor within triggers. This is really going to take a time to update rows. Imagine when, in a multiuser environment, when more user are trying to update, that would put more load on the server.

_________________________
- Vijay G
Strive for Perfection





Similar Threads
Thread Thread Starter Forum Replies Last Post
how to declare variables rehana Beginning PHP 11 April 30th, 2013 04:59 AM
Magnetic Cursor - Target Area Cursor? gcarcass .NET Framework 2.0 1 May 5th, 2008 07:20 AM
Namespace - how to declare? JillB Excel VBA 0 January 25th, 2008 07:11 PM
declare My ajkumar Visual Basic 2005 Basics 0 April 18th, 2007 05:24 AM
Cursor Question kilika SQL Server 2000 8 August 21st, 2003 01:34 PM





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