We help IT Professionals succeed at work.
Get Started

cte and where condition

137 Views
Last Modified: 2015-04-17
My code runs as below(more columns are there but eliminated for space )
Create function Getresults(@adminID varchar(20),@adminrole int,@condition nvarchar(max))
returns table
as
(
Select a.tno,
            case @adminrole
                when 1 then 1
                when 3 then
                case
                 when a.status in('setup','process') then 1 else 0
                end
          end as rolestatus,

         case when a.requestortype ='bulkprocess' then 'N/A' 
                 else a.requestorno 
         end as requestorno,
         case when (fnallowedstate(@adminid) = 1 then 'active' else 'inactive'as state,
         b.starttime,
         b.endtime
from Rules A join ruledetails B on a.ruleid = b.ruleid(this is guid)
where @adminrole in(1,3,2,4)

)

Open in new window

i want to add more conditions to the where clause but the conditions are based on the calculated columns in the above query. like

where @adminrole in(1,3,2,4) and state = 'active', I created a CTE and added the condition. but that is not being accepted as part of code review. reason being CTEs cannot be optimized and they take lot of memory. The query returns around 300,000 rows with out second condition in where clause.

If i take the result of above query in a temp table or query the function result i can achieve what i need.But this is taking some time which i wanted to eliminate.  i wanted to avoid retrieving unwanted rows in the first place. How to go about this without CTE?

Any help is appreciated. Thanks for your time.
Comment
Watch Question
Freelance IT Consultant
CERTIFIED EXPERT
Commented:
This problem has been solved!
Unlock 1 Answer and 1 Comment.
See Answer
Why Experts Exchange?

Experts Exchange always has the answer, or at the least points me in the correct direction! It is like having another employee that is extremely experienced.

Jim Murphy
Programmer at Smart IT Solutions

When asked, what has been your best career decision?

Deciding to stick with EE.

Mohamed Asif
Technical Department Head

Being involved with EE helped me to grow personally and professionally.

Carl Webster
CTP, Sr Infrastructure Consultant
Ask ANY Question

Connect with Certified Experts to gain insight and support on specific technology challenges including:

  • Troubleshooting
  • Research
  • Professional Opinions
Did You Know?

We've partnered with two important charities to provide clean water and computer science education to those who need it most. READ MORE