2011-10-24 2 views
1

Je dois écrire un proc stocké pour remplir le revenu. l'exigence est que j'ai deux sections patient et conjoint/autre où le revenu du patient doit être rempli en vertu de son article et conjoint/autre section devrait être sous cet article.Erreur de procédure stockée SQL Server - la sous-requête renvoie plus d'une valeur

J'ai un problème quand il y a plus d'un membre dans la section conjoint/autre! Voici mon code.

message d'erreur indique que ----

"@OtherMSN" « sous-requête amène à plus de 1 valeur. Ce n'est pas autorisé lorsque la sous-requête suit =! =, <, < =,> ,> = ou lorsque la sous-requête est utilisée comme expression. "

Toute aide est grandement appréciée.

declare @app_id varchar(20), @msn int 
declare @patient_msn varchar(5) 
set @patient_msn = (select msn from EXT_KSR_APP_MEMBERS (nolock) where [email protected]_id and msn [email protected]) 

declare @OtherMSN varchar(3) 
set @OtherMSN = (SELECT MSN FROM EXT_KSR_APP_MEMBERS (nolock) WHERE APP_ID = @app_id and msn <> @msn) 

select distinct 
(select gross_amount from app_income (nolock) where app_id = @app_id and income_type = 'EJ' and msn = @patient_msn) as gross_salary_patient, 
(select gross_amount from app_income (nolock) where app_id = @app_id and income_type = 'CA' and msn = @patient_msn) as cash_income_patient, 
(select sum(gross_amount) from app_income (nolock) where app_id = @app_id and income_type in('ER','RT','S','SR') and msn = @patient_msn) as gross_ssn_income_patient, 

(select gross_amount from app_income (nolock) where app_id = @app_id and income_type ='L' and msn = @patient_msn) as unemp_ben_patient, 
(select gross_amount from app_income (nolock) where app_id = @app_id and income_type ='J' and msn = @patient_msn) as st_disability_inc_patient, 
(select gross_amount from app_income (nolock) where app_id = @app_id and income_type ='A' and msn = @patient_msn)as a_c_support_patient, 
(select gross_amount from app_income (nolock) where app_id = @app_id and income_type ='M' and msn = @patient_msn) as pen_inc_patient, 
(select gross_amount from app_income (nolock) where app_id = @app_id and income_type ='RI' and msn = @patient_msn) as ren_prp_inc_patient, 
--(select sum(gross_amount) from app_income (nolock) where app_id = @app_id and income_type in('19','8','CR','FI','FS','G1','GA','IA','IC','IE','II','IK','IP','IU','LO','N','O','P','R','RI','T','TAN','TR') and msn = 1) as other_source_patient, 

(select sum(gross_amount) from app_income (nolock) where app_id = @app_id and income_type in('ER','RT','S','SR','EJ','CA','L','J','A','M','RI','19','8','CR','FI','FS','G1','GA','IA','IC','IE','II','IK','IP','IU','LO','N','O','P','R','RI','T','TAN','TR') and msn = @patient_msn) as total_mon_inc_patient 

from app_member mem (nolock) 
inner join app_income inc (nolock) 
on mem.app_id = inc.app_id and mem.msn = inc.msn 
where mem.app_id = @app_id and mem.msn = @patient_msn 

select distinct 
(select sum(gross_amount) from app_income (nolock) where app_id = @app_id and income_type = 'EJ' and msn in (@OtherMSN)) as gross_salary_oth, 
(select sum(gross_amount) from app_income (nolock) where app_id = @app_id and income_type = 'CA' and msn in (@OtherMSN)) as cash_income_oth, 
(select sum(gross_amount) from app_income (nolock) where app_id = @app_id and income_type in('ER','RT','S','SR') and msn in (@OtherMSN)) as gross_ssn_income_oth, 

(select sum(gross_amount) from app_income (nolock) where app_id = @app_id and income_type ='L' and msn in (@OtherMSN)) as unemp_ben_oth, 
(select sum(gross_amount) from app_income (nolock) where app_id = @app_id and income_type ='J' and msn in (@OtherMSN)) as st_disability_inc_oth, 
(select sum(gross_amount) from app_income (nolock) where app_id = @app_id and income_type ='A' and msn in(@OtherMSN))as a_c_support_oth, 
(select sum(gross_amount) from app_income (nolock) where app_id = @app_id and income_type ='M' and msn in (@OtherMSN)) as pen_inc_oth, 
(select sum(gross_amount) from app_income (nolock) where app_id = @app_id and income_type ='RI' and msn in (@OtherMSN)) as ren_prp_inc_oth, 
-- (select sum(gross_amount) from app_income(nolock) where app_id = @app_id and income_type in ('19','8','CR','FI','FS','G1','GA','IA','IC','IE','II','IK','IP','IU','LO','N','O','P','R','RI','T','TAN','TR') and msn = @OtherMSN) as other_source_oth, 
(select sum(gross_amount) from app_income (nolock) where app_id = @app_id and income_type in('ER','RT','S','SR','EJ','CA','L','J','A','M','RI','19','8','CR','FI','FS','G1','GA','IA','IC','IE','II','IK','IP','IU','LO','N','O','P','R','RI','T','TAN','TR') and msn in (@OtherMSN)) as total_mon_inc_oth 

from app_member mem (nolock) 
inner join app_income inc (nolock) 
on mem.app_id = inc.app_id and mem.msn = inc.msn 
where mem.app_id = @app_id and mem.msn in (@OtherMSN) 
+0

De quoi avez-vous besoin? La raison de l'erreur est simple et expliquée dans l'erreur. –

Répondre

0

Cette partie de votre requête a retourné plus d'une ligne:

(SELECT MSN FROM EXT_KSR_APP_MEMBERS (nolock) WHERE APP_ID = @app_id and msn <> @msn)

Donc il y a plusieurs valeurs possibles pour votre variable @OtherMSN. Vous devez corriger votre requête pour retourner une valeur de ligne/colonne (scalaire), dans ce cas de type varchar(3)

+0

J'ai besoin de calculer le revenu pour toutes les valeurs multiples possibles, pas seulement une seule valeur. Le processus ci-dessus fonctionne lorsque la requête renvoie une seule valeur de ligne/colonne. – userstackoverflow

+1

Si vous avez besoin de travailler avec plus d'une valeur, vous devez refactoriser votre requête - et il faut vraiment refactoriser - c'est horrible !! –

+0

Oui, refactor! Vous devez additionner "gross_amount" et "income_type" pour les débutants. Cas d'utilisation peut-être pour extraire les types spécifiques ... Mais chaque fois que vous utilisez autant de sélections, vous devriez refactoriser. C'est un cauchemar en ce qui concerne l'efficacité. –