Solved

vb.net dynamic SQL String

Posted on 2014-03-09
2
407 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
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

VMware Disaster Recovery and Data Protection

In this expert guide, you’ll learn about the components of a Modern Data Center. You will use cases for the value-added capabilities of Veeam®, including combining backup and replication for VMware disaster recovery and using replication for data center migration.

Question has a verified solution.

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

Suggested Solutions

Why is this different from all of the other step by step guides?  Because I make a living as a DBA and not as a writer and I lived through this experience. Defining the name: When I talk to people they say different names on this subject stuff l…
This article shows gives you an overview on SQL Server 2016 row level security. You will also get to know the usages of row-level-security and how it works
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…
Viewers will learn how to use the SELECT statement in SQL and will be exposed to the many uses the SELECT statement has.

825 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