15个常用的SQL Server高级语法
作者:网络转载 发布时间:[ 2015/8/28 11:27:01 ] 推荐标签:数据库
自从用了EF后很少写sql和存储过程了,需要写个比较复杂的报告,翻出了之前的笔记做参考,感觉这个笔记还是很有用的,因此发出来和大家分享。
1、case…end (具体的值)
case后面有值,相当于c#中的switch case
注意:case后必须有条件,并且when后面必须是值不能为条件。
-----------------case--end---语法结构---------------------
select name , --注意逗号
case level --case后跟条件
when 1 then '骨灰'
when 2 then '大虾'
when 3 then'菜鸟'
end as'头衔'
from [user]
2、case…end (范围)
case 后面无值,相当于c#中的if…else if…else….
注意:case后不根条件
------------------case---end--------------------------------
select studentId,
case
when english between 80 and 90 then '优'
when english between 60 and 79 then '良'
else '差'
end
from Score
------------------case---end--------------------------------
select studentId,
case
when english >=80 then '优'
when english >=60 then '良'
else '差'
end
from Score
-----------------------------------------------------
select *,
case
when english>=60 and math >=60 then '及格'
else '不及格'
end
from Score
3、if…eles
IF(条件表达式)
BEGIN --相当于C#里的{
语句1
……
END --相当于C#里的}
ELSE
BEGIN
语句1
……
END
--计算平均分数并输出,如果平均分数超过分输出成绩高的三个学生的成绩,否则输出后三名的学生
declare @avg int --定义变量
select @avg= AVG(english) from Score --为变量赋值
select '平均成绩'+CONVERT(varchar,@avg) --打印变量的值
if @avg<60
begin
select '前三名'
select top 3 * from Score order by english desc
end
else
begin
select '后三名'
select top 3 * from Score order by english
end
4、while循环
WHILE(条件表达式)
BEGIN --相当于C#里的{
语句
……
BREAK
END --相当于C#里的}
--如果不及格的人超过半数(考试题出难了),则给每个人增加分
select * from Score
declare @conut int,@failcount int,@i int=0 --定义变量
select @conut =COUNT(*) from Score --统计总人数
select @failcount =COUNT(*) from Score where english<100 --统计未及格的人数
while (@failcount>@conut/2)
begin
update Score set english=english+1
select @failcount=COUNT(*) from Score where english<100
set @i=@i+1
end
select @i
update Score set english=100 where english >100
5、索引
使用索引能提高查询效率,但是索引也是占据空间的,而且添加、更新、删除数据的时候也需要同步更新索引,因此会降低Insert、Update、Delete的速度。只在经常检索的字段上(Where)创建索引。
1)聚集索引:索引目录中的和目录中对应的数据都是有顺序的。
2)非聚集索引:索引目录有顺序但存储的数据是没有顺序的。
--创建非聚集索引
CREATE NONCLUSTERED INDEX [IX_Student_sNo] ON student
(
[sNo] ASC
)
6、子查询
将一个查询语句做为一个结果集供其他SQL语句使用,像使用普通的表一样,被当作结果集的查询语句被称为子查询。所有可以使用表的地方几乎都可以使用子查询来代替。
select * from (select * from student where sAge<30) as t --被查询的子表必须有别名
where t.sSex ='男' --对子表中的列筛选
转换为两位小数:CONVERT(numeric(10,2), AVG(english))
只有返回且仅返回一行、一列数据的子查询才能当成单值子查询。
select '平均成绩', (select AVG(english) from Score) --可以成功执行
select '姓名', (select sName from student) --错误,因为‘姓名’只有一行,而子表中姓名有多行
select * from student where sClassId in(select cid from Class where cName IN('高一一班','高二一班')) --子查询有多值时使用in
7、分页
--分页1
select top 3 * from student
where [sId] not in (select top (3*(4-1)) [sid] from student)--4表示页数
select *, row_number() over(order by [sage] desc ) from student-- row_number() over (order by..)获取行号
--分页2
select * from
(select *, row_number() over(order by [sid] desc ) as num from student)as t
where num between (Y-1)*T+1 and Y*T
order by [sid] desc
--分页3
select * from
(select ROW_NUMBER() over( order by [UnitPrice] asc) as num,* from [Books] where [publisherid]=1 )as t
where t.num between 1 and 20 --要查询的开始条数和结束条数
8、连接
select sName,sAge,
case
when english <60 then '不及格'
when english IS null then '缺考'
else CONVERT(nvarchar, english)
end as'英语成绩'
from student as s
left join Score as c on s.sid =c.sid
内连接 inner join...on...
查询满足on后面条件的数据
外连接
左连接
left join...on...
先查出左表中的所有数据
再使用on后面的条件对数据过滤
右连接
right join...on...
先查出右表中的所有数据
再使用on后面的条件对数据过滤
全连接
full join ...on...
(*)交叉连接
cross join 没有on
第一个表的每一行和后面表的每一行进行连接
没有条件。是其它连接的基础

sales@spasvo.com