会员
周边
新闻
博问
AI培训
云市场
所有博客
当前博客
我的博客
我的园子
账号设置
简洁模式
...
退出登录
注册
登录
Freiheit
Simple is beautiful
首页
订阅
管理
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关键字,略蛋疼。
posted @
2012-04-17 23:27
xiepeixing
阅读(
203
) 评论(
0
)
编辑
收藏
举报
会员力量,点亮园子希望
刷新页面
返回顶部