Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Joining 4 tables together

Status
Not open for further replies.

Mayoor

Programmer
Jan 16, 2004
198
GB
Hi I need a view which will join 4 tables together by doing this...

CREATE VIEW dbo.submittedApril
AS
SELECT *
FROM applicants INNER JOIN
applicantData ON applicants.ID = applicantData.ID
UNION ALL
SELECT *
FROM applicants_inactive INNER JOIN
appData_inactive ON applicants_inactive.ID = appData_inactive.ID

However when I try to create this as a view in Query analyser

I get this error message.......

Server: Msg 8156, Level 16, State 1, Procedure submittedApril, Line 4
The column 'ID' was specified multiple times for 'submittedApril'.

Please can anyone help....

Mayoor
 
Instead of Select *, you will need to specify the columns. If one table's column name is the same as another table's (like the "ID"), use "AS" - for example:
Code:
SELECT ID AS ApplicantID, Column1, Column2, Column3
FROM applicants INNER JOIN
applicantData ON applicants.ID = applicantData.ID
UNION ALL
SELECT  ID AS InnactiveApplicantID, ColumnX, ColumnY, ColumnZ
FROM applicants_inactive INNER JOIN
appData_inactive ON applicants_inactive.ID = appData_inactive.ID

Hope that helps...
 
right what happens if all the columns in the first join are the same as the columns in the 2nd join. Will I have to alias all of them?
 
unfortunately yes.

"Shoot Me! Shoot Me NOW!!!"
- Daffy Duck
 
right well with that code im getting

SELECT ID AS ApplicantID
FROM applicants INNER JOIN
applicantData ON applicants.ID = applicantData.ID
UNION ALL
SELECT ID AS InnactiveApplicantID
FROM applicants_inactive INNER JOIN
appData_inactive ON applicants_inactive.ID = appData_inactive.ID

Server: Msg 209, Level 16, State 1, Line 1
Ambiguous column name 'ID'.


any ideas?
 
That error means it doesn't know which ID column to display. Specify one of the tables in the join as part of the field name.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top