SQLSERVER--一条SQL语句

select语句
语法:select distinct | top 数字 [percent] 字段1 as 别名 ,包含字段表达式,函数,常量
from 表或结果集
where 逻辑条件 | 模糊处理 | 范围处理 | null值处理
group by 分组字段
having 筛选条件
order by 排序依据; 执行流程:
from子句 -> where子句 ->group by子句 ->having子句 ->select子句 ->order by子句
-- 名字
-- 作用(例子)
-- 语法
-- 子查询
-- 就是在一个查询中嵌套一个查询
-- 一般作用就是利用多张表查询一个信息
-- 例如查询"濮阳语儿"的成绩
select * from TestDataBase..Student;
select * from TestDataBase..Score;
-- 在学生表中查得stuId,然后再到分数表中查询分数
select stuId from TestDataBase..Student where stuName ='濮阳语儿';
select * from TestDataBase..Score where stuId = 5723;
--
-- 外部查询
select *
from TestDataBase..Score
where stuId in
( -- 子查询、内部查询
select stuId from TestDataBase..Student where stuName ='濮阳语儿'
);
-- 外部查询(子查询)-- 将一个查询的结果作为另一个查询的条件-- 考试成绩与课程查出来 Course
select * from TestDataBase..Course;select className from TestDataBase..Course where classId in
(
select top 1 classId from TestDataBase..Student where stuName='濮阳语儿'
);

-- 多个单值 外部查询 where 字段 in (子查询)
select '濮阳语儿' , (select className from TestDataBase..Course where classId in
(
select top 1 classId from TestDataBase..Student where stuName='濮阳语儿'
));

-- 表值 select * from (子查询) as 别名
select * from (
select stuName, case stuSex when 'f' then '女' else '男' end as stuSex, DATEDIFF(YEAR, stuBirthdate, GETDATE()) as stuAge from TestDataBase..Student where stuId <= 10
) as t
where t.stuAge between 20 and 30;
--
-- 员工编号 基本工资 请假扣款 补贴 绩效奖金 项目奖金 社保扣款
/*
select
来自员工表的查询
, 来自工资级别表的查询
, 考勤表的查询
... ...
*/-- 独立子查询(标量、多值)-- 相关子查询
-- 查询濮阳语儿的三科平均分
select AVG(testBase), AVG(testBeyond), AVG(testPro) from TestDataBase..Score where stuId = (select top 1 stuId from TestDataBase..Student where stuName='濮阳语儿');select
stuName
, (select AVG(TestBase) from TestDataBase..Score where stuId = t.stuId) as 基础平均分
, (select AVG(testBeyond) from TestDataBase..Score where stuId = t.stuId) as 中级平均分
, (select AVG(testPro) from TestDataBase..Score where stuId = t.stuId) as 高级平均分
from
TestDataBase..Student as t
where
stuName = '濮阳语儿';----------------------------------------
use HeiMa8;create table Score
(
学号 nvarchar(10),
课程 nvarchar(10),
成绩 int
)insert into Score values('0001','语文',87);
insert into Score values('0001','数学',79);
insert into Score values('0001','英语',95);
insert into Score values('0002','语文',69);
insert into Score values('0002','数学',84);case表达式
--if-else结构
case
when 条件1 then 值1
when 条件2 then 值2
。。。
else 值n
end --switch-case结构
case 字段
when 匹配1 then 值1
when 匹配2 then 值2
。。。
else 值n
end
Set statistics io on;--打开监视磁盘IO操作
Set statistics time on;select * from Score;
-- 分组
select 学号, '语文', '数学', '英语' from Score group by 学号;
--第一种结构示例:switch--case
select
学号
, case when 课程='语文' then 成绩 else 0 end as '语文'
, case when 课程='数学' then 成绩 else 0 end as '数学'
, case when 课程='英语' then 成绩 else 0 end as '英语'
from Score
--
select
学号
, sum(case when 课程='语文' then 成绩 else 0 end) as '语文'
, sum(case when 课程='数学' then 成绩 else 0 end) as '数学'
, sum(case when 课程='英语' then 成绩 else 0 end) as '英语'
from Score
group by 学号;第二种结构示例:if--else
select
sum(case when T.充值金额>=500 then T.充值金额 end) as '鲸鱼用户'
,sum(case when T.充值金额>=100 and T.充值金额<500 then T.充值金额 end) as '海豚用户'
,sum(case when T.充值金额>=10 and T.充值金额<100 then T.充值金额 end) as '小鱼用户'
from
(
select [ChannelUserKey] as 用户ID,sum(convert(float,[RechargeAmount])/100) as 充值金额,sum([RechargeCount]) as 充值用户
from [dbo].[FactRecharge]
where datekey>=20141201 and datekey<=20141210
and ChannelKey=1
group by [ChannelUserKey]
) T
-------------
-- 透视变换

