存储过程中的输出参数问题

时间:2019-02-11 05:15:36

标签: stored-procedures

我有2个表的CustomerBeneficiary。有2个或更多的受益人,并添加了一个客户移动电话号码。 我创建了一个存储过程以获取与该手机号码相关的受益人名称

create Proc Customer
(
@Mobile bigint,

@Beneficiary  varchar(100) out,

@Beneficiary2 varchar(100) out  
)
as

Begin

Select @Beneficiary = Beneficiary_Name, @Beneficiary2 = Beneficairy_Name from Beneficiary_master
as a 

join Customer_Master as b
on a.CustomerID= b.CustomerID

where Mobile_Number = @Mobile

End

Declare @Beneficiary varchar(100)

Declare @Beneficiary2 varchar(100)

Exec spCustomer 999999999, @Beneficiary out, @beneficiary2 out

如何获得两个受益人姓名,因为我只有一个名字

1 个答案:

答案 0 :(得分:0)

您的Select查询将返回与select语句匹配的记录,并将最后一条记录的结果分配给变量@Beneficiary@Beneficiary2

您必须更改选择查询才能获得所需的o / p。请尝试使用此语句,如果您的要求适合,请相应更改SP

Declare  @Mobile bigint =123

Declare @Beneficiary  varchar(100) 

Declare @Beneficiary2 varchar(100) 

Select @Beneficiary = Beneficiary_Name 
from Beneficiary_master as a 
join Customer_Master as b on a.CustomerID= b.CustomerID
where Mobile_Number = @Mobile
ORDER BY Beneficiary_Name ASC
OFFSET  0 ROWS 
FETCH NEXT 1 ROWS ONLY

Select @Beneficiary2 = Beneficiary_Name 
from Beneficiary_master as a 
join Customer_Master as b on a.CustomerID= b.CustomerID
where Mobile_Number = @Mobile
ORDER BY Beneficiary_Name ASC
OFFSET  1 ROWS 
FETCH NEXT 1 ROWS ONLY

Select @Beneficiary Beneficiary, @Beneficiary2 Beneficiary2