Solved

10774: alternatives to the OR sentence

Posted on 2014-11-20
6
79 Views
Last Modified: 2014-11-20
Hi experts:

i have this query
/****** Script for SelectTopNRows command from SSMS  ******/
SELECT
      [companyname]
      ,[contactname]
      ,[contacttitle]
      ,[address]
      ,[city]
      ,[country]
      ,[phone]
      ,[fax]
  FROM [TSQL2012].[Production].[Suppliers]
  where city = 'Tokyo' OR city = 'Osaka' OR city = 'London'

The user may enter 1, 2, 3 or n city to do the search. I understand that using OR is not advisable, so as I can do?
0
Comment
Question by:enrique_aeo
6 Comments
 
LVL 69

Expert Comment

by:ScottPletcher
ID: 40455532
Put the city name(s) into a table keyed on city and do an INNER JOIN.
0
 
LVL 142

Assisted Solution

by:Guy Hengel [angelIII / a3]
Guy Hengel [angelIII / a3] earned 167 total points
ID: 40455541
0
 
LVL 24

Expert Comment

by:Phillip Burton
ID: 40455549
"OR" is possible, but it depends on the number of cities the user can select from. If it's limited, then OR is fine. If not, then either another table or a variable/parameter would be the way to go.
0
Complete VMware vSphere® ESX(i) & Hyper-V Backup

Capture your entire system, including the host, with patented disk imaging integrated with VMware VADP / Microsoft VSS and RCT. RTOs is as low as 15 seconds with Acronis Active Restore™. You can enjoy unlimited P2V/V2V migrations from any source (even from a different hypervisor)

 

Author Comment

by:enrique_aeo
ID: 40455593
Please

I need transact sql code.

is more optimal use "IN" or use "OR" ?. In this case I can have up to 35 different city values
0
 
LVL 65

Assisted Solution

by:Jim Horn
Jim Horn earned 166 total points
ID: 40455628
I've used the table and JOIN method that Scott recommends with success in my previous life as an Access developer.
Doing it that way the SQL query does not have to change, and most of the work will be in the UI populating and clearing the table based on the user selections.
SELECT 
   s.companyname, s.contactname, s.contacttitle,
   s.address, s.city, s.country, s.phone, s.fax
FROM Suppliers s
   JOIN ui_city_choices ui ON s.companyname = ui.companyname

Open in new window

>is more optimal use "IN" or use "OR" ?.
Without the UI part IN would be easier to code after about five selections, BUT with the UI part it would be an equal pain to have to create code that parses a SQL statement that  loops through all user selections and either add an OR.. or the values within an IN block.  Just my opinion.
0
 
LVL 69

Accepted Solution

by:
ScottPletcher earned 167 total points
ID: 40455697
There's no function difference between "IN" and "OR", zilch.  Because, in fact, SQL converts the "IN" to a string of "ORs" (you can verify this by looking at the query plan).
0

Featured Post

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

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

Suggested Solutions

Use this article to create a batch file to backup a Microsoft SQL Server database to a Windows folder.  The folder can be on the local hard drive or on a network share.  This batch file will query the SQL server to get the current date & time and wi…
Slowly Changing Dimension Transformation component in data task flow is very useful for us to manage and control how data changes in SSIS.
This videos aims to give the viewer a basic demonstration of how a user can query current session information by using the SYS_CONTEXT function
Viewers will learn how to use the SELECT statement in SQL and will be exposed to the many uses the SELECT statement has.

895 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

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now