2013-04-17 11 views
11

select * from book tableサブクエリが2つ以上の値を返しました。サブクエリが=、!=、<,<=,>、> =の場合、またはサブクエリが式として使用されている場合は許可されません

USE [library] 
GO 

/****** Object: StoredProcedure [dbo].[report_r_and_l] Script Date: 04/17/2013 12:42:39 ******/ 

SET ANSI_NULLS ON 
GO 

SET QUOTED_IDENTIFIER ON 
GO 

ALTER procedure [dbo].[report_r_and_l] 
@fdate date, 
@tdate date, 
@key varchar(1) 
as 

if(@key='r') 

    select * 
    from dbo.books 
    where isbn =(select isbn from dbo.lending where (act between @fdate and @tdate) and (stat ='close')) 

else if(@key='l') 

    select * 
    from dbo.books 
    where isbn =(select isbn from dbo.lending where lended_date between @fdate and @tdate) 

私はサブクエリがメインクエリに複数のクエリを返すことを知っていますが、私はこのエラーを回避する方法を知らない、誰も助けることができますか?

+0

どうやら'select isbn'は複数の値を返します。 'isbn IN(select isbn ...' –

答えて

25

問題を使用することができますが、これらの2つのクエリがあるということですそれぞれが複数を返す行:

select isbn from dbo.lending where (act between @fdate and @tdate) and (stat ='close') 
select isbn from dbo.lending where lended_date between @fdate and @tdate 

希望する結果に応じて2つの選択肢があります。あなたはどちらか(SELECT TOP 1を使用することによって、例えば)単一の行を返すことが保証何かで上記のクエリを置き換えることもできますし、このように、あなたの=INに切り替えて、複数の行を返すことができます。

select * from dbo.books where isbn IN (select isbn from dbo.lending where (act between @fdate and @tdate) and (stat ='close')) 
+0

私はそれを私の友人、感謝しました。私はこれについて一つだけ知っておきたい、選択されたisbnを参照するテーブルが1つだけあること、貸出テーブルからisbnを選択したい、学生テーブルから貸出テーブルsudend_nameからlend_no貸出テーブルのインデックス番号を参照する、複数を取得する方法複数のテーブルからの行?? – Roshan

+0

ニースの明確な答え@ダン。 – Ads

+1

おそらく、lended_dateではなくlent_dateであるべきです。 私はちょっと言っています... – sanepete

4

あなたは

select * from dbo.books where isbn IN 
(select isbn from dbo.lending where lended_date between @fdate and @tdate) 
8

利用In代わりの=以下

select * from dbo.books 
where isbn in (select isbn from dbo.lending 
       where act between @fdate and @tdate 
       and stat ='close' 
       ) 

ように、オペレータで使用できるか、Exists

SELECT t1.*,t2.* 
FROM books t1 
WHERE EXISTS (SELECT * FROM dbo.lending t2 WHERE t1.isbn = t2.isbn and 
       t2.act between @fdate and @tdate and t2.stat ='close') 
+0

thnx bro。u saved my day:)を使用することができます。 Uは大きなビールを飲みたいと思う。 – Kings

+0

SET atRate1 =(SELECT t1。*、t2。* FROM RateCode t1 WHERE EXISTS(SELECT * FROM RateAmount t2 WHERE t1.RateCode = t2.RateCode AND t1.CountryCode = @CCode AND t1.ModuleCode = @MCode) 私はt2でエラーになります。* –