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
 SQL Server 2005 Forums
 Transact-SQL (2005)
 Subquery not working on junction table?

Author  Topic 

Matt2k9
Starting Member

10 Posts

Posted - 2008-09-05 : 10:41:20
Hi, I have a subquery that will just not work.

select product.productid, productname, datecreated,
(select qualificationName from product_qualification where
product_qualification.qualificationid = product.qualificationid) as qualification
from product

The above query works fine, I am just pulling the qualification name related to the product by qualificationid from a lookup table.

BUT when I use the exact same query design against a junction table I get the error "subquery returned more than 1 value, this is not permitted when the subquery follows, =, !=, < (etc) or when the subquery is used as an expression".

The query that returns this error is:

select product.productid, productname,
(select locationid, price from product_availabilityJunction where product_availabilityJunction.productId = product.productid) as availability
from product

So this query does not work even though it uses the same pattern as the previous query. The only reason I can think of is that in the 2nd query I'm trying to use a subquery on a junction table (with a combined primary key of productid + locationid).

This is really annoying, any ideas why this is not working?

I should also note that I cannot use joins for this due to some asp.net reasons. (urggh)

Any ideas would be appreciated!
Thanks,
Matt

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2008-09-07 : 00:35:46
use join instead of subquery

select p.productid, p.productname,paj.locationid, paj.price
from product p
inner join product_availabilityJunction paj
on paj.productId = p.productid
Go to Top of Page

madhivanan
Premature Yak Congratulator

22864 Posts

Posted - 2008-09-08 : 03:15:09
<<
should also note that I cannot use joins for this due to some asp.net reasons. (urggh)
>>

What are the resaons?

Madhivanan

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

Matt2k9
Starting Member

10 Posts

Posted - 2008-09-08 : 06:09:22
Thanks, I think I am going to have to go down the join route afterall, this subquery stuff is just crazy.

I am using Visual Studio to automatically write some of my data access layer for me, using sub queries it will create my methods for inserts, updates and deletes using optimized and connection safe code. Unfortunately if the queries you point the compiler at have joins, it won't work. Normally the thing to do is point it at queries using sub querys then change your sprocs to use joins afterwards. I have come up with a work-around though.
Go to Top of Page

jsmith8858
Dr. Cross Join

7423 Posts

Posted - 2008-09-08 : 10:35:52
quote:
Originally posted by Matt2k9

Thanks, I think I am going to have to go down the join route afterall, this subquery stuff is just crazy.



Joins are usually more efficient than sub-queries, and often much more flexible and easier to work with. You can really "divide and conquer" a complicated SQL statement by using JOINS and things like derived tables.

For more info on converting sub-queries to joins, see:

http://weblogs.sqlteam.com/jeffs/archive/2007/07/12/60254.aspx

also, you might find this useful:

http://weblogs.sqlteam.com/jeffs/archive/2007/04/30/60192.aspx



- Jeff
http://weblogs.sqlteam.com/JeffS
Go to Top of Page
   

- Advertisement -