[2 days left] What’s wrong with your cloud strategy? Learn why multicloud solutions matter with Nimble Storage.Register Now

x
?
Solved

vb.net dynamic SQL String

Posted on 2014-03-09
2
Medium Priority
?
421 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 2000 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

[Webinar] Lessons on Recovering from Petya

Skyport is working hard to help customers recover from recent attacks, like the Petya worm. This work has brought to light some important lessons. New malware attacks like this can take down your entire environment. Learn from others mistakes on how to prevent Petya like worms.

Question has a verified solution.

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

It is possible to export the data of a SQL Table in SSMS and generate INSERT statements. It's neatly tucked away in the generate scripts option of a database.
One of the most important things in an application is the query performance. This article intends to give you good tips to improve the performance of your queries.
Familiarize people with the process of utilizing SQL Server functions from within Microsoft Access. Microsoft Access is a very powerful client/server development tool. One of the SQL Server objects that you can interact with from within Microsoft Ac…
Via a live example, show how to extract information from SQL Server on Database, Connection and Server properties
Suggested Courses

656 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