
来自:http://www.voidcn.com/article/p-fvqxvkey-qb.html
--###################################################################################/* 缩写: DDL(Database DeFinition Language): 数据库定义语言 DML(Database Manipulation Language): 数据库 *** 作语言 DCL(Database Control Language): 数据库控制语言 DTM(Database Trasaction Management): 数据库事物管理 知识概要: |---1.查询Select | |---2.数据库定义语言DDL: 对表,视图等的 *** 作,包括create,drop,alter,rename,truncate | 数据库 *** 作--|---3.数据库 *** 作语言DML: 对记录进行的 *** 作,包括insert,delete,update | |---2.数据库控制语言DCL: 对访问权限等的 *** 作,包括grant,revoke | |---2.数据库事物管理DTM: 对事物的 *** 作,包括commit,rollback,savepoint 事物的是由DML(insert,update)开启的; 而引起事物的提交或结束原因有: 1.DTM *** 作: commit,savepoint 2.系统崩溃宕机: 隐式rollback 3.正常: 隐式commit 4.DDL和DCL *** 作: DDL(create,truncate) DCL(grant,revoke) 注意MS-sql的特殊处: MS-sql中的事物: 自动事物(commit)和手动事物(begin transaction). 在sql中DML(insert,update)会引起自动事物commit,而Oracle中不会 MS-sql的参数: 只能返回0或非0(如: 1,2)数字 MS-sql的存储过程: 一定会有返回值(return value),默认返回0(成功). 在程序获取改值,需要创建return参数对象(添加到参数列表),并设定其方向. MSsqlServer的登录过程分两种: 1. windows集成验证: windows用户名和口令 -> sqlServer登录列表 -> 映射到用户列表 -> 登录成功 2. sqlServer验证: sql用户名和口令 -> 映射到用户列表 -> 登录成功 两种登录方式的连接串: string connectionStr = "data source=.;database=Test;user ID=sa;password=sa"; string connectiongStr ="data source=.\sqlexpress;database=Test;integrated security=true"; 数据库设计原则: 1. 数据库设计指导原则(关系数据库中的数据通过表来体现): 先确定表后确定业务字段. 每个业务对象在数据库中对应一张表(若业务对象复杂则可对应多张表),业务对象间每有一个关系也要对应一张表. 注意: 业务字段需要跟项目结合,例如: 学生的健康情况可以用一个字段(优、良等)表示,但针对健康普查,学生的健康情况需进一步划分为身高、体重、血压等 如: 学校中的学生对象: 学院表(学院ID,专业ID); 专业表: 专业表(专业ID,专业名);学生表(学生ID,学院ID,专业ID) 2. 数据库设三大计原则: a. 完整性: 设计方案能够保存项目中的各种信息(要全) b. 低冗余: 通过主键保证记录的不重复、通过表间关系减少冗余字段 c. 尽可能满足3范式(normalForm): 1NF: 1个字段只能包含一个业务信息片(即项目中的业务字段表示的信息不可再分) 2NF: 能找到1个或多个字段的组合,用来唯一的确定表中的记录(即必须有主键). 3NF: 主键唯一且直接确定表中的其他字段(即无传递依赖,如: 教师ID,办公室ID,办公室电话关系中存在传递依赖) 注意事项: 尽可能不用业务字段做主键,通常的手段为自增列当主键,并且末尾添加默认时间字段(getdate()). 尽量采用sql92代码,保证可移植性. 如: 在sql2000中,top函数只能跟常量(sql2005可跟变量). 通用的解法为拼query串,用exec(query串)取结果 备注: sql中使用+拼接字符串,Oracle中使用||拼接字符串. C#数据类型: 整数: sbyte,byte,short,ushort,int,uint,long,ulong 实数: float,double,decimal 字符: char,string 布尔: boolean 日期: datetime 对象: object 全局唯一标识: GUID sql数据类型: 整数: bit(0,1),tinyint(8),smallint(16),int(32),bigint(64) 实数: float,real,numeric 字符: char(8000),nchar(4000),varchar(8000),nvarchar(4000),ntext 日期: smalldatetime(1900,1,1-2079,6,6),datetime(1753,1-9999,12,31) 货比: money 二进制: binary 图像: image 标识: uniqueIDentity */--###################################################################################--创建数据库:if exists(select * from sysdatabases where [name]='TestStudent') drop database TestStudentgocreate database TestStudenton( name = 'TestStudent_DB',--数据库逻辑名 filename = 'D:\WorkCode\DB\TestStudent.mdf',size = 3,filegrowth = 10,maxsize = 100)logon( name = 'TestStudent_Log',filename = 'D:\WorkCode\DB\TestStudent.log',maxsize = 100)go--###################################################################################use TestStudentgo--创建表,约束类型: 主键、外键、唯一、check约束、非空约束if exists( select * from sysobjects where [name] = 'T_Student' and [type] = 'U') drop table T_Studentgocreate table T_Student( Sno int IDentity(100,1) primary key,--可以使用scope_IDentity获得刚生成的ID Sname nvarchar(50) not null,Sgender nchar(1),Sage tinyint check(Sage >= 20 and Sage <= 30),home nvarchar(100) default('北京'),IDcard nvarchar(18) unique)goif exists( select * from sysobjects where [name] = 'T_score' and [type] = 'U') drop table T_scoregocreate table T_score( ID int primary key,Sno int IDentity(100,1) foreign key references T_Student(Sno),score tinyint)go--修改表结构alter table T_Student add Education nchar(3)goalter table T_Student drop column Educationgo--修改表约束alter table T_Student add constraint PK_Sno primary key(Sno),constraint CK_gender check(gender in('男','女')),constraint DF_home default('北京') for home,constraint UQ_IDcard unique(IDcard)goalter table T_score add constraint FK_Sno foreign key references T_Student(Sno)go--创建联合主键alter table T_SC with nocheck add constraint [PK_T_SC] primary key nonclustered( studentID,courseID)go--###################################################################################--新增(插入)数据insert into T_Student(Sname,Sgender,Sage) values('张三','男',23)goinsert into T_Student(Sname,Sage) select '李四',25 union select '王五','女',26 union select '赵六',28go--删除数据truncate table T_Student --只删除表的数据delete from T_Student where sgender = '男'--修改数据update T_Student set sgender = '女' where sgender='男'--###################################################################################--查询数据select * from T_Student where sgender = '女'select top 3 * from T_Student --选择前三项,top 3是用来筛选记录的,所以得紧跟select,不用考虑字段顺序select top 40 percent * from T_Student --选择前百分之几的记录select sname as '姓名','年龄' = sage from T_Student --起别名的两种方式as和=select * from T_Student where sage >= 20 and age <= 30select * from T_Student where sage between 20 and 30select * from T_Student where sage is nullselect sname into T_Stu from T_StuInfo --用T_StuInfo中的sname字段创建新表--模糊查询呢: 通配符: %(0-任意字符),_(1个字符),[abc](选择含a或b或c),[^abc](选择不含a或b或c)select * from T_Student where sname like '张%'select * from T_Student where sname like '王_'select * from T_Student where sname like '[张李]%'select * from T_Student where sname like '[^张李]%'--###################################################################################--排序: order by子句的结果将额外生成一个新表(2字段: 原位置索引字段和排好序的字段)select * from T_Student order by Sage desc --默认是升序asc--###################################################################################--聚合函数: 若有聚合函数,即使不写group by 子句,也会默认将表分为一个大组select sum([sID]) from T_Stuscoreselect count([sID]) from T_Stuscore --count(*)表示记录数,而count(字段)忽略掉null值select avg([sID]) from T_Stuscoreselect max([sID]) from T_Stuscoreselect min([sID]) from T_Stuscoreselect distinct([sID]) from T_Stuscore--###################################################################################--分组函数,where用于对记录的筛选,having用于对组的筛选select gender,Counter = count(*) from T_Stuinfo group by genderselect gender,Counter = count(*) from T_Stuinfo group by gender having count(*) >= 2--###################################################################################--表连接: 笛卡尔积(m*n条记录),内连接,外连接(左外连接、右外连接、全外连接)--笛卡尔积select sname,[sID],cID,scorefrom T_StuInfo s cross join T_Stuscore c on s.[sID] = c.[sID]--内连接: 先从m和n中选择,然后再连接select sname,score from T_StuInfo s inner join T_Stuscore c on s.[sID] = c.[sID]--左外连接(左连接): 内连接 + 左表剩余记录(右表记录置null)select sname,score from T_StuInfo s left join T_Stuscore c on s.[sID] = c.[sID]--右外连接(右连接): 内连接 + 右表剩余记录(左表记录置null)select sname,score from T_StuInfo s right join T_Stuscore c on s.[sID] = c.[sID]--全外连接(全连接): 内连接 + 左表剩余记录(右表记录置null) + 右表剩余记录(左表记录置null)select sname,score from T_StuInfo s full outer join T_Stuscore c on s.[sID] = c.[sID]--###################################################################################--函数: 字符串函数、日期函数、数学函数、系统函数--字符串函数print charindex('1','ab1cd') --sql中下表从1开始,类似于C#中indexof、lastindexof,返回包含'1'的下表位置print len('abcd') --C#中的lengthprint lower('ABCD') --Tolower(),toupper()print upper('abcd') --toupper()print ltrim(' abcd') --LTrim()print rtrim('abcd ') --RTrim()print rtrim(ltrim(' abcd ')) --Trim()print right('abcd',2) --从右截取,C#中的SubString(length-3,2)截取下表从0开始的2个print left('abcd',2) --SubString(0,2)print replace('abcdef','cd','1234') --Replace(),用1234替换cdupdate [card] set [passWord] = Replace(Replace([PassWord],'O','0'),'i','1') from T_UserInfoprint stuff('abcdef',2,3,'#') --填充替换,从第2个开始的3个字符替换成#print cast('2010-11-08' as datetime) --数据类型转换print convert(datetime,'2010-11-08') --数据类型转换print str(67) --数字变字符串print newID() --新uniqueIDentifIEr,它将会为记录临时添加一列,且内容是随机的print getdate() --获得当前日期--日期函数print getdate() --DateTime.Nowprint dateadd(yyyy,10,'2010-1-2') --增加10年print dateadd(mm,getdate()) --增加月,其他可以查sql联机丛书print datediff(yyyy,'1985-12-13','2010-10-10') --时间差距print datediff(dd,'2010-10-10') --时间差距print datename(mm,'2010-10-01') + '2' --返回月份+'2'print datepart(mm,'2010-10-01') + '2' --日期10+2,结果为12--数学函数print abs(-100) --求绝对值,Abs()print ceiling(10.05) --取整数,如果有小数则进1print floor(10.05) --取整数,忽略小数print round(10.567,2) --四舍五入print power(10,2) --求幂运算print sqrt(108) --求平方根print rand(10) --只能选择0-1之间的数,Random.Ran(0,1)print rand(10)*10 --随机0-10之间的数print sign(30) --只返回±1--系统函数print 'abcd' + convert(nvarchar(10),5) --ToString()print 'abcd' + cast(5 as nvarchar(10)) --同上print datalength('1+1=2') --返回表达式的字节数print current_user --返回当前登录的角色print host_name() --返回当前计算机名print system_user --返回当前用户IDprint user_name() --给定用户ID返回其角色名print isnull(filedname,0) --替换null为0raiserror('抛出异常',16,1) --抛出异常,1-15被系统占用,对应C#中的throwselect * from sysobjects where objectproperty(ID,N'IsUsertable') = 1 --判断是否用户表(y=1,n=0),N表示后边的串为unicode字符串.select * from sysobjects where type= 'U' --等价于上一行select databasepropertyex('northwind','IsbrokerEnabled') --查询该库是否开启缓存技术中的通知机制,1为开启,0为关闭alter database northwind set enable_broker --开启数据库中,缓存技术中的通知机制--注意以下三个函数的用法 eg: 结果集(1,5,11,17,19,25)select row_number() over(order by [sID]) from T_StuInfo --1,4,6select rank() over(order by [sID]) from T_StuInfo --1,6select dense_rank() over(order by [sID]) from T_StuInfo --1,4select ntile(2) over(order by [sID]) from T_StuInfo --1,5 11,17 19,25select row_number() over(order by [sID]) as sequence,sname,age,(case gender when '0' then '男' else '女' end) genderfrom T_StuInfo s left join T_Stuscore c on s.sID = c.sIDgo--###################################################################################--范式: 1NF: 原子性,2NF: 单主键,3NF: 去除传递依赖--E-R模型(表间关系): 1对1: 任何一个表添加外键; 1对多: 在多的一方添加外键; 多对多: 需要第三个表,添加前两表的外键--###################################################################################--变量--系统变量:select * from T_StuInfoprint @@IDentity; --获得结果集最大标识值print @@error; --有错误,返回大于0的数; 无错误返回0print @@rowcount; --返回结果集的行数--自定义变量declare @age tinyintdeclare @age2 tinyintdeclare @name nvarchar(20)declare @name2 nvarchar(20)set @age = 15 --一次只能对一个量变赋值select @name = '张三',@name2 = '李四' --一次性给多个变量赋值select @age2 = max(age) from T_StuInfo --可以用在查询语句中print @ageprint @age2print @nameprint @name2--###################################################################################--条件表达式declare @age intset @age = 1if (@age <20)begin set @age = @age + 1endelsebegin set @age = @age - 1end--循环declare @index intdeclare @sum intset @index = 1set @sum = 0while (@index < 11)begin set @sum = @sum + @index set @index = @index + 1endprint @sum--批处理SQL语句: 练习---打印三角形,即成批处理语句+go即可,只访问一次数据库declare @row intdeclare @col intdeclare @n int --总行数declare @result nvarchar(2000)set @row = 0set @col = 0set @n = 10 --可以修改n的值set @result = ''while (@row < @n)begin set @col = 0 --复位 set @result = '' while (@col < @n + @row) begin if (@col < @n - @row - 1) begin set @result = @result + ' ' end else begin set @result = @result + '*' end set @col = @col + 1 end print @result set @row = @row + 1endgo--case when--搜索case when(用于一个范围)select '评语' = case when sqlServer>= 90 then '优秀' when sqlServer >= 80 and sqlServer < 90 then '良' when sqlServer >= 60 and sqlServer < 80 then '及格' else '不及格' endfrom T_StuInfo--简单case when(类似swtich,用于一个定值)declare @gender bitset @gender = 'true'print case @gender when 'true' then '男' else '女' end--###################################################################################--事物: 事物的特性ACID(一致性(Consistency)、原子性(Atomicity)、隔离性(Isolation)、持久性(Durability))declare @errorcount intset @errorcount = 0begin transaction --if控制事物的提交begin update T_StuInfo set age = age + 1 where gender = '男' set @errorcount = @@error --@@error无错误返回0,有错误返回非0的数 update T_StuInfo set age = age - 1 where cardno = '女' set @errorcount = @errorcount + @@error if(@errorcount = 0) begin commit end else begin rollback endendbegin transaction --异常控制事物提交,raiserror('XXXXX',1)用于抛出xxxx的异常begin begin try update T_StuInfo set age = age + 1 where gender = '男' update T_StuInfo set age = age - 1 where cardno = '女' commit end try begin catch raiserror('性别字段输入了不合适的字符',1) --1-15级为系统使用 rollback end catchend--###################################################################################--索引: 聚集索引(Clustered Index)或称物理所引,非聚集索引(Nonclustered Index)或称逻辑索引,唯一索引(Unique Index),主键索引(PK Index)--优缺点: 查询快,但增删改慢.--何时用: 数据量特别大的情况适合建索引; 经常查找的字段建索引(聚集索引,此时要求取消主键索引)--注意事项: -- 使用索引时,需要注意查询时的where子句: 若有索引,先查索引,之后再根据索引查找原表记录位置,拼接结果; 若无索引,则不查索引,直接拼结果.-- 如此,针对索引字段,若从带where的查询结果中去掉前5项(不带where),则会出现错误(真分页有类似情况).-- 解决方案: 在子查询中也添加排序字段的永真where条件,如: where sortfIEld > -1select top 20 sortfiled,filed1 from T_S where sortfiled not in (select top 5 sortfiled from T_S where sortfiled > -1) create clustered index IDx_age on T_StuInfo(age) --创建聚集索引(每表仅一份),将对记录排序,而且索引将会和表保存在一起(采用二分查找)create nonclustered index IDx_age on T_StuInfo(age) --创建非聚集索引(任意多个),不排序但会创建独立表(含2列: 原表中的位置索引,已排序的字段) --###################################################################################--视图: 将会创建一张虚拟表,且对视图的insert、delete和update *** 作会修改源数据,但工作中禁止通过视图修改源数据.-- 视图就是个SQL语句,也就是Select结果的虚表,视图相当于虚表的别名而已.-- 注意: 视图的别名的使用.--优点: 代码易读; 经过预编译(存储过程也是预编译的),效率高; 屏蔽了表结构,比较安全性; 缺点: 增加管理开销if exists(select * from sysobjects where [name] = 'V_Snoname' and [type] = 'V') drop vIEw V_Snonamegocreate vIEw V_Snonameas select [sID],sname from T_StuInfogoselect * from V_Snonameselect * from T_StuInfoinsert into V_Snoname(sname) values('候八')--###################################################################################--存储过程(Stored Procedure): sp_help查看SP以及sp参数的信息,sp_helptext查看SP内部代码if exists(select * from sysobjects where [name] = 'P_Triangle' and [type] = 'P') drop procedure P_Trianglegocreate procedure P_Triangle( @n int) with encryption --加密,不影响编译但将无法查看SP内部代码(sp_helptext)as --局部变量 declare @row int declare @col int declare @result nvarchar(2000)begin set @row = 0 set @col = 0 set @result = '' while (@row < @n) begin set @col = 0 --复位 set @result = '' while (@col < @n + @row) begin if (@col < @n - @row - 1) begin set @result = @result + ' ' end else begin set @result = @result + '*' end set @col = @col + 1 end print @result set @row = @row + 1 endendgoexec P_Triangle 10sp_help P_Triangle --查看SP及其参数的信息sp_helptext P_Triangle --查看SP内部代码declare @result int --以下代码证明,SP默认返回值为0set @result = -1 exec @result = P_Triangle 15print @result--存储过程 + 事物 + 输出参数if exists(select * from sysobjects where [name] = 'P_InsertRecord' and [type] = 'P') drop procedure P_InsertRecordgocreate procedure P_InsertRecord( @sname nvarchar(20),@gender nchar(1) = '男',--等号后边是默认值 @age tinyint,@status nchar(2),@birdate datetime,@retrunsID int output --用以保存该记录的主键)as --局部变量begin transactionbegin begin try insert into T_StuInfo(sname,gender,[status],birdate) values(@sname,@gender,@age,@status,@birdate) set @retrunsID = @@IDentity commit return 0 end try begin catch raiserror('插入数据异常',1) rollback return 1 end catchendgodeclare @sID int --保存输出参数declare @return int --保存返回值exec P_InsertRecord '测试2',35,'毕业','1977-06-07',@sID outputexec @return = P_InsertRecord '测试2',@sID output --用@return接受SP返回值print @sIDprint @return--###################################################################################--触发器: 执行时将自动创建inserted或deleted临时表(update,同时创建两表),且均是只读的; 因为无调用痕迹,系统调试时增加困难if exists(select * from sysobjects where [name] = 'TR_DelStu' and [type] = 'TR') drop trigger TR_DelStugocreate trigger TR_DelStu --级联删除on T_StuInfoinstead of delete --(for,after,instead of),注意for和after效果是一样的as declare @currentID intbegin transactionbegin begin try --set @currentID = (select [sID] from deleted) --insert和update会用到临时表inserted select @currentID = [sID] from deleted delete from T_Stuscore where [sID] = @currentID delete from T_StuInfo where [sID] = @currentID commit end try begin catch raiserror('删除失败 *** 作异常',1) rollback end catchenddelete from T_StuInfo where [sID] = 3--###################################################################################--用户定义函数(User defined Function): 标量函数、内嵌表值函数、多语句表值函数--标量函数(Scalar Functions)if exists(select * from sysobjects where [name]='GetCountByGender' and [type] = 'FN') drop function GetCountByGendergocreate function GetCountByGender( @gender nchar(1) --函数的参数列表)returns int --函数的返回值类型asbegin declare @count int --返回值变量 set @count = ( select count([sID]) from T_StuInfo where gender = @gender ) return @count --执行返回endgoselect dbo.GetCountByGender('男') as 数量 --调用函数时,必须加上所有者--内嵌表值函数(Inline table-valued Functions)if exists(select * from sysobjects where [name]='GetInfoByStatus' and [type] = 'IF') drop function GetInfoByStatusgocreate function GetInfoByStatus( @status nchar(2) --参数列表)returns table --返回值为数据表asreturn ( select * from T_StuInfo where [status] = @status)goselect * from dbo.GetInfoByStatus('毕业') --调用函数时,必须加上所有者go--多语句表值函数(Multistatement table-valued Functions)if exists(select * from sysobjects where [name]='GetnameBySegAge' and [type] = 'TF') drop function GetnameBySegAgegocreate function GetnameBySegAge( @firstage int,--18岁 @secondage int,--18-30岁 @thirdage int --30岁以上)returns @infotable table --定义返回值变量(table类型),以及返回值表的字段 ( AgeSegment nvarchar(30),countnum int )asbegin --局部变量,用于填充返回值表 declare @currentcount int --当前年龄段的计数 declare @currentdesc nvarchar(30) --当前年龄段的描述 set @currentcount = (select count([sID]) from T_StuInfo where age<@firstage) set @currentdesc = '小于(含)-'+Convert(nvarchar(10),@firstage)+'岁' insert into @infotable(AgeSegment,countnum) values(@currentdesc,@currentcount) set @currentcount = (select count([sID]) from T_StuInfo where age>=@firstage and age<@secondage) set @currentdesc = Convert(nvarchar(10),@firstage)+'岁(含)-'+Convert(nvarchar(10),@secondage)+'岁' insert into @infotable(AgeSegment,@currentcount) set @currentcount = (select count([sID]) from T_StuInfo where age>=@secondage and age<@thirdage) set @currentdesc = Convert(nvarchar(10),@secondage)+'岁(含)-'+Convert(nvarchar(10),@thirdage)+'岁' insert into @infotable(AgeSegment,@currentcount) set @currentcount = (select count([sID]) from T_StuInfo where age>=@thirdage) set @currentdesc = Convert(nvarchar(10),@thirdage)+'岁(含)-不限' insert into @infotable(AgeSegment,@currentcount) return --执行已定义的返回值表的返回 *** 作endgoselect * from dbo.GetnameBySegAge(20,30,40) --调用函数时,必须加上所有者--###################################################################################--游标:begin transaction MoveUserInfoTransbegin declare @errcount int set @errcount = 0 declare MoveUserInfoTwo cursor --声明游标 for select userID,userpwd from UserInfoTwo open MoveUserInfoTwo --打开游标,准备开始读取 *** 作 declare @userID nvarchar(20),@userpwd nvarchar(30) fetch next from MoveUserInfoTwo into @userID,@userpwd --执行读取 while(@@fetch_status=0) begin insert into UserInfoOne(userID,userpwd) values (@userID,@userpwd) if(@@error!=0) --验证单次 *** 作的是否成功 begin set @errcount = @errcount + 1 break end fetch next from MoveUserInfoTwo into @userID,@userpwd --取下一条 end close MoveUserInfoTwo --完成游标 *** 作,关闭游标 deallocate MoveUserInfoTwo --释放游标 if(@errcount = 0) --用if验证事务的 *** 作过程 begin commit transaction MoveUserInfoTrans print '事务已成功提交!' end else begin rollback transaction MoveUserInfoTrans print '执行过程出错,事务已回滚!' endendgo --###################################################################################/* 缩写: DDL(Database DeFinition Language): 数据库定义语言 DML(Database Manipulation Language): 数据库 *** 作语言 DCL(Database Control Language): 数据库控制语言 DTM(Database Trasaction Management): 数据库事物管理 知识概要: |---1.查询Select | |---2.数据库定义语言DDL: 对表,@userpwd --取下一条 end close MoveUserInfoTwo --完成游标 *** 作,关闭游标 deallocate MoveUserInfoTwo --释放游标 if(@errcount = 0) --用if验证事务的 *** 作过程 begin commit transaction MoveUserInfoTrans print '事务已成功提交!' end else begin rollback transaction MoveUserInfoTrans print '执行过程出错,事务已回滚!' endendgo
尊重原创
另外说明:
@H_502_41@row_number(),rank(),dense_rank(),ntile(num) 区别:
都是 常用于排序后新产生一列序号
例如有一数据表:gradeInfo 表中数据如下
1.row_number():
select gradeID,gradename,row_number() over(order by gradeID asc) a from gradeInfo
结果是:
注意:row_number()不管排序字段的值是否有重复,总是 显示的是不重复,不断续的序号(序号大小根据排序字段的方式)2.rank():
select gradeID,rank() over(order by gradeID asc) newNumber from gradeInfo
结果是:
注意:显示的序号是有重复,并且断续的序号3.dense_rank():
select gradeID,dense_rank() over(order by gradeID asc) newNumber from gradeInfo
结果是:
注意:显示的是重复,不断续的序号4.ntile(num) [num:表示序号的的最大值]
(为了能看的更明白,我们多插入一条数据到gradeInfo);
select gradeID,ntile(3) over(order by gradeID asc) newNumber from gradeInfo 最大值只能排到3
结果:
如果:select gradeID,ntile(4) over(order by gradeID asc) newNumber from gradeInfo 最大值为4
那么结果:
tips:ntile(num),其实是将表中的数据分为了num个组,没一组有多少个数据,至于每一组中数据条数是怎么分配的,请看
排名函数详解
总结以上是内存溢出为你收集整理的SQLServer *** 作详解(没事多翻翻,发觉开发中用到的就是这些)全部内容,希望文章能够帮你解决SQLServer *** 作详解(没事多翻翻,发觉开发中用到的就是这些)所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
微信扫一扫
支付宝扫一扫
评论列表(0条)