如何创建一个函数,以便它从 if 语句中返回一个字符?
Posted
技术标签:
【中文标题】如何创建一个函数,以便它从 if 语句中返回一个字符?【英文标题】:How do you create a function so that it returns a char from an if statement? 【发布时间】:2017-03-16 21:48:08 【问题描述】:我有一个名为 EmployeeTransfers 的表,其中包含 EmpIdSSN、EmpBasePay、EmpTransferState 和 EmpFederalFilingStatus 列。我创建的函数采用两个参数,即状态和归档状态,据此它应该通过遍历 if 语句并返回该代码来确定代码是什么。我目前遇到的错误如下:
消息 444,级别 16,状态 2,过程 GetStateFilingStatus,第 6 行 [批处理开始第 62 行] 函数中包含的 Select 语句无法将数据返回给客户端。
我遇到的另一个问题是我想捕获 if 语句中未包含的所有其他状态,以发送一个通用代码“S”表示单身,“M”表示已婚。我试图在最后做一个 else 语句,但一直遇到各种错误。
create table EmployeeTransfers
(
EmpIdSSN char(9),
EmpBasePay decimal(16,4),
EmpTransferState char(2),
EmpFederalFilingStatus char(1)
)
GO
--Input data
insert into EmployeeTransfers
values
('555342121',60000.00, 'CA', 'S'),
('552342120',70000.00, 'NY', 'M'),
('552342120',87000.00, 'AZ', 'M'),
('456772332',55000.00, 'CT', 'S'),
('332223445',33000.00, 'NJ', 'M'),
('550667788',54000.00, 'DC', 'M')
GO
create function dbo.GetStateFilingStatus(@EmpFederalFilingStatus char(1), @EmpTransferState char(2))
returns char(1)
as
begin
declare @StateFilingStatus char;
select @StateFilingStatus
from EmployeeTransfers
where EmpFederalFilingStatus = @EmpFederalFilingStatus
and EmpTransferState = @EmpTransferState;
if (@EmpTransferState = 'MS') and (@EmpFederalFilingStatus = 'S')
set @StateFilingStatus = 'A';
else
set @StateFilingStatus = 'M';
return @StateFilingStatus;
if (@EmpTransferState = 'NJ') and (@EmpFederalFilingStatus = 'S')
set @StateFilingStatus = 'B';
else
set @StateFilingStatus = 'A';
return @StateFilingStatus;
if (@EmpTransferState = 'AZ') and (@EmpFederalFilingStatus = 'S')
set @StateFilingStatus = 'A';
else
set @StateFilingStatus = 'B';
return @StateFilingStatus;
if (@EmpTransferState = 'CT') and (@EmpFederalFilingStatus = 'S')
set @StateFilingStatus = 'F';
else
set @StateFilingStatus = 'M';
return @StateFilingStatus;
if (@EmpTransferState = 'DC') and (@EmpFederalFilingStatus = 'S')
set @StateFilingStatus = 'S';
else
set @StateFilingStatus = 'Y';
return @StateFilingStatus;
end;
go
【问题讨论】:
您的预期结果是什么? @McNets 用于函数输出“StateFilingStatus = codeReturned”。例如,如果您输入函数“NJ”和“S”,它将返回“B”,即代码。 正如错误所说,您正在执行此操作 select @StateFilingStatus 但函数旨在返回一个值,不执行要返回的选择语句。删除该选择 还有更多错误。 但标量格式执行此操作 SELECT @ret = SUM(p.Quantity) 将选择(标量)的结果分配给变量。您不是在做 dat,而是在做select @StateFilingStatus from EmployeeTransfers where EmpFederalFilingStatus = @EmpFederalFilingStatus and EmpTransferState = @EmpTransferState;
,这将尝试从 select 中返回一个表格结果
【参考方案1】:
我猜你想要的是:
create function dbo.GetStateFilingStatus(@EmpFederalFilingStatus char(1), @EmpTransferState char(2))
returns char(1)
as
begin
declare @StateFilingStatus char;
if (@EmpTransferState = 'MS')
begin
if (@EmpFederalFilingStatus = 'S')
set @StateFilingStatus = 'A';
else
set @StateFilingStatus = 'M';
end
else
if (@EmpTransferState = 'NJ')
begin
if (@EmpFederalFilingStatus = 'S')
set @StateFilingStatus = 'B';
else
set @StateFilingStatus = 'A';
end
else
if (@EmpTransferState = 'AZ')
begin
if (@EmpFederalFilingStatus = 'S')
set @StateFilingStatus = 'A';
else
set @StateFilingStatus = 'B';
end
else
if (@EmpTransferState = 'CT')
begin
if (@EmpFederalFilingStatus = 'S')
set @StateFilingStatus = 'F';
else
set @StateFilingStatus = 'M';
end
else
if (@EmpTransferState = 'DC')
begin
if (@EmpFederalFilingStatus = 'S')
set @StateFilingStatus = 'S';
else
set @StateFilingStatus = 'Y';
end
else
begin -- this is your catch-all condition for all other States
-- you will probably need to modify this because I don't know
-- what your criteria are for returning 'S' or 'M'
if (@EmpFederalFilingStatus = 'S')
set @StateFilingStatus = 'S';
else
set @StateFilingStatus = 'M';
end
return @StateFilingStatus
end;
go
【讨论】:
正是我试图实现的目标,为每个 if 语句使用 begin 和 end 以允许在不满足参数时跳过它?begin...end
块实现了 Adam V 建议的嵌套。begin...end
块之外的 if
语句使您进入特定状态,然后块内部告诉您要做什么一旦你决定了国家是什么,每个申报状态。以上是关于如何创建一个函数,以便它从 if 语句中返回一个字符?的主要内容,如果未能解决你的问题,请参考以下文章