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 1898492
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 17, 20262026-05-17T06:46:18+00:00 2026-05-17T06:46:18+00:00

I’ve got a question about reusing table data but a view won’t work in

  • 0

I’ve got a question about reusing table data but a view won’t work in this scenario as I have a parameter that needs to be passed in. Basically this part of the system requires a travellerid to be sent to the procedure and a list of arrangers returned for that specific traveller. There are around 7 business rules that are used to determine which arrangers can be returned and they are mutually exclusive, so in order to accommodate these optional rules I have used a series of UNIONS inside a derived query. This is working well, and the performance seems good across a fairly large database, however I need to reuse these rules (UNIONS) in about 4 other parts of the system.

I initially tried to create a VIEW with these UNIONS but that didn’t work due to the differing logic in each UNION and different parameter requirements, so I was thinking maybe a function could solve this issue? If I created a function that took @travellerid as a param and returned a list of arrangerid based on the business rules, would this be an ideal/fast solution? I am currently using UNION ALL and a DISTINCT in the outer query as this proved much faster than using UNION’s for the uniqueness of data.

Current Procedure with business rules below (SQL Server 2008):

CREATE PROCEDURE [dbo].[getArrangersForTraveller]
   @travellerid int
AS
  DECLARE @costcentreid int
  DECLARE @departmentid int

-- Shorthand the traveller costcentre and department for use in queries below
SET @costcentreid = (SELECT costcentreid FROM traveller WHERE id = @travellerid)
SET @departmentid = (SELECT departmentid FROM traveller WHERE id = @travellerid)


SELECT DISTINCT t.id, t.firstname, t.lastname, ti.name AS title, dv.preferred
FROM traveller t

INNER JOIN title ti ON t.titleid = ti.id
     INNER JOIN

     (

            -- Get Preferred Arrangers linked to Department Groups
            SELECT dg.arrangerid as id
            FROM departmentGroup dg 
                INNER JOIN department_departmentGroup ddg 
                ON (dg.id = ddg.departmentGroupId AND ddg.departmentid = @departmentid)

            UNION ALL

            -- Get Preferred Arrangers linked to Cost Centre Groups
            SELECT cg.arrangerid as id
            FROM costCentreGroup cg 
                INNER JOIN costcentre_costCentreGroup ccg 
                ON (cg.id = ccg.costCentreGroupId AND ccg.costcentreid = @costcentreid)

            UNION ALL

            -- If Cost Centre Group has a linked department and this department matches 
            -- the travel arrangers department then return these travel arrangers as well     
            SELECT t3.id
            FROM costCentreGroup cg1

                INNER JOIN costcentre_costCentreGroup ccg1 
                ON (cg1.id = ccg1.costCentreGroupId AND ccg1.costcentreid = @costcentreid) 

                INNER JOIN traveller t3  
                ON t3.departmentid = cg1.departmentid    

            WHERE  t3.accesslevelid > 1       

            UNION ALL

            -- Get Direct linked travel arrangers      
            SELECT t1.travelarrangerid as id
            FROM   travelarranger_traveller t1
            WHERE  t1.travellerid = @travellerid

            UNION ALL

            -- Get Cost Centre linked arrangers
            SELECT tc.travelarrangerid as id
            FROM   travelArranger_costcentre tc 
            WHERE  tc.costcentreid = @costcentreid

            UNION ALL

            -- Get Department linked arrangers
            SELECT td.travelarrangerid
            FROM   travelArranger_department td 
            WHERE  td.departmentid = @departmentid

            UNION ALL

            -- Get Company flagged arrangers 
            SELECT t2.id
            FROM   traveller t2
                   INNER JOIN company c ON t2.companyid = c.id

            WHERE  t2.accesslevelid > 1       
            AND ((c.allowTravelArrangerDepartmentAccess = 1 AND t2.departmentid = @departmentid)
            OR  (c.allowTravelArrangerCostCentreAccess = 1 AND t2.costcentreid = @costcentreid))

     ) as dv ON dv.id = t.id

