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?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

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
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
IT Pros Agree: AI and Machine Learning Key

We’d all like to think our company’s data is well protected, but when you ask IT professionals they admit the data probably is not as safe as it could be.

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.
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

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Easwaran ParamasivamAuthor Commented:
I've found solution myself. Acceptiong as solution for others reference as well.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft SQL Server

From novice to tech pro — start learning today.