I am having a lot of trouble figuring out the right way to get a trigger to work and was hoping to get some suggestions. I have spent more time than I care to admit on trying to figure this out.
I have a SQL db with a table that has a series of fields that describe customer documents. Customer 1 might have 10 records, Customer 2 might have 1. Within a particular customer most of the column data does not change. DocID is a system generated field referring to the particular document and CustomerNumber would be manually entered by the user saving the document to the system.
My goal is to have a trigger automatically copy the fields that do not change to the record that is getting inserted or updated when the user just enters in the CustomerNumber. I wrote a trigger that can do this manually for a particular customer, but I want it to be able to do it for whatever customerID is entered using variables. Can anyone point me in the right direction?
Any help would be greatly appreciated.
set ANSI_NULLS ON
set QUOTED_IDENTIFIER ON
GO
ALTER TRIGGER [dbo].[trg_update]
ON [dbo].[dbFields]
After INSERT, UPDATE
AS
BEGIN
SET NOCOUNT ON;
declare @CustNumber as varchar(10)
declare @DocID as int
set @CustNumber = (select Field15 from inserted)
set @DocID = (select DocID from inserted)
if (@CustNumber = '08302008')
BEGIN
UPDATE dbFields set Field1='John Doe', Field8='1 Main Street', Field9='Hartford', Field12='CT', Field10='06120' where ArchiveID='3' and DocID=@DocID
END
END
I have a SQL db with a table that has a series of fields that describe customer documents. Customer 1 might have 10 records, Customer 2 might have 1. Within a particular customer most of the column data does not change. DocID is a system generated field referring to the particular document and CustomerNumber would be manually entered by the user saving the document to the system.
My goal is to have a trigger automatically copy the fields that do not change to the record that is getting inserted or updated when the user just enters in the CustomerNumber. I wrote a trigger that can do this manually for a particular customer, but I want it to be able to do it for whatever customerID is entered using variables. Can anyone point me in the right direction?
Any help would be greatly appreciated.
set ANSI_NULLS ON
set QUOTED_IDENTIFIER ON
GO
ALTER TRIGGER [dbo].[trg_update]
ON [dbo].[dbFields]
After INSERT, UPDATE
AS
BEGIN
SET NOCOUNT ON;
declare @CustNumber as varchar(10)
declare @DocID as int
set @CustNumber = (select Field15 from inserted)
set @DocID = (select DocID from inserted)
if (@CustNumber = '08302008')
BEGIN
UPDATE dbFields set Field1='John Doe', Field8='1 Main Street', Field9='Hartford', Field12='CT', Field10='06120' where ArchiveID='3' and DocID=@DocID
END
END