Trigger on Insert made a strage information,

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • veasnamuch
    New Member
    • Nov 2007
    • 3

    Trigger on Insert made a strage information,

    I have a problem while I create a trigger to my table. My objective is getting any change made to my table [tblCR] and record it in to another table [tblLogWeb]. My [tblCR] have thousands records before I add new trigger to this table, everything work fine means, when I manual insert data into table the last row of inserting is what I am doing but after I add trigger on inserting to this table and I do next manual insert data into the last row in Enterprise Manager, I got a strange info, it is shown the row somewhere of my table but not the current value which I just added. If I run SQL to show the data from table again, I saw the last value which that I just added at the bottom.

    The following script is the schema of my table and also its triggers

    ------------------------ table schema -------------------------

    CREATE TABLE [dbo].[tblCR] (
    [CRID] [int] IDENTITY (1, 1) NOT NULL ,
    [FlightID] [smallint] NULL ,
    [CRDate] [smalldatetime] NULL ,
    [CRReceiveID] [smallint] NULL ,
    [CRCountingPerso nID] [nvarchar] (50) NULL ,
    [CRAirlineEmploy eeID] [nvarchar] (50) NULL
    ) ON [PRIMARY]
    GO


    ALTER TABLE [dbo].[tblCRD] WITH NOCHECK ADD
    CONSTRAINT [PK_tblCRD] PRIMARY KEY CLUSTERED
    (
    [CRDID]
    ) ON [PRIMARY]
    GO


    ----------------------- trigger ---------------------------

    --Insert
    --DROP TRIGGER trg_insert_tblC R
    CREATE TRIGGER trg_insert_tblC R ON [dbo].[tblCR]
    FOR INSERT
    AS
    INSERT INTO tblLogWeb(userT ransaction)
    SELECT 'INSERT|tblCR|' + RTRIM(LTRIM(STR (CRID)))
    FROM inserted
    GO


    --update
    --DROP TRIGGER trg_update_tblC R
    CREATE TRIGGER trg_update_tblC R ON [dbo].[tblCR]
    FOR UPDATE
    AS
    INSERT INTO tblLogWeb(userT ransaction)
    SELECT 'UPDATE|tblCR|' + RTRIM(LTRIM(STR (CRID)))
    FROM inserted
    GO


    --delete
    --DROP TRIGGER trg_delete_tblC R
    CREATE TRIGGER trg_delete_tblC R ON [dbo].[tblCR]
    FOR DELETE
    AS
    INSERT INTO tblLogWeb(userT ransaction)
    SELECT 'DELETE|tblCR|' + RTRIM(LTRIM(STR (CRID)))
    FROM deleted
    GO


    Could you please tell me what happend? And how could I do to resolve this problem? There have another application in Ms Access which used that value, so if it doesn't show the current value of added, but the some other value or the row, it cause a big trouble.

    Best regards,

    Veasna
  • vksingh24
    New Member
    • Dec 2007
    • 21

    #2
    Originally posted by veasnamuch
    I have a problem while I create a trigger to my table. My objective is getting any change made to my table [tblCR] and record it in to another table [tblLogWeb]. My [tblCR] have thousands records before I add new trigger to this table, everything work fine means, when I manual insert data into table the last row of inserting is what I am doing but after I add trigger on inserting to this table and I do next manual insert data into the last row in Enterprise Manager, I got a strange info, it is shown the row somewhere of my table but not the current value which I just added. If I run SQL to show the data from table again, I saw the last value which that I just added at the bottom.

    The following script is the schema of my table and also its triggers

    ------------------------ table schema -------------------------

    CREATE TABLE [dbo].[tblCR] (
    [CRID] [int] IDENTITY (1, 1) NOT NULL ,
    [FlightID] [smallint] NULL ,
    [CRDate] [smalldatetime] NULL ,
    [CRReceiveID] [smallint] NULL ,
    [CRCountingPerso nID] [nvarchar] (50) NULL ,
    [CRAirlineEmploy eeID] [nvarchar] (50) NULL
    ) ON [PRIMARY]
    GO


    ALTER TABLE [dbo].[tblCRD] WITH NOCHECK ADD
    CONSTRAINT [PK_tblCRD] PRIMARY KEY CLUSTERED
    (
    [CRDID]
    ) ON [PRIMARY]
    GO


    ----------------------- trigger ---------------------------

    --Insert
    --DROP TRIGGER trg_insert_tblC R
    CREATE TRIGGER trg_insert_tblC R ON [dbo].[tblCR]
    FOR INSERT
    AS
    INSERT INTO tblLogWeb(userT ransaction)
    SELECT 'INSERT|tblCR|' + RTRIM(LTRIM(STR (CRID)))
    FROM inserted
    GO


    --update
    --DROP TRIGGER trg_update_tblC R
    CREATE TRIGGER trg_update_tblC R ON [dbo].[tblCR]
    FOR UPDATE
    AS
    INSERT INTO tblLogWeb(userT ransaction)
    SELECT 'UPDATE|tblCR|' + RTRIM(LTRIM(STR (CRID)))
    FROM inserted
    GO


    --delete
    --DROP TRIGGER trg_delete_tblC R
    CREATE TRIGGER trg_delete_tblC R ON [dbo].[tblCR]
    FOR DELETE
    AS
    INSERT INTO tblLogWeb(userT ransaction)
    SELECT 'DELETE|tblCR|' + RTRIM(LTRIM(STR (CRID)))
    FROM deleted
    GO


    Could you please tell me what happend? And how could I do to resolve this problem? There have another application in Ms Access which used that value, so if it doesn't show the current value of added, but the some other value or the row, it cause a big trouble.

    Best regards,

    Veasna


    I think the column type is IDENTITY so that its creating problem.

    More info is here http://support.microsoft.com/kb/319699

    Comment

    Working...