Solved

10774: alternatives to the OR sentence

Posted on 2014-11-20
6
81 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:Scott Pletcher
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
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.

 

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:
Scott Pletcher 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

Title # Comments Views Activity
SQL Server Designer 19 42
Sql Server group by 10 29
Sql server get data from a usp to use in a usp 5 16
PROPERCASE SCRIPT IN SQL 3 7
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.
JSON is being used more and more, besides XML, and you surely wanted to parse the data out into SQL instead of doing it in some Javascript. The below function in SQL Server can do the job for you, returning a quick table with the parsed data.
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.
Viewers will learn how to use the UPDATE and DELETE statements to change or remove existing data from their tables. Make a table: Update a specific column given a specific row using the UPDATE statement: Remove a set of values using the DELETE s…

778 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