为列值指定生成方式如用checksum


Warning: Undefined array key "HTTP_REFERER" in /www/wwwroot/prod/www.enjoyasp.net/wp-content/plugins/google-highlight/google-hilite.php on line 58

表设计中增加列,在列的计算所得列的规范中录入公式即可
持久代表值是否实际存储起来还是每次查询时重新生成
如:ALTER TABLE dbo.tmp ADD cs_Pname AS CHECKSUM(c1);
为列计算校验值,然后在此列上加上索引,则比一般的查询速度要快
应用:1,字符串比较没有数字比较来得快,故可增加一列存字符串的校验值,在此值上加索引进行比较,速度将提升很多

2,SELECT CHECKSUM(NEWID()) 返回随机数1到n: abs(CHECKSUM(NEWID()))%n + 1

3,CHECKSUM(NEWID()) 返回8-10位数字,故要求11位以上的随机不重复数,可采用:
SELECT right(right(abs(CHECKSUM(NEWID())),3) + CONVERT(varchar(50),abs(CHECKSUM(NEWID()))),11)

4,随机时间:
SELECT DATEADD(mi,ABS(CHECKSUM(NEWID())%(1+DATEDIFF(mi,’2011-05-14 13:00′,’2011-05-14 14:00′))),’2011-05-14 13:00′)

5,比较两行多个列是否相同
SELECT CHECKSUM(1,2,4)
SELECT DISTINCT OBJECT_ID,NAME,TYPE
FROM sys.tables tb
WHERE CHECKSUM(OBJECT_ID,NAME,type)
IN(
SELECT CHECKSUM(OBJECT_ID,NAME,type)
FROM (
SELECT OBJECT_ID,NAME,TYPE FROM sys.objects o
)obj
)
注: checksum不区分大小写,若要区分,要用BINARY_CHECKSUM