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. How to create a insert trigger on just one table?

How to create a insert trigger on just one table?

Scheduled Pinned Locked Moved Database
databasehelptutorialquestionannouncement
8 Posts 4 Posters 7 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.
  • M Offline
    M Offline
    Member236
    wrote on last edited by
    #1

    I have a table product, columns:id,date,product,productname I want to create a insert trigger, when I insert id,date,product, the productname will auto update in to the table(the product=productname... I don't know how to make the equation, please help me. sorry for my terrible English.

    CHill60C Richard DeemingR M 3 Replies Last reply
    0
    • M Member236

      I have a table product, columns:id,date,product,productname I want to create a insert trigger, when I insert id,date,product, the productname will auto update in to the table(the product=productname... I don't know how to make the equation, please help me. sorry for my terrible English.

      CHill60C Offline
      CHill60C Offline
      CHill60
      wrote on last edited by
      #2

      Why have two columns with the same data? Have you tried anything at all yet?

      1 Reply Last reply
      0
      • M Member236

        I have a table product, columns:id,date,product,productname I want to create a insert trigger, when I insert id,date,product, the productname will auto update in to the table(the product=productname... I don't know how to make the equation, please help me. sorry for my terrible English.

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

        CREATE TRIGGER (Transact-SQL) | Microsoft Docs[^]

        CREATE TRIGGER YourTriggerName
        ON YourTableName
        AFTER insert, update
        As
        BEGIN
        SET NOCOUNT ON;

        If UPDATE(FK)
        BEGIN
            UPDATE
                T
            SET
                RedundantDuplicateColumn = T2.Name
            FROM
                YourTable As T
                INNER JOIN inserted As I
                ON I.PK = T.PK
                LEFT JOIN YourOtherTable As T2
                ON T2.PK = T.FK
            ;
        END;
        

        END

        Replace the dummy table and column names with the real table and column names. Remember that triggers will fire once per batch, not once per row. There could be multiple rows in the inserted table. But as CHill60 said, duplicating the product name in both tables seems like a bad design. If you have the ID of the product, you can simply join to the product table to get the name on demand.


        "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

        1 Reply Last reply
        0
        • M Member236

          I have a table product, columns:id,date,product,productname I want to create a insert trigger, when I insert id,date,product, the productname will auto update in to the table(the product=productname... I don't know how to make the equation, please help me. sorry for my terrible English.

          M Offline
          M Offline
          Mycroft Holmes
          wrote on last edited by
          #4

          This is a REALLY BAD design, instead of having a separate table for productname create a view

          create view vwProductName
          as
          select distinct product as productname from product

          Never underestimate the power of human stupidity RAH

          CHill60C M 2 Replies Last reply
          0
          • M Mycroft Holmes

            This is a REALLY BAD design, instead of having a separate table for productname create a view

            create view vwProductName
            as
            select distinct product as productname from product

            Never underestimate the power of human stupidity RAH

            CHill60C Offline
            CHill60C Offline
            CHill60
            wrote on last edited by
            #5

            Am I missing something ... where's the table for the ProductName mentioned?

            M 1 Reply Last reply
            0
            • M Mycroft Holmes

              This is a REALLY BAD design, instead of having a separate table for productname create a view

              create view vwProductName
              as
              select distinct product as productname from product

              Never underestimate the power of human stupidity RAH

              M Offline
              M Offline
              Member236
              wrote on last edited by
              #6

              It's an ERP bill, customer want to input the batch number, but they don't want to use any rules, just want to fetch the bill number into that field.

              M 1 Reply Last reply
              0
              • CHill60C CHill60

                Am I missing something ... where's the table for the ProductName mentioned?

                M Offline
                M Offline
                Mycroft Holmes
                wrote on last edited by
                #7

                OP wrote:

                the productname will auto update in to the table(the product=productname.

                So yes I think you missed this.

                Never underestimate the power of human stupidity RAH

                1 Reply Last reply
                0
                • M Member236

                  It's an ERP bill, customer want to input the batch number, but they don't want to use any rules, just want to fetch the bill number into that field.

                  M Offline
                  M Offline
                  Mycroft Holmes
                  wrote on last edited by
                  #8

                  The comments still stand - there should be no reason to store the productname in a different table especially as it is user input and free form.

                  Never underestimate the power of human stupidity RAH

                  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