Hàm trả về một số nguyên

Hàm trả về một số nguyên

VD1:
Create function CountAge
(
   @Age int
)
returns int
as
begin
   declare @intAge int
   set @intAge= (select count(abc)
            from Table1
            where abc=@Age)
   return @intAge
end


select dbo.CountAge (12)
print dbo.CountAge (12)  


VD2:
USE [WebjetDW]
GO
/****** Object:  UserDefinedFunction [dbo].[udf_GetNumDaysInMonth]    Script Date: 07/15/2010 19:53:32 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE FUNCTION [dbo].[udf_GetNumDaysInMonth] ( @myDateTime DATETIME )
RETURNS INT
AS
BEGIN
DECLARE @rtDate INT
SET @rtDate =
CASE WHEN MONTH(@myDateTime)
IN (1, 3, 5, 7, 8, 10, 12) THEN 31
WHEN MONTH(@myDateTime) IN (4, 6, 9, 11) THEN 30
ELSE CASE WHEN (YEAR(@myDateTime) % 4 = 0
AND
YEAR(@myDateTime) % 100 != 0)
OR
(YEAR(@myDateTime) % 400 = 0)
THEN 29
ELSE 28 END
END
RETURN @rtDate
END