Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 General SQL Server Forums
 Database Design and Application Architecture
 Joining four tables

Author  Topic 

imliability
Starting Member

4 Posts

Posted - 2012-01-15 : 14:07:24
select t.tutid,c.chapid from tutorial t join chapter c on (t.chapid=c.chapid) join book b on (c.isbn=b.isbn) join subject s on (b.subjcode=s.subjcode) where subjcode ='" & DropDownList1.Text & "'

here is my database design


and i got this error
[DB2/NT] SQL0203N A reference to column "SUBJCODE" is ambiguous. SQLSTATE=42702

GilaMonster
Master Smack Fu Yak Hacker

4507 Posts

Posted - 2012-01-15 : 15:01:31
Two problems.

- The column you have in the error message does not appear anywhere in the query you gave, nor does the query bear any resemblance at all to the tables in the design
- You're using DB2, this is a Microsoft SQL Server forum. While we might be able to guess what's wrong if you post the actual query you ran, we might guess wrong because we're SQL Server people, not DB2 people

--
Gail Shaw
SQL Server MVP
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2012-01-15 : 22:55:50
i think my guess is you've SUBJCODE coming from more than one table (its in SUBJ,ENROLLED & BOOK in diagram) so if you're using it in joins make use you prefix it with tablename (or short aliases) else query engine wont be able to make out which table column you're trying to refer and will throw this error message



------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/

Go to Top of Page

imliability
Starting Member

4 Posts

Posted - 2012-01-16 : 00:21:24
quote:
Originally posted by GilaMonster

Two problems.

- The column you have in the error message does not appear anywhere in the query you gave, nor does the query bear any resemblance at all to the tables in the design
- You're using DB2, this is a Microsoft SQL Server forum. While we might be able to guess what's wrong if you post the actual query you ran, we might guess wrong because we're SQL Server people, not DB2 people

--
Gail Shaw
SQL Server MVP



sorry sir,
i suppose to put my original query but i forgot to double check because i was so sleepy at that time...
here is my original query
select t.tutid,c.chapid from tutorial t join chapter c on (t.chapid=c.chapid) join book b on (c.isbn=b.isbn) join subject s on (b.subjcode=s.subjcode) where subjcode ='" & DropDownList1.Text & "'
Go to Top of Page

imliability
Starting Member

4 Posts

Posted - 2012-01-16 : 00:21:43
quote:
Originally posted by visakh16

i think my guess is you've SUBJCODE coming from more than one table (its in SUBJ,ENROLLED & BOOK in diagram) so if you're using it in joins make use you prefix it with tablename (or short aliases) else query engine wont be able to make out which table column you're trying to refer and will throw this error message



------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/





sorry sir,
i suppose to put my original query but i forgot to double check because i was so sleepy at that time...
here is my original query
select t.tutid,c.chapid from tutorial t join chapter c on (t.chapid=c.chapid) join book b on (c.isbn=b.isbn) join subject s on (b.subjcode=s.subjcode) where subjcode ='" & DropDownList1.Text & "'
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2012-01-16 : 01:03:05
quote:
Originally posted by imliability

quote:
Originally posted by visakh16

i think my guess is you've SUBJCODE coming from more than one table (its in SUBJ,ENROLLED & BOOK in diagram) so if you're using it in joins make use you prefix it with tablename (or short aliases) else query engine wont be able to make out which table column you're trying to refer and will throw this error message



------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/





sorry sir,
i suppose to put my original query but i forgot to double check because i was so sleepy at that time...
here is my original query
select t.tutid,c.chapid from tutorial t join chapter c on (t.chapid=c.chapid) join book b on (c.isbn=b.isbn) join subject s on (b.subjcode=s.subjcode) where subjcode ='" & DropDownList1.Text & "'



As i doubted you're missing an alias here. you should put correct table alias here to avoid ambiguity as subjcode is available in more than one table

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/

Go to Top of Page

GilaMonster
Master Smack Fu Yak Hacker

4507 Posts

Posted - 2012-01-16 : 03:52:12
select t.tutid,c.chapid from tutorial t join chapter c on (t.chapid=c.chapid) join book b on (c.isbn=b.isbn) join subject s on (b.subjcode=s.subjcode) where s.subjcode ='" & DropDownList1.Text & "'

--
Gail Shaw
SQL Server MVP
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2012-01-16 : 04:09:46
do you've some other query part following this. I notice you're missing a closing " after the last '

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/

Go to Top of Page

imliability
Starting Member

4 Posts

Posted - 2012-01-16 : 10:00:55
quote:
Originally posted by GilaMonster

select t.tutid,c.chapid from tutorial t join chapter c on (t.chapid=c.chapid) join book b on (c.isbn=b.isbn) join subject s on (b.subjcode=s.subjcode) where s.subjcode ='" & DropDownList1.Text & "'

--
Gail Shaw
SQL Server MVP



this is work!!!
Go to Top of Page

GilaMonster
Master Smack Fu Yak Hacker

4507 Posts

Posted - 2012-01-16 : 10:06:52
Do you understand why?

--
Gail Shaw
SQL Server MVP
Go to Top of Page
   

- Advertisement -