+ Reply to Thread
Results 1 to 6 of 6

Thread: Conditional JOIN in Stored Procedure

  1. #1
    Administrator richyrich is a splendid one to behold richyrich is a splendid one to behold richyrich is a splendid one to behold richyrich is a splendid one to behold richyrich is a splendid one to behold richyrich is a splendid one to behold richyrich is a splendid one to behold richyrich's Avatar
    Join Date
    Mar 2008
    Location
    Somewhere only we know...
    Posts
    3,207
    Blog Entries
    14
    Real Name
    Rich
    Rep Power
    14

    Conditional JOIN in Stored Procedure

    I want to include a JOIN in my stored procedure based on a boolean parameter passed to it.

    I have tried this, but it is giving an error saying I have incorrect SQL syntax when I try and execute it.
    Code:
    FROM user_tasks
    LEFT JOIN user_tasks_links ON (user_tasks_links.todoref=user_tasks.todoref AND user_tasks_links.userref=@userref)
    LEFT JOIN users ON users.userref=user_tasks.cur_user
    IF (@CompanyOwn=False AND @CompanyBelow=False) THEN LEFT JOIN visitors ON visitors.userref=user_tasks.visitorref;
    END IF;
    
    Anyone know if this is possible?

  2. #2
    The Barnfather jmurrayhead has much to be proud of jmurrayhead has much to be proud of jmurrayhead has much to be proud of jmurrayhead has much to be proud of jmurrayhead has much to be proud of jmurrayhead has much to be proud of jmurrayhead has much to be proud of jmurrayhead has much to be proud of jmurrayhead has much to be proud of jmurrayhead has much to be proud of jmurrayhead's Avatar
    Join Date
    Mar 2008
    Location
    Reston, VA
    Posts
    4,547
    Blog Entries
    9
    Real Name
    Jason
    Rep Power
    22

    Try using a CASE statement instead.
    jmurrayhead
    If you agree, give me rep.
    If you like it here...throw us a few bones to help support us.


  3. #3
    Administrator richyrich is a splendid one to behold richyrich is a splendid one to behold richyrich is a splendid one to behold richyrich is a splendid one to behold richyrich is a splendid one to behold richyrich is a splendid one to behold richyrich is a splendid one to behold richyrich's Avatar
    Join Date
    Mar 2008
    Location
    Somewhere only we know...
    Posts
    3,207
    Blog Entries
    14
    Real Name
    Rich
    Rep Power
    14

    Nope...Get the same problem.

    It doesn't seem to like it in the middle of a statement.

  4. #4
    The Barnfather jmurrayhead has much to be proud of jmurrayhead has much to be proud of jmurrayhead has much to be proud of jmurrayhead has much to be proud of jmurrayhead has much to be proud of jmurrayhead has much to be proud of jmurrayhead has much to be proud of jmurrayhead has much to be proud of jmurrayhead has much to be proud of jmurrayhead has much to be proud of jmurrayhead's Avatar
    Join Date
    Mar 2008
    Location
    Reston, VA
    Posts
    4,547
    Blog Entries
    9
    Real Name
    Jason
    Rep Power
    22

    Scratch that, mate...it's not possible to use in the FROM clause. I'm thinking you're going to need a dynamic query.
    jmurrayhead
    If you agree, give me rep.
    If you like it here...throw us a few bones to help support us.


  5. #5
    Administrator richyrich is a splendid one to behold richyrich is a splendid one to behold richyrich is a splendid one to behold richyrich is a splendid one to behold richyrich is a splendid one to behold richyrich is a splendid one to behold richyrich is a splendid one to behold richyrich's Avatar
    Join Date
    Mar 2008
    Location
    Somewhere only we know...
    Posts
    3,207
    Blog Entries
    14
    Real Name
    Rich
    Rep Power
    14

    Quote Originally Posted by jmurrayhead View Post
    Scratch that, mate...it's not possible to use in the FROM clause. I'm thinking you're going to need a dynamic query.
    Can you create a dynamic FROM clause or do you have to create the whole SQL query from scratch?

  6. #6
    The Barnfather jmurrayhead has much to be proud of jmurrayhead has much to be proud of jmurrayhead has much to be proud of jmurrayhead has much to be proud of jmurrayhead has much to be proud of jmurrayhead has much to be proud of jmurrayhead has much to be proud of jmurrayhead has much to be proud of jmurrayhead has much to be proud of jmurrayhead has much to be proud of jmurrayhead's Avatar
    Join Date
    Mar 2008
    Location
    Reston, VA
    Posts
    4,547
    Blog Entries
    9
    Real Name
    Jason
    Rep Power
    22

    I don't think you can do anything like that in the FROM clause unless you're building an actual string that gets executed in the procedure, you build your query in code, or you use my dynamic stored procedure example in the SQL Code Examples forum.
    jmurrayhead
    If you agree, give me rep.
    If you like it here...throw us a few bones to help support us.


+ Reply to Thread

Similar Threads

  1. SQL Job or stored procedure
    By todd2006 in forum SQL Development
    Replies: 5
    Last Post: February 11th, 2009, 02:20 PM
  2. stored procedure
    By todd2006 in forum ASP Development
    Replies: 7
    Last Post: February 5th, 2009, 03:02 PM
  3. stored procedure
    By todd2006 in forum Microsoft SQL Server
    Replies: 1
    Last Post: February 5th, 2009, 01:25 PM
  4. If statement stored procedure
    By peebman2000 in forum SQL Development
    Replies: 32
    Last Post: May 23rd, 2008, 04:54 PM
  5. Dynamic Stored Procedure
    By jmurrayhead in forum Microsoft SQL Server
    Replies: 16
    Last Post: March 26th, 2008, 12:19 PM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

SEO by vBSEO