Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. Database & SysAdmin
  3. Database
  4. Is there any alternative for Trigger After Insert

Is there any alternative for Trigger After Insert

Scheduled Pinned Locked Moved Database
databaseperformanceannouncement
3 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • S Offline
    S Offline
    simpledeveloper
    wrote on last edited by
    #1

    Hi, I have two columns in a table, one is Auto Identity set, and other one should be created as a string value with Id as Pad right, I have created a trigger but my lead is saying Triggers impact the performance, and to not create, is there any alternate for Triggers that would not effect Database performance at the same time do what I am expecting it to be done. My trigger is as follows:

    CREATE TRIGGER AfterInsert
    ON InspectionItems
    AFTER INSERT
    AS
    BEGIN
    declare @InspectionItemNumber nvarchar(20), @InspectionItemId int

    select @InspectionItemId = ins.InspectionItemId FROM INSERTED ins;
    
    update InspectionItems set InspectionItemNumber = 'T' + RIGHT('00000'+CAST(InspectionItemId AS VARCHAR(5)),6)
    where InspectionItemId = @InspectionItemId
    

    END
    GO

    Is there any alternate for trigger and doing the same and preserve the performance - thanks in advance.

    Richard DeemingR 1 Reply Last reply
    0
    • S simpledeveloper

      Hi, I have two columns in a table, one is Auto Identity set, and other one should be created as a string value with Id as Pad right, I have created a trigger but my lead is saying Triggers impact the performance, and to not create, is there any alternate for Triggers that would not effect Database performance at the same time do what I am expecting it to be done. My trigger is as follows:

      CREATE TRIGGER AfterInsert
      ON InspectionItems
      AFTER INSERT
      AS
      BEGIN
      declare @InspectionItemNumber nvarchar(20), @InspectionItemId int

      select @InspectionItemId = ins.InspectionItemId FROM INSERTED ins;
      
      update InspectionItems set InspectionItemNumber = 'T' + RIGHT('00000'+CAST(InspectionItemId AS VARCHAR(5)),6)
      where InspectionItemId = @InspectionItemId
      

      END
      GO

      Is there any alternate for trigger and doing the same and preserve the performance - thanks in advance.

      Richard DeemingR Offline
      Richard DeemingR Offline
      Richard Deeming
      wrote on last edited by
      #2

      Use a computed column: Specify Computed Columns in a Table - SQL Server | Microsoft Docs[^] If you want to continue using a trigger, you'll need to update it to account for the fact that a trigger can be fired for multiple rows at the same time:

      CREATE TRIGGER AfterInsert
      ON InspectionItems
      AFTER INSERT
      AS
      BEGIN
      SET NOCOUNT ON;

      UPDATE
          T
      SET
          InspectionItemNumber = 'T' + RIGHT('00000' + CAST(InspectionItemId AS VARCHAR(5)), 6)
      FROM
          InspectionItems As T
          INNER JOIN inserted As I
          ON I.InspectionItemId = T.InspectionItemId
      ;
      

      END
      GO


      "These people looked deep within my soul and assigned me a number based on the order in which I joined." - Homer

      "These people looked deep within my soul and assigned me a number based on the order in which I joined" - Homer

      S 1 Reply Last reply
      0
      • Richard DeemingR Richard Deeming

        Use a computed column: Specify Computed Columns in a Table - SQL Server | Microsoft Docs[^] If you want to continue using a trigger, you'll need to update it to account for the fact that a trigger can be fired for multiple rows at the same time:

        CREATE TRIGGER AfterInsert
        ON InspectionItems
        AFTER INSERT
        AS
        BEGIN
        SET NOCOUNT ON;

        UPDATE
            T
        SET
            InspectionItemNumber = 'T' + RIGHT('00000' + CAST(InspectionItemId AS VARCHAR(5)), 6)
        FROM
            InspectionItems As T
            INNER JOIN inserted As I
            ON I.InspectionItemId = T.InspectionItemId
        ;
        

        END
        GO


        "These people looked deep within my soul and assigned me a number based on the order in which I joined." - Homer

        S Offline
        S Offline
        simpledeveloper
        wrote on last edited by
        #3

        Thank you so much

        1 Reply Last reply
        0
        Reply
        • Reply as topic
        Log in to reply
        • Oldest to Newest
        • Newest to Oldest
        • Most Votes


        • Login

        • Don't have an account? Register

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • World
        • Users
        • Groups