select * from Score pivot(
sum(成绩) for 课程 in (语文,数学,英语)
) as t------------------------ 表连接
-- 作用:将多张表变成一张表
-- 用法与分类(案例)
-- 分类:交叉连接、内连接、外连接create table joinPerson
(
pId int identity(1,1) not null
, pName nvarchar(10) not null
, titleId int null
);
alter table joinPerson
add constraint PK_joinPerson_pId primary key(pId);create table joinTitle
(
titleId int identity(1,1) not null
, titleName varchar(10) not null
);
alter table joinTitle
add constraint PK_joinTitle_titleId primary key(titleId);insert into joinTitle(titleName) values('Teacher'),('Master');
insert into joinPerson(pName, titleId) values('牛亮亮', 1),('苏坤', 2),('杨中科', NULL);select * from joinPerson;
select * from joinTitle;select pName, titleName from joinPerson cross join joinTitle;
-- 如果两章表中有重名的字段,就会出问题,就需要给表加别名
select t1.pName, t2.titleName from joinPerson as t1 cross join joinTitle as t2;-- 内连接
select
*
from
joinPerson as t1
inner join
joinTitle as t2
on t1.titleId = t2.titleId;

-- 左外连接
select
*
from
joinPerson as t1
left join
joinTitle as t2
on t1.titleId = t2.titleId; -- 右外连接
insert into joinTitle(titleName) values('班主任');select
*
from
joinPerson as t1
right join
joinTitle as t2
on t1.titleId = t2.titleId;

-- 全连接
select
*
from
joinPerson as t1
full join
joinTitle as t2
on t1.titleId = t2.titleId;

-- 表表达式
-- 就是通过表与表的运算,得到一个结果集作为from后面的数据源
-- 1、派生表 返回结果集的子查询
-- 语法: select ... from (select 查询) as 别名;
-- 注意: 不能使用游标
-- 2、公用表表达式CTE
-- 3、视图
-- 4、内联表值函数-- 查询学生信息
select * from
TestDataBase..Student as t1
inner join
TestDataBase..Course as t2
on t1.classId = t2.classId
inner join
TestDataBase..Score as t3
on t1.stuId = t3.stuId
where
stuName = '濮阳语儿';

