Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: SQL2000 SQL-server-2005 SQL , +
Hi Friends,
Can anyone solve this query and help me??
 
create proc Sp_DespatchMachine
@docno int
as
select distinct m.mcidno,m.mcrefno,t.buyercomp,t.buyerfact
from Tbl_Machine m join Tbl_Asset t 
on m.mcidno = (select t.machineid from Tbl_Asset t where t.deliveryno=@docno) 
WHERE t.deliveryno= @docno
 

when my subquery returns single value it works fine..
select t.machineid from tbl_asset t where t.deliveryno=@docno 
 
when it returns more than 1 value facing error like
 
Subquery returned more than 1 value. This is not permitted when the subquery follows =, !=, <, <= , >, >
 
if my deliveryno =1 and my machineid ="machin1" the above join query will work.
 
if my deliveryno =1 and my machineid = "machine1',"machine2". it will not work.
 
but i want to make it work.. is there any way?
 
can we use for loop in my subquery? or anyother easy way..??
Posted 26-Jun-13 3:31am
Comments
Zoltán Zörgő at 26-Jun-13 8:48am
   
I suppose you could use IN instead of = (...on m.mcidno IN (select...), but using subquery in a join ON is quite unusual.
RelicV at 26-Jun-13 9:01am
   
Provide your table schema and the PK and FK relationship.!
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

I would change your query to this
 
 CREATE PROC DespatchMachine
   @docno INT
 AS
  SELECT DISTINCT m.mcidno,m.mcrefno,t.buyercomp,t.buyerfact
  FROM Tbl_Machine m INNER JOIN Tbl_Asset t
  ON m.mcidno = t.machineid
  WHERE t.deliveryno= @docno
 
p.s. Inner Join will only return the matches.
  Permalink  
v2
Comments
VIP Venkatesan at 26-Jun-13 11:12am
   
Thank u..
Simon_Whale at 26-Jun-13 11:14am
   
your welcome :)
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

Try this query
create proc Sp_DespatchMachine
@docno int
as
select distinct m.mcidno,m.mcrefno,t.buyercomp,t.buyerfact
from Tbl_Machine m join Tbl_Asset t
on m.mcidno = (select top 1 t.machineid  from Tbl_Asset t where t.deliveryno=@docno)
WHERE t.deliveryno= @docno
  Permalink  

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



Advertise | Privacy | Mobile
Web01 | 2.8.141220.1 | Last Updated 26 Jun 2013
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100