SQL Server存储过程错误 - 子查询返回多个值

时间:2011-10-24 21:21:40

标签: sql-server stored-procedures

我必须编写一个存储过程来填充收入。要求是我有两个病人和配偶/其他部分,其中患者的收入必须填入他的部分,而配偶/其他部分应该在该部分之下。

当配偶/其他部分中有多个成员时,我遇到了问题!以下是我的代码。

错误消息说----

  

“@ 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 app_id=@app_id and msn =@msn)

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)

1 个答案:

答案 0 :(得分:0)

您的查询的这一部分返回了多行:

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

因此,变量@OtherMSN有多个可能的值。您需要修复查询以返回单个行/列(标量)值,在这种情况下类型为varchar(3)