?
Solved

10774: alternatives to the OR sentence

Posted on 2014-11-20
6
Medium Priority
?
90 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
[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
6 Comments
 
LVL 69

Expert Comment

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

Assisted Solution

by:Guy Hengel [angelIII / a3]
Guy Hengel [angelIII / a3] earned 668 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
Ransomware: The New Cyber Threat & How to Stop It

This infographic explains ransomware, type of malware that blocks access to your files or your systems and holds them hostage until a ransom is paid. It also examines the different types of ransomware and explains what you can do to thwart this sinister online threat.  

 

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 66

Assisted Solution

by:Jim Horn
Jim Horn earned 664 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:
Scott Pletcher earned 668 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

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

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

Slowly Changing Dimension Transformation component in data task flow is very useful for us to manage and control how data changes in SSIS.
What if you have to shut down the entire Citrix infrastructure for hardware maintenance, software upgrades or "the unknown"? I developed this plan for "the unknown" and hope that it helps you as well. This article explains how to properly shut down …
Via a live example, show how to extract insert data into a SQL Server database table using the Import/Export option and Bulk Insert.
Using examples as well as descriptions, and references to Books Online, show the documentation available for datatypes, explain the available data types and show how data can be passed into and out of variables.
Suggested Courses

770 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