set implicit_transactions off --off的时候,无法手动回滚,可能是因为数据库自动提交了
update stu2 set name='康小分' where id=1
select * from stu2
commit
set implicit_transactions on --on的时候,可以手动回滚,可能是因为数据库还没提交了
update stu2 set name='英语' where id=1
commit
select * from stu2;
--设置数据库隔离级别
set transaction isolation level read committed
------查询当前的隔离级别:read commited:解决了脏读
DBCC USEROPTIONS
set implicit_transactions on
select * from stu2;
commit
----------------------------存储过程----------------------------
存储过程相当于java中的方法:有系统提供的,也可以自己创建
--以下为系统提供的(系统提供的一般是sp开头,sp是system procedure的简写):
--exec是execute的简写,意思是执行,后面是要执行的存储过程
exec sp_databases --查询本机有多少个数据库,分别是什么数据库
exec sp_helpdb --查看某个表的所有信息
exec sp_renamedb wx1001 ,wx101 --修改数据库wx1001的名字为wx101
exec sp_tables --查询当前数据库里面存储的所有表,包含系统的和我们自己创建的
exec sp_help account --查询account表的信息
exec sp_columns account --查询account表的列的信息
exec sp_helptext sp_databases --查看存储过程的源代码
--自己创建存储过程
create proc mySelect1 as
begin
select * from stuInfo2
end
--执行存储过程
exec mySelect1
--需求:创建一个存储过程,需要查询stuInfo2中的所有成绩都及格的男生和女生各自的个数
create proc usp_tongji as begin
select gender,COUNT(*) from stuInfo2 where english>=60 and chinese>=60 and java>=60 group by gender
end
exec usp_tongji
drop proc mySelect1 --删除名字是mySelect1的存储过程
--修改存储过程
alter proc usp_tongji as begin
select gender,COUNT(*) from stuInfo2 where english>=60 and chinese>=60 and java>=60 group by gender
end
--创建一个带参数的存储过程
create proc up_test1 @name nvarchar(50) as begin eg:--public void up_test1(String name)
select * from stuInfo2 where name=@name
end
--执行带参数的存储过程
exec up_test1 '吕令凡'
--练习:定义个存储过程,专门用来统计java分数在一定范围的同学(这个范围不一定,需要调用者指定)
create proc usp_fanwei @start int , @end int as begin
select * from stuInfo2 where java between @start and @end
end
exec usp_fanwei 60,80
--参数设置默认值
create proc usp_fanwei2 @start int = 50 , @end int=80 as begin
select * from stuInfo2 where java between @start and @end
end
exec usp_fanwei2
--参数设置默认值(有的参数有默认值,有的没有)
create proc usp_fanwei3 @start int , @end int=80 as begin
select * from stuInfo2 where java between @start and @end
end
exec usp_fanwei3 70
create proc usp_fanwei4 @start int=70 , @end int as begin
select * from stuInfo2 where java between @start and @end
end
exec usp_fanwei4 @end=90 --指定传的参数是给哪个形参的
--总结:当传递的参数不明确是哪个的时候,可以手动指明是给哪个形参赋值
Nb:--存储过程的输出参数:相当于java方法中的返回值,有点不同,java中是把新变量返回,而这里是给一个已经存在的变量赋值
create proc zy1 @r int output as begin
set @r=10
end
declare @a int
exec zy1 @a output
print @a
--分页查询第4页的数据,每页3条
select * from (select *,序号=ROW_NUMBER() over(order by java desc) from stuInfo2) as t where t.序号 between 3*(4-1)+1 and 3*4
--分页查询第5页的数据,每页3条
select * from (select *,序号=ROW_NUMBER() over(order by java desc) from stuInfo2) as t where t.序号 between 3*(5-1)+1 and 3*5
--分页查询第n页的数据,每页3条
declare @n int=4
select * from (select *,序号=ROW_NUMBER() over(order by java desc) from stuInfo2) as t where t.序号 between 3*(@n-1)+1 and 3*@n
--创建一个分页的存储过程,要求调用这个存储过程的时候,把要查询的页码传给存储过程(掌握)
create proc fenye @num int ,@page int ,@allNum int output, @allPage int output as begin
select * from (select *,序号=ROW_NUMBER() over(order by java desc) from stuInfo2) as t where t.序号 between @num*(@page-1)+1 and @num*@page
set @allNum = (select COUNT(*) from stuInfo2)
set @allPage = CEILING(@allNum*1.0/@num)
end
declare @a int,@b int --a赋给了@allNum b赋给了@allPage
exec fenye 3,2,@a output ,@b output
print '共'+convert(varchar(100),@a)+'个职位'
print '<'+convert(varchar(100),2) +'/'+convert(varchar(100),@b)+'>'
select * from stuInfo2
问题一:--模拟银行转账,要求每个人的银行卡余额不得低于10元 (不考虑事务)(掌握)
create proc zhuanzhang @from int ,@to int, @balance money,@result int output as begin
declare @money money
select @money = money from account where id=@from
if(@money-@balance>=10)begin
update account set money=money+@balance where id=@to
update account set money=money-@balance where id=@from
set @result=1
end else begin
set @result=3
end
end
select * from account;
declare @r int
exec zhuanzhang 5,2,100,@r output
print @r
作业:
问题二:--模拟银行转账,要求每个人的银行卡余额不得低于10元 (考虑事务) --作为任务