我有一个测试,我必须经常在查询中使用,以查看CPT结算代码是否是可计费的遭遇。在SQL查询中,测试如下所示:
where (pvp.code between '99201' and '99215'
or pvp.code between '99221' and '99239')
加上更多的范围。
我试图通过将其插入到函数中来创建函数,但是我得到了“错误的语法”错误 - 真的不是一个巨大的惊喜。
CREATE FUNCTION IsEncounter
(
@code varchar(20)
)
RETURNS bit
AS
BEGIN
DECLARE @Result bit;
SELECT @Result = @code between '99201' and '99215'
or @code between '99221' and '99239';
-- Return the result of the function
RETURN @Result;
END
我想我可以做一些像
这样的事情if (@code >= '99201' and @code <= '99215')
or (@code >= '99221' and @code <= '99239')
select @Result = 1
else
select @Result = 0;
但我想知道最干净的方法。感谢。
答案 0 :(得分:4)
这样的事情应该有效:
CREATE FUNCTION IsEncounter
(
@code varchar(20)
)
RETURNS bit
AS
BEGIN
DECLARE @Result bit
SET @Result = CASE WHEN (@code between '99201' and '99215') or (@code between '99221' and '99239') THEN 1 ELSE 0 END
RETURN @Result
END
答案 1 :(得分:3)
我认为你的UDF几乎是正确的。我认为只有一些语法问题。请尝试以下方法:
CREATE FUNCTION IsEncounter
(
@code varchar(20)
)
RETURNS bit
AS
BEGIN
DECLARE @Result bit = 0
IF @code between '99201' and '99215' or @code between '99221' and '99239'
BEGIN
SET @Result = 1
END
RETURN @Result
END
然后你的WHERE子句就是:
where dbo.IsEncounter(p.Code)=1
答案 2 :(得分:1)
我将我的范围移到表格中,然后执行此操作:
CREATE FUNCTION IsEncounter ( @code VARCHAR(20) )
RETURNS BIT
AS
BEGIN
DECLARE @MatchedRanges SMALLINT ;
DECLARE @Result BIT ;
SELECT @MatchedRanges = COUNT(*)
FROM tblRangeWhiteList
WHERE @code BETWEEN RangeStart AND RangeEnd
IF @MatchedRanges > 0
SET @Result = 1
ELSE
SET @Result = 0
RETURN @Result
END