Solved

10774: alternatives to the OR sentence

Posted on 2014-11-20
6
77 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
Comment Utility
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
Comment Utility
0
 
LVL 24

Expert Comment

by:Phillip Burton
Comment Utility
"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
IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 

Author Comment

by:enrique_aeo
Comment Utility
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
Comment Utility
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
Comment Utility
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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
separate column 24 19
SQL Server memory Issue 7 73
Retention Policy for Backups 1 12
Usage Scenarios for Extended Events? 1 9
Everyone has problem when going to load data into Data warehouse (EDW). They all need to confirm that data quality is good but they don't no how to proceed. Microsoft has provided new task within SSIS 2008 called "Data Profiler Task". It solve th…
In this article we will get to know that how can we recover deleted data if it happens accidently. We really can recover deleted rows if we know the time when data is deleted by using the transaction log.
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 to return specific rows and columns, with various degrees of sorting and limits in place.

772 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

9 Experts available now in Live!

Get 1:1 Help Now