我有一个名为EmployeeTransfers的表,其中包含EmpIdSSN,EmpBasePay,EmpTransferState和EmpFederalFilingStatus列。我创建的函数是接受两个参数,即状态和归档状态,由此可以通过if语句确定代码是什么并返回该代码。我目前的错误如下:
Msg 444,Level 16,State 2,Procedure GetStateFilingStatus,Line 6 [Batch Start Line 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
答案 0 :(得分: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