这是一个脚本,即使表中的数据发生变化,它也会显示 cEquationResult,它只能处理位运算符 & 和 |
代表您的表格的表格:
create table t_test(condition1 bit, condition2 bit, condition3 bit, CondEquation varchar(20))
insert t_test values(1,0, 0, 'c1&c2|c3')
insert t_test values(1,1, 1, 'c1&c2 | c3')
go
计算计算位的函数。是的,这是一个卑鄙的人:
create function f_t(@condition1 bit, @condition2 bit, @condition3 bit, @CondEquation varchar(10))
returns bit
as
begin
declare @result bit
;with a as
(
select replace(replace(replace(replace(@CondEquation, 'c1',@condition1), 'c2',@condition2), 'c3',@condition3), ' ','') 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
添加额外字段以显示计算的位
ALTER TABLE t_test ADD cEquationResult AS
dbo.f_t(condition1, condition2, condition3, CondEquation)
测试脚本:
select * from t_test