I know just this side of nothing about MSSQL triggers, (I'm a VFP programmer) but I have to write a trigger for a client.
Some rogue program is placing a record in a table (SYINVL) with a primary key of zero. Then days later the application crashes when a second person attenpts to add a record with a value other then zero with the error Duplicate Value in Primary Key. (The rogue program could be inside my application or one of the 4 other applications maintained by a different set of programmers that inserts records into this table.)
I included the script to create the holding table so you would know the Table structure if you had any questions.
The Primary key field is SYINVLID.
In the Hold table there should not be a index.
would these scripts work ??
Or do you guys know of a better way to do it until I can find the problem program/outside application.)
David W. Grewe Dave
Some rogue program is placing a record in a table (SYINVL) with a primary key of zero. Then days later the application crashes when a second person attenpts to add a record with a value other then zero with the error Duplicate Value in Primary Key. (The rogue program could be inside my application or one of the 4 other applications maintained by a different set of programmers that inserts records into this table.)
I included the script to create the holding table so you would know the Table structure if you had any questions.
The Primary key field is SYINVLID.
In the Hold table there should not be a index.
would these scripts work ??
Or do you guys know of a better way to do it until I can find the problem program/outside application.)
Code:
\* MSSQL Create Hold Table Script
--USE LEADS GO
CREATE TABLE [dbo].[syinvl_hold] (
[syinvlid] [int] NOT NULL ,
[relrec_key] [char] (2) NOT NULL ,
[relrec_id] [int] NOT NULL ,
[relatnship] [char] (15) NOT NULL ,
[date_inv] [datetime] NULL ,
[inc_desc] [char] (55) NOT NULL ,
[rec_key] [char] (2) NOT NULL ,
[rec_id] [int] NOT NULL ,
[agencyid] [char] (4) NOT NULL ,
[innameflag] [char] (1) NOT NULL ,
[sysgen] [char] (1) NOT NULL ,
[inv_status] [char] (6) NOT NULL ,
[modtime] [datetime] NULL,
[moduser] [char] (11) NOT NULL ,
[secure] [int] NOT NULL ,
[scmainid] [int] NOT NULL ,
[approved] [int] NOT NULL ,
[Username] [char] (55) NULL ,
[Hostname] [char] (22) NULL ,
[Appname] [char] (22) NULL ,
[SQLstamp] [datetime] NULL
) ON [PRIMARY]
GO
\* MSSQL Create insert trigger script
create Trigger syinvl_insert_hold
On SYINVL
For INSERT
As
IF syinvlid = 0
INSERT INTO syinvl_hold
(syinvlid, relrec_key, relrec_id, relatnship, date_inv, inc_desc, rec_key, rec_id,
agencyid, innameflag, sysgen, inv_status, modtime, moduser, secure, scmainid, approved,
username, hostname, appname, sqlstamp)
VALUES
(SELECT syinvlid, relrec_key, relrec_id, relatnship, date_inv, inc_desc, rec_key, rec_id,
agencyid, innameflag, sysgen, inv_status, modtime, moduser, secure, scmainid, approved,
SUBSTRING(SUSER_SNAME(), 1, 55), SUBSTRING(HOST_NAME(), 1, 22), SUBSTRING(APP_NAME(), 1, 22), GETDATE() from SYINVL where SYINVLID = 0)
DELETE FROM syinvl WHERE syinvlid = 0
ENDIF
GO
David W. Grewe Dave