T-SQL - how multiple tables with wild card?

Hello All:

I am using SQL server 2008 R2, how can I query multiple table using a wild card for example ?

Select * from TAB_*

I have 500 tables like this

TAB_09082013
TAB_08072012
etc...

Please let me know
LVL 5
CalmSoulAsked:
Who is Participating?
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.

Mike EghtebasDatabase and Application DeveloperCommented:
Because you need to pass the table name as parameter and views don't take parameter, you need to use a table valued function like:

Select * From fnReadDataFromTable('TAB_09082013');

Or, just use something like in below without a function call:

DECLARE @tablename AS NVARCHAR(261) = N'TAB_09082013';
EXECUTE(N'SELECT * FROM ' + @tablename);
0
Brian CroweDatabase AdministratorCommented:
You would have to use dynamic sql to generate each of the 500 queries and they use sp_executesql to execute them individually.

https://technet.microsoft.com/en-US/library/ms175170(v=SQL.105).aspx
0
Deepak ChauhanSQL Server DBACommented:
You can use this code, hope this is what you need.

declare @min int, @max int, @tbl nvarchar(128), @sql nvarchar(max)

select name, ROW_NUMBER() over(order by name) as rno into #t
 from sys.tables where name like 'TAB_%'

set @min=1 
set @max =(select MAX(rno) from #t)

while @min <= @max
begin
set @tbl=(select name from #t where rno=@min)
set @sql='select * from ' + @tbl
exec (@sql)
set @min=@min+1
end
drop table #t

Open in new window

0
Ultimate Tool Kit for Technology Solution Provider

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy now.

CalmSoulAuthor Commented:
@deepakChauhan - it working but I want merged results as well...
0
Deepak ChauhanSQL Server DBACommented:
Is all table's has same columns?

if yes you can create a temp\physical table with the same column name and change the @sql variable string value to....



set @sql='insert   into <#TAB_Result> select  * from ' + @tbl
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
Vitor MontalvãoMSSQL Senior EngineerCommented:
CalmSoul, do you still need help with this question?
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.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.