T-SQL学习小结

方便自己查询特做小记:


-- 建索引----------------------------------------------------------------------------
--create unique index yxd_student_index on yxd_student(id)
--create unique index yxd_teacher_index on yxd_teacher(id)
--create unique index yxd_course_index on yxd_course(id)

--创建存储过程-----------------------------------------------------------------------
--(1)存储过程允许标准组件式编程,在服务器端运行,执行速度快。
--(2) 存储过程执行一次后,其执行规划就驻留在高速缓冲存储器,在以后的操作中,只需从 高速缓冲存储器中调用已编译好的二进制代码执行,提高了系统性能。
--(3) 确保数据库的安全。使用存储过程可以完成所有数据库操作,并可通过编程方式控制 上述操作对数据库信息访问的权限。
--(4) 自动完成需要预先执行的任务。存储过程可以在系统启动时自动执行,而不必在系统启动后再进行手工操作,大大方便了用户的使用,可以自动完成一些需要预先执行的任务

/*
if exists(select name from sysobjects where name='yxd_student_pro' and type='p')
drop procedure yxd_student_pro
go
create procedure yxd_student_pro
@id int
as
select * from yxd_student s where s.id = @id
go

--调用存储过程-----------------------------------------------------------------------
exec yxd_student_pro @id=1001
*/

/*
if exists(select name from sysobjects where name='yxd_student_total' and type='p')
drop procedure yxd_student_total
go
create procedure yxd_student_total
@name varchar(20),@total float output
as
select @total=sum(ysc.score)
from yxd_student ys,yxd_student_course ysc
where ys.name=@name and ys.id = ysc.student_id
go

declare @num float
exec yxd_student_total '张三',@num output
print @num
go
*/

--所有存储过程的名字都存放在sysobjects表中,源代码放在系统表syscomments中--------------
--select * from sysobjects
--select * from syscomments

--exec sp_help 'yxd_student_total' --用于显示存储过程的参数及其数据类型

/*
---创建触发器-------------------------------------------------------------------------
---触发器是自动的:当对表中的数据作了任何修改(比如手工输入或者应用程序采取的操作)之后立即被激活。
---触发器可以通过数据库中的相关表进行层叠更改。
---触发器可以强制限制,这些限制比用 CHECK 约束所定义的更复杂。
use T_Student
if exists (select name from sysobjects where name='reminder' and type='TR')
drop trigger reminder
go
create trigger reminder on student
for insert
as
declare @id int
select @id=id from inserted
insert into message([message],student_id) values('插入记录',@id)
go
insert into student(name) values('aaa')
*/
---------------------------------------------------------------------------------------
/*
if exists (select name from sysobjects where name='l' and type='TR')
drop trigger l
go
create trigger l on student
for update
as
if update(name)
begin
raiserror('禁止修改学生name',16,10)
rollback transaction
end
--insert into student(name) values('aaa')
go
update student set name = 'cccc' where id=1002
*/


--创建视图---------------------------------------------------------------
/*
create view stuview
as
select id,name from yxd_student
go
exec sp_helptext 'stuview'
*/
--select * from stuview

---创建游标--------------------

use yaoxingda
go
if CURSOR_STATUS('global','cur') <> -3 --状态-3表示游标不存在
begin
close cur
deallocate cur
end
drop trigger updatetr;--删除在update操作上建立的触发器(祸害啊!!!)
declare cur cursor scroll dynamic --声明动态游标
for select id,name from yxd_student
FOR UPDATE

go
declare @id int,@name char(20)
open cur
fetch first from cur into @id,@name
while @@FETCH_STATUS=0
begin
if @name='李四'
begin
begin transaction
print '更新中....'
update yxd_student set age=age+1 where CURRENT OF cur
print '更新完毕~'
commit;
end
else
begin
print '继续找'
end
fetch next from cur into @id,@name
end
go
select * from yxd_student;
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值