sql

--查询学生
if(OBJECT_ID('proc_GetStudent','P')is not null)
drop proc proc_GetStudent
go
create proc proc_GetStudent
as
select * from Student
go
exec proc_GetStudent
--查询课程
if(OBJECT_ID('proc_GetCourse','P')is not null)
drop proc proc_GetCourse
go
create proc proc_GetCourse
as
select * from Course
go
exec proc_GetCourse
--分页排序
if(OBJECT_ID('proc_pageByOrder','P')is not null)
drop proc proc_GetCourse
go
create proc proc_pageByOrder
(
@pageIndex int,
@pageSize int
)
as
declare @startRow int,@endRow int
set @startRow=(@pageIndex-1)*@pageSize+1
set @endRow=@startRow+@pageSize-1
if @pageIndex=0
select st.Student_id,st.Student_name,s.Score_Grade,c.Course_name from Score s join Student st on s.Score_student_id=st.Student_id
join Course c on s.Score_course_id=c.Course_id where 1=1
else
select * from(select st.Student_id,st.Student_name,s.Score_Grade,c.Course_name,ROW_NUMBER() over(order by s.Score_Grade desc)rowIndex from Score s join Student st on
s.Score_student_id=st.Student_id join Course c on c.Course_id=s.Score_course_id) T
where T.rowIndex between @startRow and @endRow
go
exec proc_pageByOrder 1,2
--添加
if(OBJECT_ID('proc_AddScore','P')is not null)
drop proc proc_AddScore
go
create proc proc_AddScore
(
@Score_course_id int,
@Score_student_id int,
@Score_Grade int
)
as
insert Score(Score_course_id,Score_student_id,Score_Grade) values(@Score_course_id,@Score_student_id,@Score_Grade)
go
exec proc_AddScore 1,2,99
select * from Student
select * from Score
select * from Course
--删除
if(OBJECT_ID('proc_DelScore','P')is not null)
drop proc proc_DelScore
go
create proc proc_DelScore
(
@Score_id int
)
as
delete from Score where Score_id=@Score_id
go
exec proc_DelScore 4
--修改学生
if(OBJECT_ID('proc_UpdateScore','P')is not null)
drop proc proc_UpdateScore
go
create proc proc_UpdateScore
(
@Score_id int,
@Score_course_id int,
@Score_student_id int,
@Score_Grade int
)
as
update Score set Score_course_id=@Score_course_id,Score_student_id=@Score_student_id,Score_Grade=@Score_Grade where Score_id=@Score_id
go
exec proc_UpdateScore 1,2,2,94
---------------------------------------结束 下一个----------------------------
--事物
create table Bank
(
Id int identity primary key,
Name nvarchar(20),
Moneys money
)
alter table Bank
add unique(Name)
alter table Bank
Add check(Moneys>=0)
--事物
begin tran
begin try
update Bank set Moneys=(Moneys-800) where Name='张三'
update Bank set Moneys=(Moneys+800) where Name='李四'
end try
begin catch
print('金额不足')
rollback tran
end catch
commit tran
---------------------------------------结束 下一个----------------------------
----------------------游标-------------------------
--创建俩个表
--用户表
create table UserInfo
(
Id int identity primary key,
Account varchar(20),
[PassWord] varchar(20),
[Status] int
)
--用户状态表
create table UserStatus
(
Id int identity primary key,
[Uid] int,
Isdelete int
)
--查询用户信息表
select * from UserInfo
--查询用户状态表
select * from UserStatus
--给用户信息表添加数据
insert UserInfo values
('Test1','test1',0),
('Test2','test2',0),
('Test3','test3',0),
('Test4','test4',0),
('Test5','test5',0)
--给用户状态表添加数据
insert UserStatus values
(1,1),
(2,1),
(3,1),
(4,1),
(5,1)
--创建游标
declare cursor_UserId cursor for select Id from UserInfo
--创建一个变量Tid
declare @Tid int
--打开游标
open cursor_UserId
--逐条读取UserInfo的数据,并把当前数据的ID赋值给Tid
fetch next from cursor_UserId into @Tid
while @@FETCH_STATUS=0
begin
update UserStatus set Isdelete=0 where Uid=@Tid
fetch next from cursor_UserId into @Tid
end
close cursor_UserId
deallocate cursor_UserId
---------------------------------------结束 下一个----------------------------
----------------------行转列,列转行-------------------------
create table StudentDemo
(
--主键
Id int identity primary key,
--学生名称
Name varchar(20),
--学科名称
SubjectName varchar(50),
--学生成绩
Score int
)
go
--插入数据
insert StudentDemo values
('王五','英语',90),
('王五','物理',90),
('王五','语文',90)
select Name '姓名',
MAX(case SubjectName when '语文' then Score else 0 end) '语文',
MAX(case SubjectName when '数学' then Score else 0 end) '数学',
MAX(case SubjectName when '物理' then Score else 0 end) '物理',
AVG(Score) '平均分',
SUM(Score) '总分'
from StudentDemo group by Name

if(exists(select * from Student where name='fun_Getdate'))
drop function fun_Getdate
go
create function fun_Getdate
(

)
returns datetime
as
begin
return getdate()
end
go
select dbo.fun_Getdate()

---------------------------------------结束 下一个----------------------------
----------------------触发器-------------------------


--防删除触发器,尚有学生的班级不可被删除

if(object_Id('tri_DeleteClass','TR') is not null)
drop trigger tri_DeleteClass
go

create trigger tri_DeleteClass
on Class instead of delete
as
--到学生表中去找,假设有学生数据那么不能删除:
if not exists(select s.Id from Student s inner join deleted b on s.CId=b.Id)
delete from Class where Id in (select Id from deleted)
--否则给出提示
else
print('尚有学生,不能删除!')
go
--测试
delete Class where Id=1;
---------------------
--使用触发器实现在修改班级表名称的时候,新增一条学生记录
--如果这个名字的触发器存在,那么删除这个触发器
if(object_Id('tri_AddStudent','TR') is not null)
drop trigger tri_AddStudent
go
--新增这个触发器在班级表上,在修改的时候触发
create trigger tri_AddStudent
on Class for update
as
--触发器执行新增学生
insert Student values ('Test4',20,1)
go
--测试
update Class set Name = Name+'优秀' where id=1
-------------------------触发器结束-----------------------------------


-------------创建视图-----------------
if(exists(select * from sys.objects where name='view_GetSrtudent'))
drop view view_GetSrtudent
go
create view view_GetSrtudent as select * from Student
---------------创建索引-----------
if(exists(select * from sys.indexes where name='index_name'))
drop index Student.index_name
go
create index index_name on Student(Student_name)

原文地址:https://www.cnblogs.com/xuan666/p/10819122.html