Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2008 Forums
 Transact-SQL (2008)
 Simplify a select statement?

Author  Topic 

Fenster894111
Starting Member

4 Posts

Posted - 2014-01-21 : 06:35:28
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

38200 Posts

Posted - 2014-01-21 : 12:13:23
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 - 2014-01-22 : 05:28:16
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

52326 Posts

Posted - 2014-01-22 : 08:21:01
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 - 2014-01-22 : 09:26:11
Thanks Visakh16!
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2014-01-23 : 07:14:35
welcome

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

- Advertisement -