Click here to Skip to main content
15,905,508 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
I Create a Store Procedure
I want to getting data some different table and I use a Sub Query,
I run store procedure and store procedure run successfully and i execute a store procedure then error

Subquery returned more than 1 value. This is not permitted when the subquery follows =, !=, <, <= , >, >= or when the subquery is used as an expression.

i tried the following sp

SQL
insert into @MyTableVar(TinNo,CardName,Address,DocCode,DocumentDate,TaxCategoryDescription,TotalBtax,Partnertax ,Partnertaxsurcharge )
select ISNULL((select TinNo from BP.BPTAX where CardCode=H.CardCode),'NA'),H.CardName,H.ShipToAddress,
H.DocCode,H.DocumentDate,ISNULL((select FkTaxCategoryDescription from SD.ARTXM  where DocCode=H.DocCode ),'NA')
ISNULL((select sum(TotalBTax)  from sd.ARDTL where DocCode=H.DocCode ),0.00),
ISNULL((select PartnerTaxSum   from sd.ARTXM where FKTaxDescription like'%TAX%' and DocCode=H.DocCode ),0.00),
ISNULL((select PartnerTaxSum   from sd.ARTXM where FKTaxDescription='%Surcharge%' and DocCode=H.DocCode ),0.00)

from SD.ARHDR H
where(( @FromDate is null or @ToDate is null) or (H.PostingDate >= Convert(datetime,@FromDate) and H.PostingDate<=Convert(datetime,@ToDate)))
and (@CardCode is null or H.CardCode=@CardCode)
and (@DocCode is null or H.DocCode=@DocCode)
and(@Status is null or H.Status=@Status)

group by CardCode,CardName,ShipToAddress,H.DocCode,DocumentDate,H.ModTime





I want data in this form
Tinno, cardname, shiptoaddress,DocCode,Document Date,FKTaxCategoryDescription,TotalBtax,PartnertaxSum,PartnertaxSurcharge,



PartnertaxSum,PartnertaxSurchargeas field get the one column
Posted

Your getting this error because your subquery is returning more than one value. One to way solve the problem is to use "TOP 1” in your subquery.
For Example
select Top 1 FkTaxCategoryDescription from SD.ARTXM  where DocCode=H.DocCode
 
Share this answer
 
v2
All below queries are sub queries pass value for H.CardCode and execute it's return more then 1 row so you got error
SQL
select TinNo from BP.BPTAX where CardCode=H.CardCode
select FkTaxCategoryDescription from SD.ARTXM  where DocCode=H.DocCode 
select PartnerTaxSum   from sd.ARTXM where FKTaxDescription like'%TAX%' and DocCode=H.DocCode

select PartnerTaxSum   from sd.ARTXM where FKTaxDescription='%Surcharge%' and DocCode=H.DocCode

use distinct or top 1 in sub query to solve it.

Happy Coding!
:)
 
Share this answer
 

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



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900