### 定义一个用户标量函数,用以实现指定年月,当月有多少天

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
create function getDaysOfMonth(@year int,@month int) returns int 
as begin
	declare @days int
	if @year%400 =0 or (@year %4 =0 and @year %100 != 0)
		set @days = 29
	else
		set @days = 28
	set @days = 
	case 
		when @month = 2 then @days
		when @month  = 4 or @month  = 6 or @month  = 9 or @month  = 11 then 30
		else 31
		end
		return @days
	end

用流程控制语句编写程序,求1×2×3×…×100的积。

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
create function getDaysOfMonth(@year int,@month int) returns int 
as begin
	declare @days int
	if @year%400 =0 or (@year %4 =0 and @year %100 != 0)
		set @days = 29
	else
		set @days = 28
	set @days = 
	case 
		when @month = 2 then @days
		when @month  = 4 or @month  = 6 or @month  = 9 or @month  = 11 then 30
		else 31
		end
		return @days
	end