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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 19, 20262026-05-19T10:53:05+00:00 2026-05-19T10:53:05+00:00

I want to know if I can add a join in to a SQL

  • 0

I want to know if I can add a join in to a SQL statement based on a an IF statement. I’ve got a query with three variables and I only want to include the join if the third variable is actually filled in. This is my query:

-- Returns all new woksteps for a given time period
-- Includes the ability to narrow down to a specific arrangment

DECLARE @start_added_date DATETIME
DECLARE @end_added_date DATETIME
DECLARE @arrang_part_key INT

SET @start_added_date = '1/4/2011'
SET @end_added_date = '1/5/2011'
SET @arrang_part_key = '1230631'

-- Compensate for CST in the database
SET @end_added_date = DATEADD(DAY, 1, @end_added_date)

SELECT DISTINCT
  PWS.Part_Work_Step_Key,
  O.Operation_Code,
  WS.Work_Step,
  PWS.Sort_Order,
  U.User_ID,
  PWS.Added_Date,
  PWS.Cycle_Time,
  ET.ECR_Type,
  E.ECR_No,
  PWS.Effective_Date,
  P.Part_No,
  P.Revision,
  P.Name
FROM Part_V_Part_Work_Step PWS
JOIN Part_V_Work_Step WS
  ON WS.Work_Step_Key = PWS.Work_Step_Key
JOIN Part_V_Operation O
  ON O.Operation_Key = PWS.Operation_Key
JOIN User U
  ON U.User_No = PWS.Added_By
JOIN Part_V_ECR E
  ON E.ECR_Key = PWS.Effective_ECR_Key
JOIN Part_V_ECR_Type ET
  ON E.ECR_Type_Key = ET.ECR_Type_Key
JOIN Part_V_Part_Work_Step_BOM PWSB
  ON PWSB.Part_Work_Step_Key = PWS.Part_Work_Step_Key
JOIN Part_V_BOM B
  ON B.BOM_Key = PWSB.BOM_Key
JOIN Part_V_Part P
  ON P.Part_Key = B.Part_Key

IF @arrang_part_key IS NOT NULL
BEGIN
JOIN 
(
    SELECT
      AGP.Part_Key
    FROM Part_V_Flat_BOM FB
    JOIN Part_V_Part AGP
      ON AGP.Part_Key = FB.Component_Part_Key
    JOIN Part_V_Part_Group PG
      ON AGP.Part_Group_Key = PG.Part_Group_Key
     AND PG.Part_Group = 'Engineering Group'
    WHERE FB.Part_Key = @arrang_part_key
) AG
  ON AG.Part_Key = P.Part_Key
END

WHERE PWS.Part_Key IS NULL
  AND PWS.Added_Date BETWEEN @start_added_date AND @end_added_date
  AND PWS.Active = 1

ORDER BY
  O.Operation_Code,
  PWS.Sort_Order

But when I run the query I get this as my result:

Error: Incorrect syntax near the keyword 'JOIN'
Error: Incorrect syntax near 'AG'

