[英]UDF to evaluate a condition using select statement
這是UDF功能要求:
CREATE FUNCTION [dbo].[ConditionEvaluation]
(@condEquation VARCHAR(MAX),
@condParameters VARCHAR(MAX
)
RETURNS bit
AS
@condEquation
和@condParameters
@condEquation = (C01 & C02)
和@condParameters =10
@condEquation = ((C01 & C02) | C3)
和@condParameters =101
因此,對於@condEquation
每個C系列條件,第二個參數(即@condParameters
提供對應的0或1值。
我想將以上條件評估為...
select (1 & 0)
select ((1 & 0) | 0)
參數@condEquation
在方程式中可能包含任意數量的@condEquation
但是parameter2中將有相應的位數。
我在這里利用SQL Select語句的條件評估功能,並希望將評估結果返回為0或1。
如何使用UDF做到這一點?
為了做到這一點,我必須做兩個功能:
create function [dbo].[f_test1](@CondEquation varchar(50))
returns bit
as
begin
declare @result bit
;with a as
(
select replace(replace(replace(@CondEquation, '(',''), ')',''), ' ','') n
),
b as
(
select n, 1 rn from a
union all
select stuff(n, patindex('%&%', n) - 1, 3 , case when substring(n, patindex('%&%', n) - 1, 3) like '%0%' then 0 else 1 end), rn+1
from b
where patindex('%&%', n)> 0
), c as
(
select n from (
select n, row_number() over (order by rn desc) rn2 from b
) a where rn2 = 1
), d as
(
select n, 1 rn from c
union all
select stuff(n, patindex('%|%', n) - 1, 3 , case when substring(n, patindex('%|%', n) - 1, 3) like '%1%' then 1 else 0 end), rn+1
from d
where patindex('%|%', n)> 0
), e as
(
select n from (
select n, row_number() over (order by rn desc) rn2 from d
) a where rn2 = 1
)
select @result=n from e
return @result
end
go
create function [dbo].[f_test2](@CondEquation varchar(max), @condparameters varchar(max))
returns bit
as
begin
declare @result bit
declare @i int = 1
while @i <= len(@condparameters)
begin
set @CondEquation = replace(@CondEquation, 'c' + right(@i+100, 2), substring(@condparameters, @i, 1))
set @i += 1
end
declare @returnvalue bit
;with a as
(
select @CondEquation a, 1 rn
union all
select stuff(a.a, z.a-z.b+1, z.b+1,[dbo].[f_test1](substring(a.a, z.a-z.b+1, z.b+1)) ), rn+1 from
a cross apply(
select patindex('%_)%', a.a) a, charindex('(', reverse(left(a.a, patindex('%_)%', a.a)))) b
where a.a like '%)%'
) z
), b as
(
select a, row_number() over (order by rn desc) rn from a
)
select @returnvalue = [dbo].[f_test1](a) from b where rn = 1
return @returnvalue
end
go
您可以像這樣測試功能
select dbo.[f_test2]('((C01 & C02) | C03)', '110')
請注意,使用正確的參數格式很重要。 你可以不寫C3代替C03。 我懇求您回去,對您的舊問題進行評估以獲取正確答案。
CREATE FUNCTION [dbo].[ConditionEvaluation]
(@condEquation VARCHAR(MAX),
@condParameters VARCHAR(MAX)
)
RETURNS bit
AS
BEGIN
declare @len int=len(@condParameters)
declare @val varchar(100)
declare @i int=1
declare @Sindex int=0
declare @op bit
set @condEquation=replace(replace(@condEquation,' ',','),')',',)')
while(@i<=@len )
begin
set @val=SUBSTRING (@condParameters,@i,1)
set @Sindex =CHARINDEX ('C',@condEquation)
set @condEquation=stuff(@condEquation,@Sindex ,charindex(',',@condEquation)-2 ,@val)
set @i=@i+1
end
set @condEquation= 'select @OP1'+replace(@condEquation,',','')+' as output1'
execute sp_executesql @condEquation,N'@OP1 int OUTPUT',@OP1=@OP OUTPUT
return @OP
END
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.