multiple tables in db

Hi,
 I need help with getting data from multiple tables in one db .each table has table scheme = date  then table name .
e.g 20151119.transaction

now i want to get amount from all transaction table which come under year 2015

e.g select amount from 2015xxxx.transaction where account = '100001'

.thanks
Syed Ali Shah RashdiT24/Insight Technical ConsultantAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

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

Barry CunneyCommented:
Hi Syed,
A UNION query may help you in this scenario.
So please take a look at the following sample:

SELECT [Amount] FROM [Transaction Table A] WHERE [Year] = 2015
UNION
SELECT [Amount] FROM [Transaction Table B] WHERE [Year] = 2015

Open in new window

Arun MuruganCommented:
You can do this with Dynamic Queries.
SELECT
   SEQ_NO = IDENTITY(5),
   SCHEMA_NAME(schema_id) + '.' + name AS TABLE_NAME
INTO
   #TABLES
FROM
   sys.tables
WHERE
   name = 'transaction'
and SCHEMA_NAME(schema_id) like '2015%'

DECLARE @Query varchar(1000)
DECLARE @COUNT INT
DECLARE @MIN INT

SET @MIN = 1
SELECT @COUNT = COUNT(1) FROM #TABLES

WHILE(@MIN <= @COUNT)
BEGIN
   SELECT @Query = 'SELECT account_info FROM ' + TABLE_NAME + ' WHERE account = ''100001'' ' FROM #TABLES WHERE SEQ_NO = @MIN
   IF @MIN <> @COUNT
   BEGIN
      SELECT @Query = @Query + ' UNION '
   END
END

EXEC (@Query)

Open in new window

Make sure that the column_names that you are selecting are same in all the tables.

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
Syed Ali Shah RashdiT24/Insight Technical ConsultantAuthor Commented:
Hi Arun Thanks but i am getting error on IDENTITY(5),  ?
Arun MuruganCommented:
Can you replace the 2nd line with the below line and try.
SEQ_NO = IDENTITY(int,1,1),

Open in new window

There could be minor syntax errors. I din't compile it as I'm out of editor. See if you can correct them or post it here, I will correct it.
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.