我有以下代碼。我試圖通過傳遞@Hire_Date Datetime來測試函數,看看會返回什麼值然而,我是SQL Server 2012的新手,不知道如何去做這件事。SQL測試用戶定義函數
謝謝。
CREATE FUNCTION dbo.getYearsOfService (@Hire_Date Datetime)
RETURNS int
AS BEGIN
DECLARE @thisYear int
DECLARE @thisMonth int
DECLARE @hireYear int
DECLARE @hireMonth int
DECLARE @yearlengthOfService int
DECLARE @monthlengthOfService int
SET @thisYear = YEAR(getdate())
SET @thisMonth = MONTH(getdate())
SET @hireYear = YEAR('@Hire_Date')
SET @hireMonth = MONTH('@Hire_Date')
SET @yearlengthOfService = (@thisYear - @hireYear);
SET @monthlengthOfService = (@thisMonth - @hireMonth);
IF (@monthlengthOfService < 0)
SET @yearlengthOfService = (@yearlengthOfService - 1);
RETURN @yearlengthOfService
END;
您是否知道SQLServer包含自己的函數來查找兩個日期之間的差異? https://msdn.microsoft.com/en-GB/library/ms189794.aspx –