[英]Create a user defined function?
I'm trying to create an user defined function with take 2 parameters the federal filing status char 1 where have data like S and M which are single and married and the second parameter is state char 2 where have data created already in a table EmployeeTransfers have MS,FL,NY etc.. 我正在尝试创建一个带有2个参数的用户定义函数,联邦申报状态为char 1,其中有S和M这样的数据是已婚和已婚,第二个参数为state char 2,其中数据已在表EmployeeTransfers中创建MS,FL,NY等。
I have create a functoin that returns one of the code which is of data type char 1 with this logic example: IF State = NY and Federal Filing Single(S) then Code is A or Federal Filing Married(M) then Code is B 我创建了一个functoin,它使用以下逻辑示例返回数据类型为char 1的代码之一:IF State = NY和Federal Filing Single(S),则代码为A或Federal Filing Married(M),然后代码为B
etc... 等等...
so there my code my returns are NULL i dnt knw how to set the return more specific 所以我的代码我的回报是NULL我不知道如何设置回报更具体
create function dbo.GetStateFilingStatus
(
@federalfilingstatus char(1),
@state char(2)
) returns char(1)
as
begin
declare @code char(1) = null
select @federalfilingstatus = EmpFederalFilingStatus from EmployeeTransfers
where @state = EmpTransferState
if (@state = 'MS' AND @federalfilingstatus = 'S')
set @code = 'A'
return @code
if (@state = 'MS' AND @federalfilingstatus = 'M')
set @code = 'M'
return @code
if (@state = 'NJ' AND @federalfilingstatus = 'S')
set @code = 'B'
return @code
if (@state = 'NJ' AND @federalfilingstatus = 'M')
set @code = 'A'
return @code
if (@state = 'AZ' AND @federalfilingstatus = 'S')
set @code = 'A'
return @code
if (@state = 'AZ' AND @federalfilingstatus = 'M')
set @code = 'B'
return @code
if (@state = 'CT' AND @federalfilingstatus = 'S')
set @code = 'F'
return @code
if (@state = 'CT' AND @federalfilingstatus = 'M')
set @code = 'M'
return @code
if (@state = 'DC' AND @federalfilingstatus = 'S')
set @code = 'S'
return @code
if (@state = 'DC' AND @federalfilingstatus = 'M')
set @code = 'Y'
return @code
end
Go
I tried to execute it with 我试图用
select dbo.GetStateFilingStatus(EmpTransferState ,EmpFederalFilingStatus) as StateFilingStatus from EmployeeTransfers
my returns are nulls. 我的回报为空。
Your code is returning at the very first return because it doesn't know it's part of the if block. 您的代码将在第一次返回时返回,因为它不知道它是if块的一部分。 Just remove all the returns and have one return at the very end.
只需删除所有退货并在最后获得一份退货即可。
A better way of doing this is to create a lookup table that you can join to. 更好的方法是创建一个可以加入的查找表。 That way, if you ever need to add a filing status or state, you can just add a row.
这样,如果您需要添加文件状态或状态,则只需添加一行即可。 And if you need to modify a code, you can just update the table.
而且,如果您需要修改代码,则只需更新表即可。
You are missing BEGIN
and END
statements after the IF
, otherwise SQL will execute treat the first statement as being inside the IF block and the rest after. 您在
IF
之后缺少BEGIN
和END
语句,否则SQL将执行第一个语句,将其视为在IF块内,其余的则视为之后。 Anyway, you don't need to use the @code
variable here at all, just return the value directly: 无论如何,您根本不需要在这里使用
@code
变量,只需直接返回值即可:
if (@state = 'MS' AND @federalfilingstatus = 'S')
return 'A'
if (@state = 'MS' AND @federalfilingstatus = 'M')
return 'M'
if (@state = 'NJ' AND @federalfilingstatus = 'S')
return 'B'
--etc....
A better solution however would be to use a lookup table instead of this complicated list of conditionals. 但是,更好的解决方案是使用查找表代替此复杂的条件列表。 That way you can add to it and not have to worry about maintaining a complex stored procedure.
这样,您可以添加它,而不必担心维护复杂的存储过程。
You need to add BEGIN
and END
in your IF
statements: 您需要在
IF
语句中添加BEGIN
和END
:
if (@state = 'MS' AND @federalfilingstatus = 'S') begin
set @code = 'A'
return @code
end
What happened before is that, without the BEGIN - END
statements the RETURN
statement is executed after evaluating the first IF
statement. 之前发生的事情是,没有
BEGIN - END
语句,则在评估第一个IF
语句之后执行RETURN
语句。
Alternatively, you can use IF
- ELSE IF
combination to set the value of @code
and put the RETURN
statement at the end. 另外,您可以使用
IF
- ELSE IF
组合来设置@code
的值,并将RETURN
语句放在最后。
IF <condition> BEGIN
SET @code = 'A'
ELSE IF <condition> BEGIN
SET @code = 'B'
END
RETURN @code
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.