Click here to monitor SSC
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


Union With IF


Union With IF

Author
Message
nandu.chowdary412
nandu.chowdary412
SSC Rookie
SSC Rookie (28 reputation)SSC Rookie (28 reputation)SSC Rookie (28 reputation)SSC Rookie (28 reputation)SSC Rookie (28 reputation)SSC Rookie (28 reputation)SSC Rookie (28 reputation)SSC Rookie (28 reputation)

Group: General Forum Members
Points: 28 Visits: 111
Hi
LutzM
LutzM
SSCertifiable
SSCertifiable (7K reputation)SSCertifiable (7K reputation)SSCertifiable (7K reputation)SSCertifiable (7K reputation)SSCertifiable (7K reputation)SSCertifiable (7K reputation)SSCertifiable (7K reputation)SSCertifiable (7K reputation)

Group: General Forum Members
Points: 7001 Visits: 13559
Instead of

Union
If(@NPI is not null or @TIN IS Not null)
Begin

You'll need to remove the If condition as well as the BEGIN ... END.
Add the following line to the WHERE Condition of each of the threee union statements you want to exclude:
AND ISNULL(@NPI,@TIN) IS NOT NULL

e.g. the first "conditional union" would look like
Where Cl.keyvaluechar IN (
Select Distinct claim_num from dw..claim where svc_provider_npi=@NPI or Payto_Provider_NPI=@NPI
)
AND ISNULL(@NPI,@TIN) IS NOT NULL



But the question is: Why do you need to do the UNION in the first place?
You could run each statement individually as a separate INSERT into #temptable.
If you want to exclude duplicates, you could add
AND NOT EXISTS (SELECT 1 FROM #temptable WHERE [add the check you want to perform])

In that scenario the section
If(@NPI is not null or @TIN IS Not null)
Begin
is valid and then UNION needs to be removed and separa Insert into #temptable need to be added.



Lutz
A pessimist is an optimist with experience.

How to get fast answers to your question
How to post performance related questions
Links for Tally Table , Cross Tabs and Dynamic Cross Tabs , Delimited Split Function
nandu.chowdary412
nandu.chowdary412
SSC Rookie
SSC Rookie (28 reputation)SSC Rookie (28 reputation)SSC Rookie (28 reputation)SSC Rookie (28 reputation)SSC Rookie (28 reputation)SSC Rookie (28 reputation)SSC Rookie (28 reputation)SSC Rookie (28 reputation)

Group: General Forum Members
Points: 28 Visits: 111
Apologies for delay..Much appreciated..thanks
Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum

































































































































































SQLServerCentral


Search