Solved

vb.net dynamic SQL String

Posted on 2014-03-09
2
410 Views
Last Modified: 2014-03-09
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
0
Comment
Question by:SimonPrice33
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
2 Comments
 
LVL 6

Accepted Solution

by:
slinkygn earned 500 total points
ID: 39915949
Not sure you can avoid that -- a conditional is a conditional.  Obviously you don't need an if/case with the full SQL for every possibility; you can just construct the string and append certain parts of the string (each of the "and"s in the WHERE clause) with the ifs -- so sqlString=sqlString + (deptIdIsFilledIn() ? "and d.DeptId = 1" : "") or the like -- but if there's a way to avoid conditionals altogether it's not coming to me.
0
 

Author Closing Comment

by:SimonPrice33
ID: 39915979
that's what I was hoping to be able to avoid... ohwell..

lots of conditions to write for me then...

thank you
0

Featured Post

Free learning courses: Active Directory Deep Dive

Get a firm grasp on your IT environment when you learn Active Directory best practices with Veeam! Watch all, or choose any amount, of this three-part webinar series to improve your skills. From the basics to virtualization and backup, we got you covered.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
sql server cross db update 2 20
Telerik RadEditor Control Save 8 15
Need some alteration to below mention query 2 12
Adjust the codes 3 35
For both online and offline retail, the cross-channel business is the most recent pattern in the B2C trade space.
This article shows how to deploy dynamic backgrounds to computers depending on the aspect ratio of display
Via a live example, show how to set up a backup for SQL Server using a Maintenance Plan and how to schedule the job into SQL Server Agent.
Viewers will learn how the fundamental information of how to create a table.

733 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question