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.
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 Whitewhere 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 Pinkwhere ID = @GUIDselect ID as Blue from Bluewhere ID = @GUIDselect ID as Brown from Brownwhere ID = @GUID |
|
tkizer
Almighty SQL Goddess
38200 Posts |
Posted - 2014-01-21 : 12:13:23
|
You could union them all together:select Type, IDfrom (select 'White' as Type, ID from White union all select 'Orange', ID from Orange union all....) tBut 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 KizerSQL Server MVP since 2007http://weblogs.sqlteam.com/tarad/ |
|
|
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. |
|
|
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 MVPhttp://visakhm.blogspot.com/https://www.facebook.com/VmBlogs |
|
|
Fenster894111
Starting Member
4 Posts |
Posted - 2014-01-22 : 09:26:11
|
Thanks Visakh16! |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2014-01-23 : 07:14:35
|
welcome------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/https://www.facebook.com/VmBlogs |
|
|
|
|
|