----------------------------
select * from
(
select
t1.stuId
, t1.stuName
, case t1.stuSex when 'f' then '女' else '男' end as stuSex
, datediff(year, t1.stuBirthdate, GETDATE()) as stuAge
, t1.stuEmail
, t1.stuAddress
, t1.stuPhone
, t2.className
, t3.testBase
, t3.testBeyond
, t3.testPro
from
TestDataBase..Student as t1
inner join
TestDataBase..Course as t2
on t1.classId = t2.classId
inner join
TestDataBase..Score as t3
on t1.stuId = t3.stuId
) as t
where t.stuName = '濮阳语儿';------------------------------------
-- 分页
------------------------------------
select * from TestDataBase..Student;
-- 当前页数、每页显示的条数 10
-- SQL Server 2005+ row_number() 可以为表生成一个连续的数字列
-- 语法 row_number() over(order by 字段)
-- select ROW_NUMBER() over(order by stuId), * from TestDataBase..Studentselect top 10 * from TestDataBase..Student;select top 10 * from TestDataBase..Student
where stuId not in(select top 10 stuId from TestDataBase..Student);set statistics io on;
set statistics time on;select top 10 * from TestDataBase..Student
where stuId not in(select top ((100-1)*10) stuId from TestDataBase..Student);--
select * from
(
select ROW_NUMBER() over(order by stuId) as num, * from TestDataBase..Student
) as t
where
t.num between 1 and 10;select * from
(
select ROW_NUMBER() over(order by stuId) as num, * from TestDataBase..Student
) as t
where
t.num between 21 and 30;
/*
select * from
(
select ROW_NUMBER() over(order by stuId) as num, * from TestDataBase..Student
) as t
where
t.num between (n-1) * m + 1 and n * m;
*/-- 公用表表达式(CTE)
-- 语法
/*
with 别名
as
(
结果集
)
使用别名的一个查询;
*/
with t
as
(
select
t1.stuId
, t1.stuName
, case t1.stuSex when 'f' then '女' else '男' end as stuSex
, datediff(year, t1.stuBirthdate, GETDATE()) as stuAge
, t1.stuEmail
, t1.stuAddress
, t1.stuPhone
, t2.className
, t3.testBase
, t3.testBeyond
, t3.testPro
from
TestDataBase..Student as t1
inner join
TestDataBase..Course as t2
on t1.classId = t2.classId
inner join
TestDataBase..Score as t3
on t1.stuId = t3.stuId
)
select * from t where t.stuName = '濮阳语儿';-- t可以重用
-- 自交差
-- 生成一个数字表select num from (values(0),(1),(2),(3),(4),(5),(6),(7),(8),(9)) as t(num);-- 自交差 10000
select
t1.num * 10 + t2.num + 1
from
(select num from (values(0),(1),(2),(3),(4),(5),(6),(7),(8),(9)) as t(num)) as t1
cross join
(select num from (values(0),(1),(2),(3),(4),(5),(6),(7),(8),(9)) as t(num)) as t2
;
-- 用公用表表达式
with t
as
(
select num from (values(0),(1),(2),(3),(4),(5),(6),(7),(8),(9)) as tt(num)
)
select
t1.num * 1000 + t2.num * 100 + t3.num * 10 + t4.num + 1 as orderId
from
t as t1
cross join
t as t2
cross join
t as t3
cross join
t as t4
order by
orderId;select * from HeiMa8..AreaFull as t1 inner join HeiMa8..AreaFull as t2 on t1.AreaPid = t2.AreaId ;--
-- 在需要频繁的操作一些表表达式的时候
-- 视图和内联表值函数-- 视图
-- 就是将查询的语句封装成一个对象,每次查询的时候直接操作这个对象即可
-- 虚拟表
-- 使用派生表
select * from
(
select
t1.stuId
, t1.stuName
, case t1.stuSex when 'f' then '女' else '男' end as stuSex
, datediff(year, t1.stuBirthdate, GETDATE()) as stuAge
, t1.stuEmail
, t1.stuAddress
, t1.stuPhone
, t2.className
, t3.testBase
, t3.testBeyond
, t3.testPro
from
TestDataBase..Student as t1
inner join
TestDataBase..Course as t2
on t1.classId = t2.classId
inner join
TestDataBase..Score as t3
on t1.stuId = t3.stuId
) as t
where t.stuName = '濮阳语儿';-- 创建视图
-- 语法:
/*
create view vw_视图名
as
select语句
;
*/use TestDataBase;
go
create view vw_StuInfo
as
select
ROW_NUMBER() over(order by t1.stuId) as n
, t1.stuId
, t1.stuName
, case t1.stuSex when 'f' then '女' else '男' end as stuSex
, datediff(year, t1.stuBirthdate, GETDATE()) as stuAge
, t1.stuEmail
, t1.stuAddress
, t1.stuPhone
, t2.className
, t3.testBase
, t3.testBeyond
, t3.testPro
from
TestDataBase..Student as t1
inner join
TestDataBase..Course as t2
on t1.classId = t2.classId
inner join
TestDataBase..Score as t3
on t1.stuId = t3.stuId
;
go-- 虚拟的表
select * from vw_StuInfo where stuName='濮阳语儿';select * from vw_StuInfo where stuId = 304;update Testdatabase..Student set stuName = '嘉嘉' where stuId=304;--
-- 视图可以更新数据,但是不建议更新和增加以及删除
-- 连接多张表、视图并没有显式所有的字段--
-- 视图的一个主要作用(数据安全)
use HeiMa8;
go
create view Exe3.vw_StuInfo
as
select * from TestDataBase..vw_StuInfo;
go-- HeiMa8
select * from Exe3.vw_StuInfo;-- select * from sys.databases;-- 内联表值函数
-- 带有参数的视图
-- 作用: 将一个可变条件的查询封装成一个函数对象,执行结果是一张表
/*
create function fn_函数名
(@参数名 as 类型, ...)
returns table
as
return 查询语句;
*/
-- 分页
-- @pageIndex 当前页码
-- @pageSize 每页条数
use TestDataBase;
gocreate function fn_FenYe
(@pageSize as int, @pageIndex as int)
returns table
as
return
select * from
(
select ROW_NUMBER() over(order by stuId) as num, * from Student
) as t
where t.num between (@pageIndex-1) * @pageSize + 1 and @pageIndex * @pageSize;
go
-- 分页-- 1008
select * from fn_FenYe(10, 1008);
-- 视图怎么提供权限,怎么安全
-- 角色 访问能力的集合
-- 架构 可访问对象的集合
-- 视图 表的一个结果集 -- 变量
-- int num = 10;
-- 声明 赋值 使用
-- declare @变量名 类型;
-- set @变量名 = 值;declare @num varchar(2);
set @num = '10';select @num + 'a';
go-- SQL Server 2008+
declare @num int = 10;
-- 局部变量
-- 系统变量 @@开头select @@connections
select @@CPU_BUSY;-- @@error 最近一次执行SQL语句的错误码
select @@ERROR;
-- @@version
select @@version
print @@version;-- @@identity 最近一次插入数据的自动增长编号
select @@IDENTITY;
use HeiMa8 ;
select * from Exe3.newStudent where stuId = (select @@IDENTITY);insert into Exe3.newStudent
(stuName, stuSex, stuBirthdate, stuStudydate, stuAddress, stuEmail, stuPhone, classId)
values('牛亮亮', 'm', '1999-9-9 9:9:9.123', '2001-1-1 1:1:1.111','123', '123', '12345678909', 2);insert into Exe3.newStudent(stuName, stuSex, stuBirthdate, stuStudydate, stuAddress, stuEmail, stuPhone, classId)
output inserted.*
values('刘琦', 'm', '1999-9-9 9:9:9.123', '2001-1-1 1:1:1.111','123', '123', '12345678909', 2);
-- @@trancount
select @@TRANCOUNT -- 获得当前事务的深度-- @@SPID
select @@SPID; -- session_id
kill 54-- set
-- select
-- select @变量=值
godeclare @num int;
select @num = 10;
select @num;
goselect top 3 * from Exe3.newStudentdeclare @name nvarchar(1000) = '';
-- set @name = (select top 1 stuName from Exe3.newStudent);
select @name+=stuName from Exe3.newStudent where stuId<10;
select @name;select @@ERROR;select * from sys.messages where message_id = 208;
-- 流程控制(C#的编程)
-- 选择结构 if-else
-- 循环结构 while
/*
if (bool表达式)
begin -- {
脚本
end -- }
else if bool表达式
begin
-- 脚本
end
*/declare @num int;
--set @num = 12;if @num is null
select '是NULL';
else if @num % 2 = 0
begin
select '是偶数';
end
else
begin
select '是奇数';
end
go
-- 循环结构
/*
while bool表达式
begin
脚本
end
*/
declare @sum int;-- = 0;
declare @i int = 0;while @i <= 100
begin
set @sum += @i;
set @i += 1; -- 没有 @i++ 的语法
end
select @sum;-- 注意set   ------------------
-- 事务
------------------
-- 最基本的操作以事务为单位
-- 将一个已经完成的数据库操作行为规定为一个事务
-- 特点:
-- 原子性——执行的结果是01特征(要么完成、要么失败)
-- 持久性——执行的结果不可逆转
-- 一致性——一旦完成事务,各个版本的结果都一样
-- 隔离性——事务与事务之间不冲突-- 事务严格定义: 找一个操作,如果满足原子性、持久性、一致性和隔离性就称为一个事务select * from Exe2.LoginTbl;insert into Exe2.LoginTbl(uid, pwd)
values
('苏坤', 'susu123')
, ('尼古拉斯洛夫斯基斯巴达司机四司机司机司机司机', '123')
, ('牛亮亮', 'niuniu123');

------------------------
-- 自己写的事务
--事务
create table bank
(
cId char(4) primary key,
balance money, --余额
)alter table bank
add constraint CH_balance check(balance >=10)go
--delete from bank
insert into bank values('0001',1000)
insert into bank values('0002',10)
goselect * from bank-- 0001 -> 0002 1000元-- 默认的事务方式——隐式事务
update bank set balance=balance - 1000 where cid='0001';
update bank set balance=balance + 1000 where cid='0002';
-- 手动执行事务
-- 开启事务 -> 执行语句 -> 判断满足与否 -> 提交或回滚
-- 语法
-- 开启 begin transaction
-- 提交 commit
-- 回滚 rollbackselect @@TRANCOUNT;begin transaction delete from TestDataBase..Score;
delete from TestDataBase..Student;
select * from HeiMa8.Exe3.vw_StuInfo;
rollback--
select * from bank;begin tran
declare @myError int;
update bank set balance=balance - 900 where cid='0001';
set @myError = @@ERROR;
update bank set balance=balance + 900 where cid='0002';
set @myError += @@ERROR;
if @myError > 0
rollback
else
commit
;-- try-catch
--begin try
--end try
--begin catch
--end catchbegin tran
begin try
update bank set balance=balance - 1000 where cid='0001';
update bank set balance=balance + 1000 where cid='0002';
commit;
end try
begin catch
rollback;
end catch
;-- 怎么知道成功没有? -- 使用变量
-- 怎么使用事务? -- 存储过程
declare @isSuccess bit;
begin tran
begin try
update bank set balance=balance - 900 where cid='0001';
update bank set balance=balance + 900 where cid='0002';
commit;
set @isSuccess = 'true';
end try
begin catch
rollback;
set @isSuccess = 'false';
end catch
select @isSuccess;
; -- 存储过程
-- 简单的看成数据库中的方法
-- 函数、视图、存储过程-- 就是一段执行代码
-- 系统中常用的存储过程 sp_ stored procedure
--
--sp_renamedb -- 修改数据库的名字
--sp_detach_db -- 分离
-- sp_attach_db -- 附加--sp_executesql-- sp_renamedb-- exec 存储过程名 参数;
exec sp_renamedb 'MyThirdDataBase', 'my3thDataBase';exec sp_renamedb @dbname='my3thDataBase', @newname='我的第三个数据库';-- sp_executesql
exec sp_executesql N'select @@version'; -- unicode编码
exec('select ''张三'', ''李四''');
-- 存储过程的语法
/*
create proc[edure] usp_存储过程名
参数名 类型名 [= 默认值] [output]
, 参数名 类型名 [= 默认值] [output]
, ...
as
begin
脚本
end
*/
-- 无参无返回值的存储过程
go
create proc Exe3.usp_StuInfo
as
select * from vw_StuInfo;
go
exec Exe3.usp_StuInfo;
go-- 有参有默认值的存储过程
-- 带有参数的
create proc Exe3.usp_StuSearch
@stuName nvarchar(10)
as
select * from Exe3.vw_StuInfo where stuName = @stuName;
goexec Exe3.usp_StuSearch @stuName='濮阳语儿';exec Exe3.usp_StuSearch '濮阳语儿';-- 带有默认值的存储过程
-- 分页
gocreate proc Exe3.usp_FenYe
@pageIndex int = 1
, @pageSize int = 10
as
begin
select '今天天气很好';
select * from
(
select ROW_NUMBER() over(order by stuId) as num
, * from Exe3.newStudent
)as t
where t.num between (@pageIndex - 1) * @pageSize + 1 and @pageIndex * @pageSize;
end
goexec Exe3.usp_FenYe 2, 5;exec Exe3.usp_FenYe @pageSize=11, @pageIndex=3;-- 有参有返回值的存储过程
-- return outputgo
-- return 返回值
create proc Exe3.usp_ReturnTest
as
return 123;
go/*
public string Func()
{
return "赵晓虎就是牛,你让牛亮亮怎么办?";
}
*/
declare @num int;
exec @num = Exe3.usp_ReturnTest;select @num;
go
-- 银行转账的案例
--
create proc Exe3.usp_ZhuanZhang
@from char(4)
, @to char(4)
, @money money
as
begin
begin tran
begin try
update bank set balance=balance - @money where cid=@from;
update bank set balance=balance + @money where cid=@to;
commit;
end try
begin catch
rollback;
end catch
end
go--
select * from bank;exec Exe3.usp_ZhuanZhang '0002', '0001', 900;go
-- 考虑返回值
create proc Exe3.usp_ZhuanZhangExt
@from char(4)
, @to char(4)
, @money money
, @isSuccess int output -- 表示需要在存储过程中赋值,传出去
as
begin
begin tran
begin try
update bank set balance=balance - @money where cid=@from;
update bank set balance=balance + @money where cid=@to;
commit;
set @isSuccess = 1;
end try
begin catch
rollback;
set @isSuccess = 0;
end catch
end
go-- 关键使用法
-- 定义一个变量,不赋值,调用存储过程,将参数传入 后跟outputdeclare @isSuccess int;-- exec Exe3.usp_ZhuanZhangExt '0001', '0002', 500, @isSuccess output;
exec Exe3.usp_ZhuanZhangExt
@from = '0001',
@to = '0002',
@money = -500,
@isSuccess = @isSuccess output;select @isSuccess;-- 注意,不要将变量名命名为与存储过程的参数一致
gocreate proc Exe2.usp_Login
@uid nvarchar(20)
, @pwd varchar(20)
, @isLogin int output
as
select @isLogin=COUNT(*) from Exe2.LoginTbl
where uid=@uid and pwd=@pwd;
go--
select * from Exe2.LoginTbl;declare @isTrue int;exec Exe2.usp_Login '苏坤1', '1234', @isTrue output;select @isTrue;-- 用C#执行存储过程
-- 步骤
-- -> 将sql语句改为存储过程名
-- -> 修改CommandType命令(text)
-- -> 看返回结果调用指定方法
-- -> 如果有参数,与参数化查询用法一样
-- -> 如果有返回值,设置参数方向即可(难度)exec Exe3.usp_FenYe;-- 触发器
-- 在你执行一个操作的时候,自动的执行的一个存储过程-- DML DDL
-- 对行为的分类 update、delete、insert
-- 发生方式 after | instead of-- 语法
/*
create trigger tr_in|del|up_触发器的名字 on 表名
for | after | instead of
update | delete | insert
as
begin
脚本
end
*/-- inserted deletedselect * from inserted;--
select * from Exe2.LoginTbl;
go
create trigger Exe2.tr_del_deleteReturn on Exe2.loginTbl
after delete
as
insert into Exe2.LoginTbl(uid, pwd)
select uid, PWD from deleted;
godelete from Exe2.LoginTbl;-- 作为数据验证的补充-- -- 索引就是数据的目录
-- 新华字典
-- 拼音(聚集索引) she 与正文一致
-- 部首(非聚集索引) 厍 相当于存储的一个键值对表-- 字 拼音 意思 词组。。。
-- 如何添加索引set statistics io on;
set statistics time on;select * from Exe3.newStudent where stuName='苍昊天';
/*
SQL Server 分析和编译时间:
CPU 时间 = 0 毫秒,占用时间 = 0 毫秒。
SQL Server 分析和编译时间:
CPU 时间 = 0 毫秒,占用时间 = 20 毫秒。(2 行受影响)
表 'newStudent'。扫描计数 1,逻辑读取 2264 次,物理读取 0 次,预读 0 次,lob 逻辑读取 0 次,lob 物理读取 0 次,lob 预读 0 次。 SQL Server 执行时间:
CPU 时间 = 16 毫秒,占用时间 = 31 毫秒。
--------------
SQL Server 分析和编译时间:
CPU 时间 = 0 毫秒,占用时间 = 0 毫秒。
SQL Server 分析和编译时间:
CPU 时间 = 0 毫秒,占用时间 = 0 毫秒。
SQL Server 分析和编译时间:
CPU 时间 = 0 毫秒,占用时间 = 0 毫秒。(2 行受影响)
表 'newStudent'。扫描计数 1,逻辑读取 5 次,物理读取 0 次,预读 0 次,lob 逻辑读取 0 次,lob 物理读取 0 次,lob 预读 0 次。 SQL Server 执行时间:
CPU 时间 = 15 毫秒,占用时间 = 24 毫秒。
*/-- 索引不一定好create unique nonclustered index ix_索引名字 on 表
(字段 desc, 字段 desc)

相关内容推荐