Home > Database > Mysql Tutorial > SQLServer技巧集

SQLServer技巧集

WBOY
Release: 2016-06-07 15:51:18
Original
1276 people have browsed it

1.把某个字段重新生气序列(从1到n): DECLARE @i int Set @i = 0 Update Table1 Set @i = @i + 1,Field1 = @i 2.按成绩排名次 Update 成绩表 Set a.名次 = ( Select Count(*) + 1 From 成绩表 b Where a.总成绩 b.总成绩 ) From 成绩表 a 3.查询外部数据库 Se

1.把某个字段重新生气序列(从1到n):
DECLARE @i int
Set @i = 0
Update Table1 Set @i = @i + 1,Field1 = @i

2.按成绩排名次
Update 成绩表
Set a.名次 = (
Select Count(*) + 1
From 成绩表 b
Where a.总成绩 )
From 成绩表 a

3.查询外部数据库
Select a.*
From OpenRowSet('Microsoft.Jet.OLEDB.4.0','c:/test.mdb';'admin';'',Table1) a

4.查询Excel文件
Select *
From OpenDataSource('Microsoft.Jet.OLEDB.4.0','Data Source="c:/test.xls";User ID=Admin;Password=;Extended properties=Excel 8.0')...Sheet1$

5.在查询中指定排序规则
Select * From Table1 Order By Field1 COLLATE Chinese_PRC_BIN
为什么要指定排序规则呢?参见:
http://www.delphibbs.com/delphibbs/dispq.asp?lid=1633985
例,检查数据库中的Pub_Users表中是否存在指定的用户:
Select Count(*) From Pub_Users Where [UserName]='admin' And [PassWord]='aaa' COLLATE Chinese_PRC_BIN
默认比较是不区分大小写的,如果不加COLLATE Chinese_PRC_BIN,那么密码aaa与AAA是等效的,这当然与实际不符.注意的是,每个条件都要指定排序规则,上例中用户名就不区分大小写.


6.Order By的一个小技巧
Order By可以指定列序而不用指定列名,在下面的例子里说明它的用处(注意,第三列未指定别名)
Select a.ID,a.Name,(Select Count(*) From TableB b Where a.ID=b.PID) From TableA a Order By 3 

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template