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