Sql 即使范围字段都被索引,方法的性能特征也可能不同(虽然在实践中,我不希望这样)。我希望我可以将两个答案都标记为已接受,但我必须先选择解决问题的答案。但两者都很棒 create function dbo.ValidateStatusPeriodInfoTime
Sql 即使范围字段都被索引,方法的性能特征也可能不同(虽然在实践中,我不希望这样)。我希望我可以将两个答案都标记为已接受,但我必须先选择解决问题的答案。但两者都很棒 create function dbo.ValidateStatusPeriodInfoTime,sql,sql-server,sql-function,check-constraints,Sql,Sql Server,Sql Function,Check Constraints,即使范围字段都被索引,方法的性能特征也可能不同(虽然在实践中,我不希望这样)。我希望我可以将两个答案都标记为已接受,但我必须先选择解决问题的答案。但两者都很棒 create function dbo.ValidateStatusPeriodInfoTimeRange ( @btf_id VARCHAR(32), @start_time BIGINT, @end_time BIGINT ) returns bit as begin declare @Valid bit =
即使范围字段都被索引,方法的性能特征也可能不同(虽然在实践中,我不希望这样)。我希望我可以将两个答案都标记为已接受,但我必须先选择解决问题的答案。但两者都很棒
create function dbo.ValidateStatusPeriodInfoTimeRange
(
@btf_id VARCHAR(32),
@start_time BIGINT,
@end_time BIGINT
)
returns bit
as
begin
declare @Valid bit = 1;
if exists( select *
from dbo.StatusPeriodInfoOccurrence o
where o.btf_id = @btf_id
and @start_time <= o.end_time and o.start_time <= @end_time )
set @Valid = 0;
return @Valid;
alter table dbo.StatusPeriodInfoOccurrence with nocheck add constraint
CK_StatusPeriodInfoOccurrence_ValidateTimeRange
check (dbo.ValidateStatusPeriodInfoTimeRange(btf_id, start_time, end_time) = 1);
The INSERT statement conflicted with the CHECK constraint
"CK_StatusPeriodInfoOccurrence_ValidateTimeRange". The conflict occurred in
database "D600600TD01_BSM_Surveillance", table "dbo.StatusPeriodInfoOccurrence".
DECLARE @ReturnValue INT
EXEC @ReturnValue = ValidateStatusPeriodInfoTimeRange
@btf_id = 'a596933eff9143bceda5fc5d269827cd',
@start_time = 2432432,
@end_time = 432432423
SELECT @ReturnValue
INSERT INTO StatusPeriodInfoOccurrence (btf_id, start_time, end_time) VALUES ('a596933eff9143bceda5fc5d269827cd',2432432,432432423);
select *
from dbo.StatusPeriodInfoOccurrence o
where o.btf_id = @btf_id
and @start_time <= o.end_time and o.start_time <= @end_time
if (select count(*)
from dbo.StatusPeriodInfoOccurrence o
where o.btf_id = @btf_id
and @start_time <= o.end_time and o.start_time <= @end_time ) > 1
create function dbo.ValidateStatusPeriodInfoTimeRange
(
@id INT,
@btf_id VARCHAR(32),
@start_time BIGINT,
@end_time BIGINT
)
returns bit
as
begin
declare @Valid bit = 1;
if exists( select *
from dbo.StatusPeriodInfoOccurrence o
where o.id <> @id AND o.btf_id = @btf_id
and @start_time <= o.end_time and o.start_time <= @end_time )
set @Valid = 0;
return @Valid;
end;
check (dbo.ValidateStatusPeriodInfoTimeRange(id, btf_id, start_time, end_time) = 1)