Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>You could rewrite it as one single select of 4-part UNION ALL.</p> <p>I also notice that your conditions are not comprehensive, not sure if that is intentional.</p> <p>In the 2nd branch of <code>@PoolID = 0</code> , the tests are specifically <code>@Bevoegdheid &lt; 3</code> and <code>@Bevoegdheid &gt;= 3</code> (instead of ELSE ) which will do nothing if <code>@Bevoegdheid IS NULL</code></p> <p>The optimizer will only run the branch that fits the criteria marked with &lt;&lt;&lt;</p> <pre><code>ALTER PROCEDURE [dbo].[Pool_select] @PartnerCode nvarchar(8), @GebrID int, @PoolID int, @Bevoegdheid int -- WITH RECOMPILE -- &lt;&lt; may need this AS SELECT p.*, pr.poolrecht FROM racpPool p, racpPoolrecht pr WHERE p.poolid = pr.poolid and p.PartnerCode = @PartnerCode and pr.poolrecht &gt; 0 and p.actief = 1 and pr.GebrID = @GebrID AND @PoolID = 0 and @Bevoegdheid &lt; 3 --- &lt;&lt;&lt; UNION ALL SELECT *, NULL FROM racpPool p WHERE standaardpool = 1 and partnercode = @PartnerCode AND @PoolID = 0 and IsNull(@Bevoegdheid,4) &gt;= 3 --- &lt;&lt;&lt; UNION ALL SELECT p.*, pr.poolrecht FROM racpPool p, racpPoolrecht pr WHERE p.poolid = pr.poolid and p.PartnerCode = @PartnerCode and pr.poolrecht &gt; 0 and p.actief = 1 and pr.GebrID = @GebrID and p.PoolID = @PoolID AND Isnull(@PoolID,-1) &lt;&gt; 0 AND @Bevoegdheid &lt; 3 --- &lt;&lt;&lt; UNION ALL SELECT *, NULL FROM racpPool p WHERE PoolID = @PoolID AND Isnull(@PoolID,-1) &lt;&gt; 0 AND @Bevoegdheid &gt;= 3 --- &lt;&lt;&lt; </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload