How would I use a trigger, in SQL Server 2000 to solve the following problem?
I need a trigger that will do the following:
As data is imported from a stored procedure into a local database I
want it to do two things:
1. As my data is imported from the stored procedure, I want it to
change on-the-fly, to relocate the data.
- I want the data that normally would be inserted into JOB_NO_TBC, to
insert into WO_KEY_TBC, row-by-row as it is inserted.
2. I want it to perform this task only if the date is > 11/23/2002
There are specific reasons that I just cannot change the insert clause
in the sql. Im also just wondering how the transact sql should look.
I want an example based on the following query:
IE.
Here is an example of 1 part of my query:
INSERT INTO dbo.CsgResultsTbcDts
SELECT distinct * FROM OPENQUERY(csg, '
SELECT
SYS_TBC
, PRIN_TBC
, AGNT_TBC
, SUB_ACCT_NO_TBC
, ADDR1_HSE
, RES_CITY_HSE
, MGMT_AREA_HSE
, JOB_NO_TBC
, TOT_UNITS_TBC
, WO_RSN_TBC
, CLS_OUT_RSN_TBC
, ORIG_OPER_ID_TBC
, IR_TECH_TBC
, TIME_FLG_TBC
, LS_CHG_DTE_TBC
, LS_CHG_OPER_ID_TBC
, LS_CHG_TERM_ID_TBC
, WO_KEY_TBC
, WO_STAT_TBC
, WO_CLASS_TBC
, WO_TYP_TBC
, SCHED_CAT_TBC
, DISPATCH_STAT_TBC
, HSE_KEY_HSE
, PRIN_HSE
, SYS_HSE
, res_state_hse
FROM TBC_BASE, HSE_BASE
where
SUBSTR( WO_KEY_TBC, 1, 14) = HSE_KEY_HSE
and LS_CHG_DTE_TBC >= SYSDATE -3
AND PRIN_TBC = PRIN_HSE
AND SYS_TBC = SYS_HSE
AND SYS_TBC = 8773')
WHERE SUBSTRing(WO_KEY_TBC, 1, 14) = HSE_KEY_HSE
AND (SUBSTRing(WO_RSN_TBC, 1, 2) = '42'
OR SUBSTRing(WO_RSN_TBC, 3, 2) = '42'
OR SUBSTRing(WO_RSN_TBC, 5, 2) = '42'
OR SUBSTRing(WO_RSN_TBC, 7, 2) = '42')
AND WO_STAT_TBC IN ('C', 'D' , 'X', 'H', 'O', 'R')
and not exists (select * from CsgResults_Master
where WO_KEY_WoAndTbc = WO_KEY_TBC and ls_chg_dte_WoAndTbc =
ls_chg_dte_tbc and wo_stat_woandtbc = wo_stat_tbc)
Thank you,
-Peter |
Request for Question Clarification by
mathtalk-ga
on
02 Dec 2002 06:36 PST
Hi, pmclinn-ga:
Thanks for the clarification. I have developed and tested an example
trigger that I believe may be satisfactory for your purpose.
Without the specific definition of table dbo.CsgResultsTbcDts, any
example of a CREATE TRIGGER statement that I could provide would
necessarily be based on a table that I invent for the sake of
illustration.
For example, we might consider a table that has the basic features you
require:
- a field JOB_NO_TBC of numeric type
- a field WO_KEY_TBC of type varchar(20)
The trigger would have "if" logic regarding a date being after
11/23/2002, although I'm unclear from looking at your example which
date is meant. Did you mean the system date, GETDATE( )? Perhaps
LS_CHG_DTE_TBC ? Is this of type datetime ?.
So I propose to use the following, for the sake of the example:
CREATE TABLE tblNeedsTrigger
( MY_1St_COL int,
JOB_NO_TBC int,
WO_KEY_TBC varchar(20),
MY_CHG_DTE datetime
)
GO
Note that the table contains no identity or computed columns, and no
columns of type timestamp, features that involve special
considerations for inserts.
The insert trigger logic would be to that if MY_CHG_DTE exceeds
11/23/2002, a value for WO_KEY_TBC will be inserted by converting
JOB_NO_TBC into a string; otherwise the values inserted will simply be
those provided by the insert action.
The trigger and its logic will apply to the existing stored procedure
and to all other INSERT actions on the table.
If such an example would be acceptable to you, please let me know.
Clarify my thinking about the date condition if I have not correctly
interpreted it. If my proposed example is not sufficient, then you
should perhaps define the target table as you wish to have it treated.
regards, mathtalk-ga
|