Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9125137
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T06:43:56+00:00 2026-06-17T06:43:56+00:00

I have below sample format table structure: CUSTOMER table CUSTID INT,//PRIMARY KEY CUSTDATA VARCHAR

  • 0

I have below sample format table structure:

CUSTOMER table

CUSTID INT,//PRIMARY KEY
CUSTDATA VARCHAR

EMPLOYEE table

EMPID INT,//PRIMARY KEY
EMPDATA VARCHAR

ITEM table

ITEMID INT,//PRIMARY KEY
CUSTID INT,//REFERENCE KEY
ITEMTYPE INT //1 FOR CUSTOMER AND 2 FOR EMPLOYEE
EMPID INT

MERGEDATA table

MERGEID INT,
CUSTDATA VARCHAR,
EMPDATA VARCHAR
ACTION VARCHAR

Above I have four tables (CUSTOMER, EMPLOYEE, ITEM and MERGEDATA), now I want to create SQL Server trigger on the ITEM table for INSERT/UPDATE/DELETE events, so whenever activity is done on the ITEM table it populates my MERGEDATA table with the action done on ITEM table.

So if any new rows get inserted into the ITEM table a new entry goes into the MERGEDATA table depending on the CUSTOMER or EMPLOYEE entries, like if there is any entry from customer it will add one entry in the ITEM table as well as in CUSTOMER table as below

CUSTOMER table

123, TESTCUSTOMER

ITEM table

1, 123, 1, 0

And same goes for Employee table too, however in the ITEM table row will be like 1, 456, 0, 2.

Now I want to write if condition in Trigger that if there is entry from CUSTOMER table

IF (ITEMTYPE == 1)
//Entry in MERGEDATA will as below

MERGEDATA table

1, 123, 0, ACTION (will depend on type of trigger event trigger if INSERT(ACTION = INS), for update (UPDATE = UPD) etc

So in short I am looking below things in trigger:

  • How to write IF condition in trigger
  • How to do inner joins from other table in trigger

Please suggest!!

Thanks

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-17T06:43:57+00:00Added an answer on June 17, 2026 at 6:43 am

    My suggestion would be:

    create three separate triggers – one for each operation. This makes it easier to determine the Action, and it also gives you the flexibility to turn on/off each operation individually. Furthermore, the trigger code will be smaller, easier to understand, easier to maintain.

    Given that design, your trigger will be dead simple:

    CREATE TRIGGER trg_Item_Insert 
    ON dbo.Item FOR INSERT
    AS 
       -- first insert customer-related data - if any
       INSERT INTO dbo.MergeTable(CUSTDATA, EMPDATA, ACTION)
          SELECT CustData, EmptyData, 'INSERT'
          FROM Inserted
          WHERE ITEMTYPE = 1
    
       -- then insert employee-related data - if any
       INSERT INTO dbo.MergeTable(CUSTDATA, EMPDATA, ACTION)
          SELECT CustData, EmptyData, 'INSERT'
          FROM Inserted
          WHERE ITEMTYPE = 2
    

    When you create three separate triggers, just replace the 'INSERT' value for Action with 'Update' and 'Delete' in those other triggers.

    What I have trouble with is:

    • what columns from Item should be stored into what column in MergeData ?
    • your columns in Item or mostly INT – your columns in MergeData are Varchar – what kind of “conversion” do you want to apply here?

    For more details, see the excellent SQL Server Books Online documentation (freely available!) on CREATE TRIGGER

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have data in a mysql table in long / tall format (described below)
I have a database with sample data represented by Table 1 below. How do
I have below sample data. AID Date Title ----- ---------- ------ 1 2011-12-12 test1
I have a IObservable [named rows in the sample below] from Reactive extensions framework
Below I have written a sample program that I have written to learn about
I have the following sample dataset (below and/or as CSVs here: http://goo.gl/wK57T ) which
I have posted my class below and with sample data. I need to pass
I have written grammar for a language (sample code below) //this is a procedure
I have a sample xml file which looks like the one below: <Root> <SubOne>
I have a plot (sample code pasted below) that I am trying to add

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.