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

  • Home
  • SEARCH
  • 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 7598543
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T22:25:54+00:00 2026-05-30T22:25:54+00:00

So I have a work order table with 3 fields relevant to the question

  • 0

So I have a work order table with 3 fields relevant to the question being asked here
(dbo.workorder with some sample values):

location | supervisor | ownergroup
ABC-123  | JSMITH     | ALPHA
XYZ-987  | JDOE       | OMEGA
ABC-123  | NULL       | NULL
XYZ-987  | NULL       | NULL

The last two rows are to show that sometimes the supervisor/ownergroup is not filled out when inserting a workorder row which leads to the question! I have another table called “roles” with rows such as below:

role          | value
ABCSupervisor | JSMITH
ABCOwnergroup | ALPHA
XYZSupervisor | JDOE
XYZOwnergroup | OMEGA

As you can see from the “roles” table, a role is made up of the first 3 letters of the location (ALWAYS) plus the word Supervisor or Ownergroup. When the 3rd and 4th rows of the “workorder” table are inserted, I’d like to develop a trigger that would attempt to fill in the values for Supervisor/Ownergroup IF there is a match in the “roles” table. If there isn’t a supervisor/ownergroup for that location prefix, then it should default to a set value (let’s say supervisor=’super’ and ownergroup=’og’). Here’s what I have so far, though perhaps a different approach would be better:

CREATE TRIGGER [dbo].[OwnergroupSupervisor]
   ON  [dbo].[workorder]
   AFTER INSERT,UPDATE
AS 
BEGIN
    SET NOCOUNT ON;

    UPDATE wo
        SET wo.ownergroup= (THIS IS WHERE I NEED HELP)

    FROM dbo.workorder AS wo INNER JOIN inserted AS i ON wo.wonum=i.wonum
    WHERE wo.ownergroup IS NULL
END
GO

I’m guessing an IF EXISTS or a CASE of some sort? Probably involving something like LEFT(wo.location,3)+’Ownergroup’ perhaps?

Any and all help is greatly appreciated! Thanks!


Clarification:
wonum is the primary key for the “workorder” table. I am joining the “inserted” table (which contains the rows that are being inserted or updated) to the “workorder” table so that I am only updating the new/updated rows and not the entire “workorder” table. Nothing in my question right now has anything to do with the “roles” table. See update 1 below for a better understanding of what I’m doing…


Update 1:

I’ve worked out one solution, but if there is nothing in the “roles” table, it just leaves the supervisor/ownergroup null instead of changing it to a default value. I can deal with this for now, but would like a better option. Here’s what I have:

CREATE TRIGGER [dbo].[OwnergroupSupervisor]
   ON  [dbo].[workorder]
   AFTER INSERT,UPDATE
AS 
BEGIN
    SET NOCOUNT ON;

    UPDATE wo
        SET wo.ownergroup=(SELECT value FROM roles WHERE role=LEFT(wo.location,3)+'Ownergroup')     
    FROM dbo.workorder AS wo INNER JOIN inserted AS i ON wo.wonum=i.wonum
    WHERE wo.ownergroup IS NULL

    UPDATE wo
        SET wo.supervisor=(SELECT value FROM roles WHERE role=LEFT(wo.location,3)+'Supervisor')     
    FROM dbo.workorder AS wo INNER JOIN inserted AS i ON wo.wonum=i.wonum
    WHERE wo.supervisor IS NULL
END

I have not worked out what would happen if two rows were found in the roles table (although I’m pretty sure the application restricts this so it should be OK). But as I mentioned above, this just keeps the supervisor/ownergroup NULL if no role is found in the roles table.

  • 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-05-30T22:25:56+00:00Added an answer on May 30, 2026 at 10:25 pm

    How you can update the workorder table based on information in roles is as below:

    update wo
    set ownergroup = isnull(r.value, 'og')
    from workorder wo
      left outer join roles r on r.role = left(wo.location, 3) + 'Ownergroup'
    where wo.ownergroup is null
    

    Then you can do a similar update for the Supervisor role.

    In theory, the full answer is something like:

    create trigger [dbo].[ownergroupsupervisor]
       on  [dbo].[workorder]
       after insert,update
    as 
    begin
        set nocount on;
    
        update wo
        set wo.ownergroup= isnull(r.value, 'og')
        from dbo.workorder as wo 
          inner join inserted as i on wo.wonum=i.wonum
          left outer join roles r on r.role = left(wo.location, 3) + 'Ownergroup'
        where wo.ownergroup is null
    
        -- Repeat for Supervisor
    end
    go
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

In order to work with decimal data types, I have to do this with
I have to work on an old 1.3 JVM and I'm asked to create
I have to work on several VB6 legacy projects and despite some good VB6
I have to work on some code that's using generic lists to store a
I have a string column which contains some numeric fields, but a lot are
I have a simple table (product) with fields id, label. I want to get
I have a products table, with the fields product, category and cost, of type
I have a product order table in mysql. It's like this: create table `order`
I have the following in Entity Framework . Table - Country Fields List item
I have multiple text fields that each have onChange=submit(); in order to save the

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.