MSSQL编程笔记一初识函数 发现T-SQL还是可以像c语言的,只不过是在某些情况下。 一个十六进制转十进制的例子: create function Hex_to_Dec(@Hex_num varchar(20)) returns int as begin declare @m int declare @return_value int set @m=1 set @return_value=0 while @m<=len(@Hex_num) begin set @return_value=@return_value+convert(int, ( case when substring(@Hex_num,@m,1)<='9' then substring(@Hex_num,@m,1) when substring(@Hex_num,@m,1)='A' then '10' when substring(@Hex_num,@m,1)='B' then '11' when substring(@Hex_num,@m,1)='C' then '12' when substring(@Hex_num,@m,1)='D' then '13' when substring(@Hex_num,@m,1)='E' then '14' when substring(@Hex_num,@m,1)='F' then '15' end ))*power(16,len(@Hex_num)-@m) set @m=@m+1 end return @return_value end 函数的写法跟c语言相似极了,只是吧几个关键字换了,在这里的case when语句,跟c语言的switch case很相似。麻烦的是每次变量赋值都要加set关键字,略蛋疼。