What is the best option to search

 I have 5 tables saying that each table has Code column along with some other columns. I want to check that whether given code falls in which table?

  Please note that code in one table will not exist in another table.

  As of now I'm querying each table. If one table does not contain the code it will search in another table and so on. Is this the best option?

  Whether I could go for JOIN operation?

  Please do suggest me some solution.

  Thanks.
LVL 16
Easwaran ParamasivamAsked:
Who is Participating?
 
Easwaran ParamasivamAuthor Commented:
I've found solution myself.
SET ANSI_NULLS ON
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[GetCodeTypeByCode]   
  @Code VARCHAR(10),
  @CodeType VARCHAR(10) OUTPUT
AS
BEGIN

  IF EXISTS (SELECT TOP 1 CODE FROM [dbo].[TableOne] WHERE Code like @Code) 
  BEGIN
    SET @CodeType = 'TypeOne'
    RETURN
  END

  IF EXISTS (SELECT TOP 1 Code FROM [dbo].[TableTwo] WHERE Code like @Code) 
  BEGIN
    SET @CodeType = 'TypeTwo'
    RETURN
  END

   .......


END

Open in new window

0
 
Pratima PharandeCommented:
You can try a trick here
as you say code in one table will not exist in another table
then try union , and add one extra column for tablename in it

something like this

Select * from

(Select code ,'Table1'as tablename from Table1
Union
Select code ,'Table2' as tablename from Table2
Union
Select code ,'Table3' as tablename from Table3
Union
Select code ,'Table4' as tablename from Table4
Union
Select code ,'Table5' as tablename from Table5 ) X

where X.code = 'yourcodehere'


thois query will return you
cdoe and tablename where it found
0
 
Easwaran ParamasivamAuthor Commented:
I would like to get the result as from which table the code exists?

Each table contains N number of rows. Union them will result in performance overhead. Please let me know some other option.
0
Ultimate Tool Kit for Technology Solution Provider

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy now.

 
DrewKjellCommented:
Union will not result in more overhead that running a separate query against each table.  The query provided would return you two columns, the code and the table that it came from.

You could also modify the query provided slightly like this:



Select code ,'Table1'as tablename from Table1
where code = 'yourcodehere'
Union
Select code ,'Table2' as tablename from Table2
where code = 'yourcodehere'
Union
Select code ,'Table3' as tablename from Table3
where code = 'yourcodehere'
Union
Select code ,'Table4' as tablename from Table4
where code = 'yourcodehere'
Union
Select code ,'Table5' as tablename from Table5
where code = 'yourcodehere'

Which will then not utilize a CTE.  I don't know if there is much gained here except for possible a better chance of utilizing indexes on your tables.


Drew
0
 
DALSOMCommented:
Hi,
You can do this :

1- Build a View with an additional field that indicates from wich table are selecting.
2. Query against this view.

See the DrewKjell select , build a view based on his query :

Create view vwFullTables
as
Select code ,'Table1'as tablename from Table1
Union
Select code ,'Table2' as tablename from Table2
Union
Select code ,'Table3' as tablename from Table3
Union
Select code ,'Table4' as tablename from Table4
Union
Select code ,'Table5' as tablename from Table5


2. Query this view ,

declare @mycode int
set @mycode = 203
select * from vwFullTables  where code = @mycode

Then as result you will get all the columns, plus an additional telling you where this record belongs.

Hope this help you,
Bye,
Dalsom.
0
 
DALSOMCommented:
By the way, create index on your tables. I strong recomend to create a primary key one each table's code field.

This will enhance your search performance.

Bye,
Dalsom.
0
 
Easwaran ParamasivamAuthor Commented:
I've found solution myself. Acceptiong as solution for others reference as well.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.