【问题标题】:SQL Server stored procedure error - subquery returns more than one valueSQL Server 存储过程错误 - 子查询返回多个值
【发布时间】:2011-10-24 21:21:40
【问题描述】:

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

当配偶/其他部分的成员超过一个时,我遇到了问题!下面是我的代码。

错误信息表明----

"@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)

【问题讨论】:

  • 您在哪些方面需要帮助?错误的原因很简单,错误中解释了。

标签: sql-server stored-procedures


【解决方案1】:

这部分查询返回了不止一行:

(SELECT MSN FROM EXT_KSR_APP_MEMBERS (nolock) WHERE APP_ID = @app_id and msn &lt;&gt; @msn)

因此,您的变量 @OtherMSN 有多个可能的值。您需要修复查询以返回单个行/列(标量)值,在本例中为 varchar(3)

【讨论】:

  • 我需要计算所有可能的多个值的收入,而不仅仅是单个值。当查询返回单个行/列值时,上述过程起作用。
  • 如果你需要它处理多个值,你需要重构你的查询——它确实需要重构——太可怕了!!
  • 是的,重构!对于初学者,您应该将“gross_amount”和“income_type”相加。可能用于提取特定类型的用例......但是任何时候您使用这么多选择时,您都应该重构。就效率而言,这是一场噩梦。
猜你喜欢
  • 2021-01-27
  • 1970-01-01
  • 1970-01-01
  • 2015-10-06
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2010-11-23
相关资源
最近更新 更多