SQL Server Forums
Profile | Register | Active Topics | Members | Search | Forum FAQ
 
Register Now and get your question answered!
Username:
Password:
Save Password
Forgot your Password?

 All Forums
 SQL Server 2008 Forums
 Transact-SQL (2008)
 Simplify a select statement?
 New Topic  Reply to Topic
 Printer Friendly
Author Previous Topic Topic Next Topic  

Fenster894111
Starting Member

4 Posts

Posted - 01/21/2014 :  06:35:28  Show Profile  Reply with Quote
Hi Guys,

I was wondering if any of you could help me shorten/simplify the below?

I would prefer it to be shorter if possible maybe using a more complex select (sample only below , its searching 30+tables) and to return only the tables where it finds the value.
Is this possible?

Thanks in advance!
Davie.


Declare @GUID varchar(8000)
set @GUID = 'string'

select ID as White from White
where ID = @GUID
select ID as orange from orange
where ID = @GUID
select ID as Blonde from Blonde
where ID = @GUID
select ID as Pink from Pink
where ID = @GUID
select ID as Blue from Blue
where ID = @GUID
select ID as Brown from Brown
where ID = @GUID

tkizer
Almighty SQL Goddess

USA
36615 Posts

Posted - 01/21/2014 :  12:13:23  Show Profile  Visit tkizer's Homepage  Reply with Quote
You could union them all together:

select Type, ID
from (select 'White' as Type, ID from White union all select 'Orange', ID from Orange union all....) t

But instead, I would normalize the database so that you don't have to write queries like this. The data should be stored in one table or possibly two (EAV) if they have different columns. Tell us more about these tables.

Tara Kizer
SQL Server MVP since 2007
http://weblogs.sqlteam.com/tarad/
Go to Top of Page

Fenster894111
Starting Member

4 Posts

Posted - 01/22/2014 :  05:28:16  Show Profile  Reply with Quote
Hi Tkizer,

Thanks for replying.

From time to time I get an ID string.
The ID will be in one of 30 tables (always the same 30) each table has an ID column.

I guess the select statement works as I paste inthe string and just run it and it runs through the tables till it finds it.

I just thought as Im pretty green in SQL that there may have been a 'cleaner' way to search for the string and maybe only return tables where the value was found.
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

India
52309 Posts

Posted - 01/22/2014 :  08:21:01  Show Profile  Reply with Quote
quote:
Originally posted by Fenster894111

Hi Tkizer,

Thanks for replying.

From time to time I get an ID string.
The ID will be in one of 30 tables (always the same 30) each table has an ID column.

I guess the select statement works as I paste inthe string and just run it and it runs through the tables till it finds it.

I just thought as Im pretty green in SQL that there may have been a 'cleaner' way to search for the string and maybe only return tables where the value was found.


so far as data exists in 30 tables you've to serach all of them.
one thing you may do is create a view involving all the 30 tables so that you can search for the data directly against it and avoid repeating all 30 tables. But on the background it does the same thing ie search against all the tables

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/
https://www.facebook.com/VmBlogs
Go to Top of Page

Fenster894111
Starting Member

4 Posts

Posted - 01/22/2014 :  09:26:11  Show Profile  Reply with Quote
Thanks Visakh16!
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

India
52309 Posts

Posted - 01/23/2014 :  07:14:35  Show Profile  Reply with Quote
welcome

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/
https://www.facebook.com/VmBlogs
Go to Top of Page
  Previous Topic Topic Next Topic  
 New Topic  Reply to Topic
 Printer Friendly
Jump To:
SQL Server Forums © 2000-2009 SQLTeam Publishing, LLC Go To Top Of Page
This page was generated in 0.06 seconds. Powered By: Snitz Forums 2000