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
 New to SQL Server Programming
 help needed with a join

Author  Topic 

BendJoe
Posting Yak Master

128 Posts

Posted - 2008-04-08 : 11:41:27
SELECT * FROM A INNER JOIN
B ON A.AId =
(SELECT B.BId
FROM B
WHERE (B.AId = 4))

I have two tables
Table A has several columns and B has just two columns
B.AId and B.BId.

I will pass B.AId as a parameter and that will return more than one B.BId values. Then I need to create a join where A.AId matches B.BId.

I tried the above SQL but it says subquery can't return more than one value. In my case it must return more than on value.
How can I construct the SQL query.
Please Help.
Thanks

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2008-04-08 : 11:44:08
SELECT * FROM A
INNER JOIN B
ON A.AId =B.BId
AND B.AId = 4
Go to Top of Page

SwePeso
Patron Saint of Lost Yaks

30421 Posts

Posted - 2008-04-08 : 11:45:47
[code]SELECT *
FROM A
INNER JOIN B ON B.BId = A.AId
WHERE B.AId = 4[/code]


E 12°55'05.25"
N 56°04'39.16"
Go to Top of Page

madhivanan
Premature Yak Congratulator

22864 Posts

Posted - 2008-04-09 : 04:21:12
quote:
Originally posted by visakh16

SELECT * FROM A
INNER JOIN B
ON A.AId =B.BId
AND B.AId = 4


It is better to have filtering on WHERE clause otherwise it would make differnece in left and right joins

Madhivanan

Failing to plan is Planning to fail
Go to Top of Page
   

- Advertisement -