Discussion Locked

T-SQL querying multiple tables based on certain conditions

0 helpful
1 reply
Closed to new repliesThis discussion is closed. It stays readable, but it takes no new replies.

Hi,

I have a situation where i need to query multiple tables (12 tables) within a database for records containing values that are listed in another table and also based on a certain conidition.

Specifically, i have a EMP table with a list of employee names, date of joining, office address etc. I have 12 other tables that contain detailed information for all employees split into 12 monthly tables, based on date of joining.

I need a query to list all information in these 12 tables for all employees that are listed in table EMP for employees that joined before certain date (the date field and EMP number is common to the EMP field and all the 12 tables).

Can you help me?
thnak you in advance!

1 reply

by tony hopkinson

select * from (

select * From emp,empJan
Where emp.empno = empjan.empno
union
select * From emp,empFeb
Where emp.empno = empFeb.empno
union
....
union
select * From emp,empDec
Where emp.empno = empDec.empno
) allemps
where date_joined < ?????

There are some less messy but more complex solutions.

Not used them myself, but look up user defined functions and table variables.

This discussion is closed to new replies.