问题描述
您好,
我想在sql server中创建.net等效函数。 .Net功能如下
Hello,
I want to create a .net equivalent function in sql server. .Net function is as follows
private string getEligibility(int? elig, string overridden)
{
string ret = elig == 0 ? "Red" : elig == 1 ? "Green" : elig == 2 ? "Yellow" : elig == 3 ? "Purple" : elig == 4 ? "P" : elig == 5 ? "NA" : elig == 6 ? "PB" : elig == 7 ? "EX" : elig == 8 ? "PA" : elig.ToString();
if (!string.IsNullOrEmpty(overridden) && overridden == "Y")
{
ret += "^";
}
return ret;
}
和上面一样我需要在sql server中创建。
我试过如下所示,但只获得资格。我还需要检查其重写值,然后需要返回最终值。 .net代码的大胆部分我无法在sql server中编写。
我尝试了什么:
创建函数[dbo]。[udfgetEligibility]
(
@eligibilty as int
)
RETURNS varchar(100)
as
开始
返回CASE
WHEN @eligibilty = 0那么'RED'
当@eligibilty = 1然后'绿色'
当@eligibilty = 2然后'黄色'
WHEN @eligibilty = 3然后'紫'
WHEN @eligibilty = 4然后'P'
WHEN @eligibilty = 5然后'NA'
当@eligibilty = 6然后'PB'
WHEN @eligibilty = 7然后'EX'
当@eligibilty = 8然后'PA '
- 等......
ELSE CONVERT(varchar(10),@ eligibilty)
结束
end
Same like above I need to create in sql server.
I have tried as below but only getting eligibility. I also need to check its overridden value and then need to return final value. Bold part of .net code I am not able to write in sql server.
What I have tried:
create function [dbo].[udfgetEligibility]
(
@eligibilty as int
)
RETURNS varchar (100)
as
begin
return CASE
WHEN @eligibilty = 0 THEN 'RED'
WHEN @eligibilty = 1 THEN 'GREEN'
WHEN @eligibilty = 2 then 'YELLOW'
WHEN @eligibilty = 3 then 'Purple'
WHEN @eligibilty = 4 then 'P'
WHEN @eligibilty = 5 then 'NA'
WHEN @eligibilty = 6 then 'PB'
WHEN @eligibilty = 7 then 'EX'
WHEN @eligibilty = 8 then 'PA'
-- etc...
ELSE CONVERT(varchar(10), @eligibilty)
end
end
推荐答案
ID Description
0 RED
1 GREEN
2 YELLOW
...
然后它非常简单:
Then it's pretty trivial:
SELECT ISNULL((SELECT Description FROM Mappings WHERE ID = @eligibilty), @eligibilty)
这篇关于如何在SQL Server中创建一个.NET等效函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!