Click here to Skip to main content
11,496,146 members (700 online)
The site is currently in read-only mode for maintenance. Posting of new items will be available again shortly.
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.!
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 :)
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)

  Print Answers RSS
0 RyanDev 230
1 Sergey Alexandrovich Kryukov 114
2 Richard Deeming 100
3 OriginalGriff 89
4 Mostafa Asaduzzaman 59
0 Sergey Alexandrovich Kryukov 10,401
1 OriginalGriff 8,910
2 Sascha Lefèvre 3,899
3 Maciej Los 3,422
4 Richard Deeming 2,600


Advertise | Privacy | Mobile
Web02 | 2.8.150520.1 | Last Updated 26 Jun 2013
Copyright © CodeProject, 1999-2015
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