0

I have a long stored procedure and I would like to make a slight modification to the procedure without having to create a new one(for maintenance purposes).

Is it possible to use a IF or CASE in the FROM statement of the select statement to join other tables?

Like this:

from tableA a
join tableB b a.indexed = c.indexed
IF @Param='Y'
BEGIN
    join tableC c a.indexed = c.indexed
END

It didn't seem to work for me. But I am wondering if this is even possible and/or if this even makes sense to do.

Thanks.

2
  • You can use a CASE within a statement since it is an expression that results in a value. It could be coerced to make, say, an OUTER JOIN behave in various ways. You might be better off using a simple IF to invoke one of two SELECT INTO statements and then using the resulting temporary table in the balance of the stored procedure.
    – HABO
    Mar 23, 2012 at 16:20
  • The CASE statement can return values for a comparison or a condition - but it cannot return fragments of SQL to be executed..
    – marc_s
    Mar 23, 2012 at 16:58

5 Answers 5

3

No, it is not possible. You can only accomplish this through the use of dynamic SQL.

The Curse and Blessings of Dynamic SQL

An Intro to Dynamic SQL

I would not advise using Dynamic SQL, there are most likely better ways to perform this operation but you would have to provide more info.

2
  • I will take a look at this as well.
    – Eric
    Mar 23, 2012 at 16:39
  • I think will try to handle this in with my joins and only return what I need. If both results are really that different I will just use separate sp's. Thanks
    – Eric
    Mar 23, 2012 at 16:46
0

You can achieve something like it if you have a left outer join

Consider

declare @param bit = 1

select a.*, b.*, c.* from a
    inner join b on a.id = b.a_id
    left outer join c on b.id = c.b_id and @param = 1

This will return all columns from a, b, c.

Now try with

declare @param bit = 0

This will return all columns from a and b, and nulls for columns of c.

It won't work if both joins are inner.

0

No this is not possible. Your best bet would probably be to select from both tables and only include the data your care about. If you provide an example of what you are trying to do I can provide a better answer.

Attempt at an example:

SELECT t1.id, COALESCE(t2.name, t3.name)
FROM Table1 as t1
LEFT JOIN Table2 as t2
ON t1.id = t2.id
LEFT JOIN Table2 as t3
ON t1.id = t3.id
2
  • I am running multiple crystal reports were I have the same basic data returning except for one report that needs to join to multiple tables at the end of the stored procedure. I currently have 2 sp's with one being for report with the special case but I would like to just have a parameter in sp for the changes. I am going to look over the code some more to see if there are other options as well.
    – Eric
    Mar 23, 2012 at 16:37
  • This is probably what I'll do since I don't want to use dynamic sql in the this case.
    – Eric
    Mar 23, 2012 at 16:44
0

While what you proposed is not possible, you can play with your where conditions:

from tableA a
inner join tableB b ON a.indexed = c.indexed 
 left join tableC c ON a.indexed = c.indexed AND 1 = CASE @Param WHEN 'Y' THEN 1 ELSE 0 END

More performant would be to just doing a big

IF @Param='Y' THEN 
    from tableA a
    inner join tableB b ON a.indexed = c.indexed 
ELSE
    from tableA a
   inner join tableB b ON a.indexed = c.indexed 
    left join tableC c ON a.indexed = c.indexed 
0

You haven't revealed you SELECT clause. The essence of what you want is as follows:

SELECT indexed
  FROM tableA 
INTERSECT 
SELECT indexed
  FROM tableB
INTERSECT 
SELECT indexed
  FROM tableC
 WHERE @Param = 'Y'

Then use this table expression as dictated by your SELECT clause e.g. say you only want to project tableA:

WITH T
     AS
     (
      SELECT indexed
        FROM tableA 
      INTERSECT 
      SELECT indexed
        FROM tableB
      INTERSECT 
      SELECT indexed
        FROM tableC
       WHERE @Param = 'Y'
     )
SELECT * 
  FROM tableA
 WHERE indexed IN ( SELECT indexed FROM T );

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.