I am beginning to think that this isn’t the right approach to solving my problem. The query is trying to kill two birds with one stone, return all results for a given time frame, and then allow for filtering down to a specific arrangement if supplied.

  • 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-19T10:53:06+00:00Added an answer on May 19, 2026 at 10:53 am

    OK, so adding a join by an IF block simply cannot work. But I’ve found a way to do my query as well as utilize as much re-usable code as I could think of.

    There were two key elements to the query:

    1. Query the work step table all the time
    2. Filter by an arrangement occasionally

    To accomplish this I used a combination of both a Table Variable and a CTE (Common Table Expression) and then placed two queries in to my if..else block.

    The new query looks like this:

    -- Returns all new woksteps for a given time period
    -- Includes the ability to narrow down to a specific arrangment
    
    DECLARE @start_added_date DATETIME
    DECLARE @end_added_date DATETIME
    DECLARE @arrang_part_key INT
    
    SET @start_added_date = '1/4/2011'
    SET @end_added_date = '1/5/2011'
    SET @arrang_part_key = '1230631'
    --SET @arrang_part_key = NULL
    
    -- Compensate for CST in the database
    SET @end_added_date = DATEADD(DAY, 1, @end_added_date)
    
    DECLARE @Work_Step_Table TABLE
    (
      Customer_Number INT,
      Part_Work_Step_Key INT,
      Operation_Code VARCHAR(255),
      Work_Step VARCHAR(255),
      Sort_Order INT,
      User_Id VARCHAR(255),
      Added_Date DATETIME,
      Cycle_Time Float,
      ECR_Type VARCHAR(255),
      ECR_No INT,
      Effective_Date DATETIME,
      Part_Key INT,
      Part_No VARCHAR(255),
      Revision VARCHAR(255),
      Name VARCHAR(255)
    )
    
    INSERT INTO @Work_Step_Table
    SELECT DISTINCT
      P.Customer_Number,
      PWS.Part_Work_Step_Key,
      O.Operation_Code,
      WS.Work_Step,
      PWS.Sort_Order,
      U.User_ID,
      PWS.Added_Date,
      PWS.Cycle_Time,
      ET.ECR_Type,
      E.ECR_No,
      PWS.Effective_Date,
      P.Part_Key,
      P.Part_No,
      P.Revision,
      P.Name
    FROM Part_V_Part_Work_Step PWS
    JOIN Part_V_Work_Step WS
      ON WS.Work_Step_Key = PWS.Work_Step_Key
    JOIN Part_V_Operation O
      ON O.Operation_Key = PWS.Operation_Key
    JOIN User U
      ON U.User_No = PWS.Added_By
    JOIN Part_V_ECR E
      ON E.ECR_Key = PWS.Effective_ECR_Key
    JOIN Part_V_ECR_Type ET
      ON E.ECR_Type_Key = ET.ECR_Type_Key
    JOIN Part_V_Part_Work_Step_BOM PWSB
      ON PWSB.Part_Work_Step_Key = PWS.Part_Work_Step_Key
    JOIN Part_V_BOM B
      ON B.BOM_Key = PWSB.BOM_Key
    JOIN Part_V_Part P
      ON P.Part_Key = B.Part_Key
    
    WHERE PWS.Part_Key IS NULL
      AND PWS.Added_Date BETWEEN @start_added_date AND @end_added_date
      AND PWS.Active = 1
    
    
    IF (@arrang_part_key > 0)
    BEGIN 
    ;WITH Arrangement_Groups (PCN, Arrangement_Part_Key, Component_Part_Key) AS
    (
        SELECT
          AGP.Customer_Number,
          FB.Part_Key AS Arrangement_Part_Key,
          AGP.Part_Key AS Component_Part_Key      
        FROM Part_V_Flat_BOM FB
        JOIN Part_V_Part AGP
          ON AGP.Part_Key = FB.Component_Part_Key
        JOIN Part_V_Part_Group PG
          ON AGP.Part_Group_Key = PG.Part_Group_Key
         AND PG.Part_Group = 'Engineering Group'
        WHERE FB.Part_Key = @arrang_part_key
    )
    SELECT
      *
    FROM @Work_Step_Table WST
    JOIN Arrangement_Groups AG 
      ON AG.Component_Part_Key = WST.Part_Key  
    ORDER BY
      WST.Operation_Code,
      WST.Sort_Order
    END
    
    ELSE
     BEGIN
      SELECT
        *
      FROM @Work_Step_Table WST
      ORDER BY
        WST.Operation_Code,
        WST.Sort_Order
     END
    

    The table variable will always be created and populated with the relevant records for the date range. The IF..Else block will check for an additional arrangement key and create the additional CTE if needed and filter accordingly.

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

Sidebar

Related Questions

I want to know I can dynamically modify an existing Crystal Report (using C#
I want to know which tool can be used to measure the cyclomatic complexity
I want to know if i can create a custom google maps application,on which
I want to know how I can run a method in a separate thread?
I want to know which *.config file the ConfigurationManager is using. How can I
Just out of curiosity: I know I can tell the compiler if I want
Want to know what the stackoverflow community feels about the various free and non-free
I want to know what a virtual base class is and what it means.
I want to know what are the options to do some scripting jobs in
I want to know what exactly is the sequence of calls that occurs when

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.