两种方法的优缺点:
第一种方法采用自定义函数实现,无须另外写dll,也无须考虑安全性的问题,大家知道sql server中使用sp_oacreate这个存储过程是很危险的,而且执行sp_oacreate需要sysadmin权限,而第一种方法就可以不必考虑这个问题,但第一种方法的缺点是不好控制字符输出格式,而第二种方法很容易控制字符输出的格式
两种方法性能上的比较
我们做如下测试:
新建一Table,如下:
CREATE TABLE Int_To_Hex
(
intValue int ,
strHex varchar(255))
DECLARE @I INT
SET @I=0
WHILE(@I<=10000)
BEGIN
INSERT INTO Int_To_Hex(intValue) VALUES(@I)
SET @I=@I+1
END
我的电脑配置:
IBMX30 RAM:512M CPU:P3 1066
第二种方法:
UPDATE Int_To_Hex SET strHex=dbo.fn_GetHexStr(intValue,3)
UPDATE 10001笔,总共耗时13秒,update结果如下:
intValue strHex
—————————
0 000
1 001
2 002
3 003
4 004
5 005
6 006
7 007
8 008
9 009
10 00A
11 00B
12 00C
13 00D
第一种方法:
UPDATE Int_To_Hex SET strHex=dbo.fn_hexadecimal(intValue)
同样是10001笔资料,共耗时1秒,结果如下:
intValue strHex
—————————
0 000
1 001
2 002
3 003
4 004
5 005
6 006
7 007
8 008
9 009
10 00A
11 00B
12 00C
13 00D
当然这种测试方法未必是正确的,但从中可以看出二种方法的优点是:
第一种方法:容易控制输出字符的长度
第二种方法:性能上有很大的优势
至于采用哪种方法,看各位的需要了:)
相关连接:
SQL SERVER中实现十六进制转换为字符的两种方法(一)
No Comments
Be the first to comment on this entry.
Leave a comment
Fields in bold are required. Email addresses are never published or distributed.
Some HTML code is allowed:
<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>URLs must be fully qualified (eg: http://www.dbifan.com),and all tags must be properly closed.
Line breaks and paragraphs are automatically converted.
Please keep comments relevant. Off-topic, offensive or inappropriate comments may be edited or removed.