WHERE t.accessLevelid > 1 -- arranger or manager
AND t.isenabled = 1
ORDER BY dv.preferred DESC, t.lastname, t.firstname;
  • 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-17T06:46:18+00:00Added an answer on May 17, 2026 at 6:46 am

    I initially tried to create a VIEW with these UNIONS but that didn’t work due to the differing logic in each UNION and different parameter requirements, so I was thinking maybe a Function could solve this issue? If i created a Function that took as a param @travellerid, and returned a list of arrangerid’s based on the business rules, would this be an ideal/fast solution?

    You’re thinking procedural/OO programming, but SQL is SET based.
    A function would work, but would ensure that an index could not be used when you use the function for decision criteria/etc. A non-materialized view is only slightly better; in SQL Server there’s the option to use an indexed view (AKA materialized view) but they are notoriously constrained. It goes against modular programming concepts, but SQL works better the less you try to modularize it and use only what you actually need to.

    I re-wrote your query, but noticed that the dv.preferred column is referenced in the outer query but isn’t present in the inner one. Being that dv is a conglomerate of various tables & logic, the id value being returned isn’t of any real value outside the inner query because you’d need to know which table the value came from. That said, here it is:

    SELECT t.id, t.firstname, t.lastname, ti.name AS title /*, dv.preferred */
      FROM TRAVELLER t
      JOIN title ti ON t.titleid = ti.id
     WHERE (EXISTS(SELECT NULL -- Get Preferred Arrangers linked to Department Groups
                     FROM departmentGroup dg 
                     JOIN department_departmentGroup ddg ON ddg.departmentGroupId = dg.id 
                                                        AND ddg.departmentid = @departmentid
                    WHERE dg.arrangerid = t.id)
        OR EXISTS(SELECT NULL -- Get Preferred Arrangers linked to Cost Centre Groups
                    FROM costCentreGroup cg 
                    JOIN costcentre_costCentreGroup ccg ON ccg.costCentreGroupId = cg.id 
                                                       AND ccg.costcentreid = @costcentreid
                   WHERE cg.arrangerid = t.id)
        OR EXISTS(SELECT NULL -- If Cost Centre Group has a linked department and this department matches the travel arrangers department then return these travel arrangers as well     
                    FROM costCentreGroup cg1
                    JOIN costcentre_costCentreGroup ccg1 ON ccg1.costCentreGroupId = cg1.id 
                                                        AND ccg1.costcentreid = @costcentreid
                    JOIN traveller t3 ON t3.departmentid = cg1.departmentid    
                                     AND  t3.accesslevelid > 1
                   WHERE t3.id = t.id)
        OR EXISTS(SELECT NULL  -- Get Direct linked travel arrangers    
                    FROM travelarranger_traveller t1
                   WHERE t1.travellerid = @travellerid
                     AND t1.travelarrangerid = t.id)
        OR EXISTS(SELECT NULL -- Get Cost Centre linked arrangers
                    FROM travelArranger_costcentre tc 
                   WHERE tc.costcentreid = @costcentreid
                     AND tc.travelarrangerid = t.id)
        OR EXISTS(SELECT NULL -- Get Department linked arrangers
                    FROM travelArranger_department td 
                   WHERE td.departmentid = @departmentid
                     AND td.travelarrangerid = t.id)
        OR EXISTS(SELECT NULL -- Get Company flagged arrangers 
                    FROM traveller t2
                    JOIN company c ON t2.companyid = c.id
                                  AND t2.accesslevelid > 1       
                   WHERE (   (c.allowTravelArrangerDepartmentAccess = 1 AND t2.departmentid = @departmentid)
                          OR (c.allowTravelArrangerCostCentreAccess = 1 AND t2.costcentreid = @costcentreid))
                     AND t2.id = t.id))
       AND t.accessLevelid > 1 -- arranger or manager
       AND t.isenabled = 1
    ORDER BY /*dv.preferred DESC,*/ t.lastname, t.firstname;
    

    Using a subquery (IN, EXISTS) will alleviate the duplicates issue that comes with using joins if there are more than one child record attached to the parent.

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

Sidebar

Related Questions

I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
I don't have much knowledge about the IPv6 protocol, so sorry if the question
I have some data like this: 1 2 3 4 5 9 2 6
this is what i have right now Drawing an RSS feed into the php,
I have this code to decode numeric html entities to the UTF8 equivalent character.
I have a French site that I want to parse, but am running into
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
I have this code: - (void)parser:(NSXMLParser *)parser foundCDATA:(NSData *)CDATABlock { NSString *someString = [[NSString
I have an MVC Razor view @{ ViewBag.Title = Index; var c = (char)146;
I want to construct a data frame in an Rcpp function, but when I

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.