Link to home
Start Free TrialLog in
Avatar of SimonPrice33
SimonPrice33

asked on

vb.net dynamic SQL String

Hi Experts,

I am trying to see if there is a way where I can complete this search via my VB.Net application, or SQL Server without having multiple if functions

The application has a selection criteria, however it is not compulsory that every item is filled out depending on the search they want to complete.

the SQL that would complete this is

select  w.WorkOrderNumber, w.WorkOrderType, r.result,
d.DeptDescription, u.UserID, w.DatetobeStarted, w.DatetobeFinished 
from WorkOrders.WorkOrders w 
left join Risks.RAM  r on r.RiskID = w.RiskID 
left join WorkOrders.Dept d on d.DeptID = w.DeptID 
left join Users.Users u on w.Assignedto = u.UserIDNumber 
where 
(u.UserIDNumber is null or (UserIDNUmber = 1003)) 
and (d.DeptID is null or(d.DeptID  = 1))
and (w.FaultID is null or (w.FaultID = 4))
and (r.Result  is null or (r.Result = 2))
and (w.Status is null or (w.Status = 9))
and (w.DatetobeStarted is null or (w.DatetobeStarted = '2014-03-04'))
and (w.DatetobeFinished is null or (w.DatetobeFinished = '2014-03-04'))

Open in new window


What I am trying to avoid is multiple if statements \ conditions for every possibility of the search

thanks
Simon
ASKER CERTIFIED SOLUTION
Avatar of slinkygn
slinkygn
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of SimonPrice33
SimonPrice33

ASKER

that's what I was hoping to be able to avoid... ohwell..

lots of conditions to write for me then...

thank you