SQL Trigger Help

Status
Not open for further replies.

frankrizzo

Ars Tribunus Militum
1,892
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
 

svdsinner

Ars Legatus Legionis
15,152
Subscriptor
frankrizzo":almrwb6g said:
I am not able to fix/change the schema.

What sort of transaction would I be able to encapsulate the insert into? In this instance, the insert is initiated by the software and I am not able to intercept that.
My understanding is that all triggers are inherently run in the same transaction as the firing statement, due to the nature of SQL. (Each individual commands is always either fully run or fully backed out, basic ACID stuff)

I don't think you need to do anything to make this happen. (I haven't tested this, so I might be wrong)
 

Richard Berg

Ars Legatus Legionis
43,037
Subscriptor
So...you don't control the DB, you don't control the app...but the raw table contents matters to you, and you're allowed to create arbitrary triggers that modify the app's logical (if not physical) design?

Sorry, that situation is totally foreign to me. I have no context to guess what other (better) solutions might be available to you.
 

frankrizzo

Ars Tribunus Militum
1,892
I am a Sales Engineer with a copy of our software that runs on SQL Express 2005 on my laptop. I need to be able to simulate various actions that would be normally be automated on the backend in an installation, typically where a trigger would connect to the customer's accounting, ERP, or whatever db. I used CustomerID in this example, but it just as easily could have been PO Number, Loan Number, etc.

I am not looking for the best way to get this done, just "A" way. While my SQL knowledge is clearly below average, I am familiar with database design. Once given a way to do something, I can build it out pretty good and re-use. This is all for non production activities and just the proof of concept/sales process. The reason I am not going to the programmers with this is because I have already bugged them enough for the next couple of weeks.

Hope that explains why I am trying to do this.
 

A.W.

Ars Tribunus Angusticlavius
6,790
Richard Berg":3nicfzcp said:
So...you don't control the DB, you don't control the app...but the raw table contents matters to you, and you're allowed to create arbitrary triggers that modify the app's logical (if not physical) design?

Sorry, that situation is totally foreign to me. I have no context to guess what other (better) solutions might be available to you.
It sounds like it's a commercial application, which uses a set database schema (so he can't change it functionally, but he can do things that are transparent to the application), and he's trying to make things easier for his users. Is that situation really that foreign to you?

Edit: Or it could be that. :p
 

Fristie Blade

Ars Tribunus Militum
1,722
Subscriptor++
Well, just to point you in the right direction.
So you have a customer ID field and some customer descriptive fields in your documents table and your goal is that whenever the customer ID changes (or a new one on insert) that those extra customer descriptive fields match the information belonging to the customer ID that is referred to for that row. Right?
So I assume you also have a customer table with that info to copy it from. Your question would not make sense otherwise I suppose.

That in itself is quite easy to do. Basically whenever the insert or update event occurs, your trigger will need to update the documents table by joining it with the customer table and the inserted pseudo table.
Small remark here: in your example you try to pull a single customer id and single document number from the inserted pseudo table and store it into a local variable. This behavior will fail when your 'inserted' contains more than one record. Eg, when updating more than one record at a time.
Better to avoid your trigger blocking some update possibilities even if you don't expect it to happen. Especially since you don't need to pull that data into a variable. It will only slow down your trigger.

I would do something like this (code not tested on SQL server as I have no time for it):

table cust (cust_id, cust_name, cust_address)
table doc (doc_id, doc_name, cust_id, cust_name, cust_address)

Code:
CREATE TRIGGER doc_cust_id_aiu ON doc
AFTER insert, update
BEGIN

SET NOCOUNT ON

UPDATE 
doc
FROM
doc
INNER JOIN inserted as i ON doc.doc_id = i.doc_id
INNER JOIN cust as c ON doc.cust_id = c.cust_id
SET
cust_name = c.cust_name
, cust_address = c.cust_address

END

Something like that to start with.
 

Richard Berg

Ars Legatus Legionis
43,037
Subscriptor
Find another job, fast
Owning my schemas is a feature, not a bug.

It sounds like it's a commercial application, which uses a set database schema (so he can't change it functionally, but he can do things that are transparent to the application), and he's trying to make things easier for his users. Is that situation really that foreign to you?
I considered that, but the main reason people stick with crappy old proprietary apps is because their business actually depends on them. Mucking with DB internals is a great excuse for its vendor (who probably hates the crappy old app more than you do) to void your support agreement.

The OP's followup explanation makes a lot more sense. The rules for a one-off demo are very different from "the right way" he originally requested.
 
Status
Not open for further replies.