0

UDF 関数の要件は次のとおりです。

CREATE FUNCTION [dbo].[ConditionEvaluation]

(@condEquation VARCHAR(MAX),
@condParameters VARCHAR(MAX
)
RETURNS bit
AS

@condEquationとの値@condParameters

  • 例 1:@condEquation = (C01 & C02)および@condParameters =10
  • 例 2:@condEquation = ((C01 & C02) | C3)および@condParameters =101

そのため、すべての C シリーズの条件に対して、@condEquation対応する 0 または 1 の値が 2 番目のパラメーターで提供されます@condParameters

上記の条件を次のように評価したい...

  • 例 1:select (1 & 0)
  • 例 2:select ((1 & 0) | 0)

パラメーター@condEquationには、方程式内の任意の数の C が含まれる場合があります。ただし、パラメータ 2 には対応するビット数があります。

ここでは SQL Select ステートメントの条件評価機能を利用しており、評価結果を 0 または 1 として返したいと考えています。

UDFを使用してそれを行う方法は?

4

2 に答える 2

0
    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
于 2012-07-31T17:38:33.933 に答える
0

これを達成するために、2 つの関数を作成する必要がありました。

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')

パラメータの正しい形式を使用することが重要であることに注意してください。C03 の代わりに C3 を書くことはできません。以前の質問に戻って正しい答えを評価することをお勧めします。

于 2012-07-31T15:42:00.687 に答える