IF OBJECT_ID('[dbo].[get_service_status]') IS NOT NULL
DROP FUNCTION [dbo].[get_service_status]
GO
CREATE FUNCTION [dbo].[get_service_status]
(
@arrear_time datetime,
@reminder_time int, (单位:天)
@buffer_time int (单位:天)
)
RETURNS nvarchar(10)
AS
BEGIN
DECLARE @service_status nvarchar(10)
SET @service_status = '状态1'
-- 当前时间
DECLARE @current_time datetime
--SET @current_time = getdate() -- sqlserver2000中不能这样调用
SELECT @current_time = Today FROM [dbo].[view_get_date]
-- 时间差
DECLARE @nDiffDay int
SELECT @nDiffDay = datediff(dd, @current_time, @arrear_time)
IF (@nDiffDay > @reminder_time)
BEGIN
SET @service_status = '状态1'
END
ELSE IF (@nDiffDay <= @reminder_time AND @nDiffDay >= 0)
BEGIN
SET @service_status = '状态2'
END
ELSE IF (@nDiffDay < 0 AND @nDiffDay >= -@buffer_time)
BEGIN
SET @service_status = '状态3'
END
ELSE
BEGIN
SET @service_status = '状态4'
END
RETURN @service_status
END
GO