create trigger on update and on insert

  • Hi,

    I have this table

    CREATE TABLE [dbo].[CARTOES](

    [COD_ID] [int] IDENTITY(1,1) NOT NULL,

    [CODCTB] [int] NULL,

    [NIFCTB] [varchar](20) NULL,

    [NOMECTB] [varchar](100) NULL,

    [DT_EMISSAO] [datetime] NULL,

    [DT_VALIDADE] [smalldatetime] NULL,

    [DT_ANULACAO] [datetime] NULL,

    [DESCR_REP_FISCAL] [varchar](100) NULL,

    [NUMCARTAO] [int] NULL,

    [STATUS] [char](1) NULL,

    [UTILIZADOR] [varchar](20) NULL,

    CONSTRAINT [PK_CARTOES] PRIMARY KEY CLUSTERED

    (

    [COD_ID] ASC

    )WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]

    ) ON [PRIMARY]

    GO

    SET ANSI_PADDING OFF

    GO

    ALTER TABLE [dbo].[CARTOES] WITH CHECK ADD CONSTRAINT [FK_CARTOES_CONTRIBUINTES] FOREIGN KEY([CODCTB])

    REFERENCES [dbo].[ALLCTB] ([CODCTB])

    GO

    ALTER TABLE [dbo].[CARTOES] CHECK CONSTRAINT [FK_CARTOES_CONTRIBUINTES]

    GO

    ALTER TABLE [dbo].[CARTOES] WITH CHECK ADD CONSTRAINT [FK_CARTOESAUX_CONTRIBUINTES] FOREIGN KEY([CODCTB])

    REFERENCES [dbo].[ALLCTB] ([CODCTB])

    GO

    ALTER TABLE [dbo].[CARTOES] CHECK CONSTRAINT [FK_CARTOESAUX_CONTRIBUINTES]

    Iwant to create a trigger that sends the record from this table to another table everytime I make a insert or an update through an app.

    The second table has the exact same estructure.

    Can someone help?

  • You can use CREATE TRIGGER and MERGE.

    Where are you having problems with the CREATE TRIGGER?

  • I don't have problems. I just can create it. It's my first time with triggers.

  • I just cannot create it. the sintaxe seems very complicated.

  • If you just want to do INSERTs to the other table, and not UPDATEs:

    CREATE TRIGGER trigger_name

    ON dbo.CARTOES

    AFTER INSERT, UPDATE

    AS

    SET NOCOUNT ON;

    INSERT INTO dbo.another_table --(column_name1, column_name2, ...)

    SELECT

    [COD_ID],

    [CODCTB],

    [NIFCTB],

    [NOMECTB],

    [DT_EMISSAO],

    [DT_VALIDADE],

    [DT_ANULACAO],

    [DESCR_REP_FISCAL],

    [NUMCARTAO],

    [STATUS],

    [UTILIZADOR]

    FROM inserted

    GO

    SQL DBA,SQL Server MVP(07, 08, 09) "It's a dog-eat-dog world, and I'm wearing Milk-Bone underwear." "Norm", on "Cheers". Also from "Cheers", from "Carla": "You need to know 3 things about Tortelli men: Tortelli men draw women like flies; Tortelli men treat women like flies; Tortelli men's brains are in their flies".

  • Thank you very much. I will test it today.

  • You can try this,

    http://www.sql-server-performance.com/2014/dml-triggers-multiple-triggers/

    With Thanks,

    Satnam

Viewing 7 posts - 1 through 6 (of 6 total)

You must be logged in to reply to this topic. Login to reply