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 2000 Forums
 SQL Server Development (2000)
 Anyone want to help me create a update trigger?

Author  Topic 

leodesol
Starting Member

9 Posts

Posted - 2006-06-01 : 17:00:42
I have no clue how to do this, I am new to SQL and to triggers.

The trigger would be for updates to the notes field of a record to keep the old notes.

Table names is CT_MASTER_T.

I need it to copy the PARENT_CHANGE_NUM and NOTES fields to a new history type of table. From the things I have read on creating update triggers I believe I could manage to create this part of it on my own using online tutorials etc...

The kicker is I need to make a primary key field for the notes table to be able to pull these notes back later. I need it to be like this:

[ID] [Parent_change_number] [Notes]
1 56789AB Added new power.
2 56789AB Removed power, decommed.
1 12345BA New device.
3 56789AB Device sold.
2 12345BA Power requested for 5/05/2006
1 54321CA Device ordered.
2 54321CA Device Recieved.
3 12345BA Added new power.

Everytime an instance of a Parent_change_number is seen in the notes-history table the id would be give an increment of one. This will allow me set the ID+Parent and the PK and it would give me the ability to pull the notes for a record easily in my form.

I do believe I could stumble through creation of a trigger that moves the old notes and the parent number, but I have no clue how I could make the ID field do increments like that increment like that and make it be the primary key.

Please go easy on me, SQL is not something I work with often, but I would really like to make this work. Any help appreciated :)

nr
SQLTeam MVY

12543 Posts

Posted - 2006-06-01 : 17:38:59
Have a look at identity in bol.
also see
http://www.nigelrivett.net/#Triggers

==========================================
Cursors are useful if you don't know sql.
DTS can be used in a similar way.
Beer is not cold and it isn't fizzy.
Go to Top of Page

Michael Valentine Jones
Yak DBA Kernel (pronounced Colonel)

7020 Posts

Posted - 2006-06-01 : 18:25:16
Something like this may get you started.

This creates an audit table and logs all adds, changes, and deletes, along with time, application, login, and host to the audit table.


-- Create your table
Create table CT_MASTER_T (
[ID] int not null identity(1,1) primary key clustered,
[Parent_change_number] varchar(10) not null,
[Notes] varchar(4000) not null,
)

go
-- Drop Audit Table, if it exists

if objectproperty(object_id('[dbo].[CT_MASTER_T_AUDIT]'),'IsUserTable') = 1
begin drop table [dbo].[CT_MASTER_T_AUDIT] end
go

-- Create Audit Table for table [dbo].[CT_MASTER_T]
go
create table [dbo].[CT_MASTER_T_AUDIT] (

-- Start of standard audit columns

-- Audit Table Identity Primary Key
[CT_MASTER_T_AUDIT_ID] int not null
identity(1,1)
constraint [PK_CT_MASTER_T_AUDIT]
primary key clustered ( [CT_MASTER_T_AUDIT_ID] ) ,
-- Date and time of the transaction
[AUDIT_DATETIME] datetime not null
constraint [DF__CT_MASTER_T_AUDIT__AUDIT_DATETIME]
default ( CURRENT_TIMESTAMP ) ,
-- Login name performing the transaction
[AUDIT_LOGIN] sysname not null
constraint [DF__CT_MASTER_T_AUDIT__AUDIT_LOGIN]
default ( SYSTEM_USER ) ,
-- Host name performing the transaction
[AUDIT_HOST_NAME] nvarchar (256) not null
constraint [DF__CT_MASTER_T_AUDIT__AUDIT_HOST_NAME]
default ( HOST_NAME() ) ,
-- Application performing the transaction
[AUDIT_APPLICATION_NAME] nvarchar (128) not null
constraint [DF__CT_MASTER_T_AUDIT__AUDIT_APPLICATION_NAME]
default ( APP_NAME() ) ,
-- Unique Identifier for transaction
[AUDIT_UNIQUE_ID] uniqueidentifier not null,
-- If source is INSERTED table, then 1, else 0
[AUDIT_INSERTED_INDICATOR] bit not null ,
-- If source is DELETED table, then 1, else 0
[AUDIT_DELETED_INDICATOR] bit not null ,

-- Start of columns from [dbo].[CT_MASTER_T]

[ID] int not null ,
[Parent_change_number] varchar ( 10 ) not null ,
[Notes] varchar ( 4000 ) not null ,
)
go

-- Drop Audit Trigger, if it exists

if objectproperty(object_id('[dbo].[TR_AUDIT__CT_MASTER_T]'),'IsTrigger') = 1
begin drop trigger [dbo].[TR_AUDIT__CT_MASTER_T] end
go
create trigger [dbo].[TR_AUDIT__CT_MASTER_T]
on
[dbo].[CT_MASTER_T]
after
delete, insert, update
as

set nocount on

declare @error int
declare @rowcount int
declare @ErrorMessage varchar(400)
declare @AUDIT_UNIQUE_ID uniqueidentifier

select @error = 0
select @rowcount = 0
select @ErrorMessage = ''
select @AUDIT_UNIQUE_ID = newid()


insert into [dbo].[CT_MASTER_T_AUDIT]
(
[AUDIT_UNIQUE_ID],
[AUDIT_INSERTED_INDICATOR],
[AUDIT_DELETED_INDICATOR],
[ID],
[Parent_change_number],
[Notes]
)
select
[AUDIT_UNIQUE_ID] = @AUDIT_UNIQUE_ID,
a.[AUDIT_INSERTED_INDICATOR],
a.[AUDIT_DELETED_INDICATOR],
-- Columns from [dbo].[CT_MASTER_T]
a.[ID],
a.[Parent_change_number],
a.[Notes]
from
(
select
[AUDIT_INSERTED_INDICATOR] = 0 ,
[AUDIT_DELETED_INDICATOR] = 1 ,
a1.*
from
deleted a1
union all
select
[AUDIT_INSERTED_INDICATOR] = 1 ,
[AUDIT_DELETED_INDICATOR] = 0 ,
a2.*
from
inserted a2
) a
order by
-- Order by Primary Key columns
a.[ID],
-- Order by Deleted first and then Inserted
case
when a.[AUDIT_DELETED_INDICATOR] = 1
then 1
when a.[AUDIT_INSERTED_INDICATOR] = 1
then 2
else 3
end

select @error = @@error , @rowcount = @@rowcount

if @error <> 0
begin
select @ErrorMessage =
'Error inserting Audit data into [dbo].[CT_MASTER_T_AUDIT]'
goto Error_Exit
end

-- Normal exit
return

-- error handler
Error_Exit:

select @ErrorMessage = isnull(@ErrorMessage+' - ','') +
'Error in Trigger [dbo].[TR_AUDIT__CT_MASTER_T]' +
', SQL Error = ' +
isnull(convert(varchar(20),@error),'NULL') +
', Rowcount = ' +
isnull(convert(varchar(20),@rowcount),'NULL')

raiserror( @ErrorMessage, 16, 1 )

rollback

return
go





CODO ERGO SUM
Go to Top of Page
   

- Advertisement -