2011-10-24 8 views
1

収入を埋めるためにストアドプロシージャを記述する必要があります。要件は、私は2つのセクションの患者と配偶者/その他があり、そこでは患者収入が彼のセクションの下に配置され、配偶者/他のセクションはそのセクションの下に置かれなければならないということです。SQL Serverストアドプロシージャエラー - サブクエリが複数の値を返します

配偶者/その他のセクションに複数のメンバーがいる場合に問題があります。以下は私のコードです。

エラーメッセージが

は "@OtherMSN"「サブクエリ>が、これは ないサブクエリが続く場合許可=、!=、<、< =を1つの以上の値を返し----と言います、> =またはサブクエリが式として使用されているときに発生します。

ご協力いただきまして誠にありがとうございます。

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

何が助けになりますか?エラーの理由は簡単で、エラーで説明されています。 –

答えて

0

クエリのこの部分は、複数の行が返されました:

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

だからあなたの変数@OtherMSNための複数の可能な値があります。この場合、単一の行/列(スカラー)値を返すようにクエリを修正する必要があります

+0

私は、単一の値だけでなく、すべての可能な複数の値の所得を計算する必要があります。上記のprocは、クエリが単一の行/列の値を返すときに機能します。 – userstackoverflow

+1

複数の値を扱う必要がある場合は、クエリをリファクタリングする必要があります。リファクタリングが本当に必要です。恐ろしいことです! –

+0

はい、リファクタリング!初心者は "gross_amount"と "income_type"を合計してください。おそらく、特定の型を抽出するためにケースを使用します...しかし、この多くの選択を使用しているときはいつでも、リファクタリングする必要があります。効率が上がる限り、それは悪夢です。 –

関連する問題