视图的概念
视图中不存放数据
表视图的多样性
视图的用途
CREATE VIEW view_Stulnfo AS <SELECT语句>
1 use StudentManager 2 go 3 --判断视图是否存在 4 if exists(select * from sysobjects where name='view_ScoreQuery') 5 drop view view_ScoreQuery 6 go 7 --创建视图 8 create view view_ScoreQuery 9 as 10 select top 1000 Students.StudentId,StudentName,ClassName, 11 C#=CSharp,SQLDB=SQLServerDB, 12 ScoreSum=(CSharp+SQLServerDB) from Students 13 inner join ScoreList on Students.StudentId=ScoreList.StudentId 14 inner join StudentClass on Students.ClassId=StudentClass.ClassId 15 order by StudentClass.ClassId 16 go 17 --使用视图查询 18 select * from view_ScoreQueryView Code
IF EXISTS (SELECT*FROM sysobjects WHERE name = view_Stulnfo) DROP VIEW view_Stulnfo
SELECT* FROM view Stulnfo
类似于Java和C#语言中的方法
应用程序发送SQL的过程
<传输语句> <语法检查> < 语句优化> <语句编译> <语句执行>
应用程序调用存储过程或视图的过程
<传输参数> <语句执行>
系统存储过程
扩展存储过程
用户自定义存储过程
调用存储讨程的语法
EXECUTE 过程名[参数] EXEC 过程名[参数]
如果执行存储过程的语句是批处理中的第一个语句,则可以不指定EXECUTE关键字
常用系统存储过程调用示例
1 sp_databases 2 3 EXEC sp_renamedb 'ProductDB','pDB' 4 5 USE StudentManager 6 GO 7 sp_tables 8 9 EXEC sp_columns Students 10 11 EXEC sp_help Students 12 13 EXEC sp_helpconstraint Students 14 15 EXEC sp_stored_proceduresView Code
系统存储过程 | 说明 |
sp_databases | 列出服务器上的所有数据库 |
sp_helpdb | 报告有关指定数据库或所有数据库的信息 |
sp_renamedb | 更改数据库的名称 |
sp_tables | 返回当前环境下可查询的对象的列表 |
sp_columns | 返回某个表列的信息 |
sp_help | 查看某个表的所有信息 |
sp_helpconstraint | 查看某个表的约束 |
sp_helpindex | 查看某个表的索引 |
sp_stored_procedures | 列出当前环境中的所有存储过程 |
sp_password | 添加或修改登录帐户的密码 |
sp_helptext | 显示默认值、未加密的存储过程、用户定义的存储过程、触发器或视图的实际文本 |
xp_cmdshell
1 USE master 2 GO 3 EXEC xp_cmdshell 'mkdir D:\ProductDB', NO_OUTPUT 4 IF EXISTS(SELECT * FROM sysdatabases WHERE name='ProductDB') 5 DROP DATABASE ProductDB 6 GO 7 --CREATE DATABASE ProductDB 8 -- ( 9 -- … 10 --) 11 --GO 12 EXEC xp_cmdshell 'dir D:\ProductDB\' -- 查看文件View Code
定义存储过程的语法
存储过程的参数
创建存储过程usp_ScoreQuery
代码编写分析
1 use StudentManager 2 go 3 if exists(select * from sysobjects where name='usp_ScoreQuery') 4 drop procedure usp_ScoreQuery 5 go 6 create procedure usp_ScoreQuery --创建存储过程 7 as 8 --查询考试信息 9 select Students.StudentId,StudentName,ClassName, 10 ScoreSum=(CSharp+SQLServerDB) from Students 11 inner join StudentClass on StudentClass.ClassId=Students.ClassId 12 inner join ScoreList on Students.StudentId=ScoreList.StudentId 13 order by ScoreSum DESC 14 --统计分析考试信息 15 select StudentClass.ClassId,C#Avg=avg(CSharp),DBAvg=avg(SQLServerDB) into #scoreTemp 16 from StudentClass 17 inner join Students on StudentClass.ClassId=Students.ClassId 18 inner join ScoreList on ScoreList.StudentId=Students.StudentId 19 group by StudentClass.ClassId order by ClassId 20 select ClassName,C#Avg,DBAvg from #scoreTemp 21 inner join StudentClass on StudentClass.ClassId=#scoreTemp.ClassId 22 go 23 exec usp_ScoreQuery --调用存储过程View Code
存储过程的参数分两种:
查询考试成绩,要求能够按照自定义的及格线查询结果?
1 use StudentManager 2 go 3 if exists(select * from sysobjects where name='usp_ScoreQuery2') 4 drop procedure usp_ScoreQuery2 5 go 6 --创建带参数的存储过程 7 create procedure usp_ScoreQuery2 8 @CSharp int, 9 @DB int 10 as 11 select Students.StudentId,StudentName,C#=CSharp,DB=SQLServerDB 12 from Students 13 inner join ScoreList on Students.StudentId=ScoreList.StudentId 14 where CSharp<@CSharp or SQLServerDB<@DB 15 go 16 --调用带参数的存储过程 17 exec usp_ScoreQuery2 60,65 --按照参数顺序赋值 18 exec usp_ScoreQuery2 @DB=65,@CSharp=60 --参数顺序可以调换View Code
查询中,如果用户没有输入及格线参数,则默认60
1 use StudentManager 2 go 3 if exists(select * from sysobjects where name='usp_ScoreQuery3') 4 drop procedure usp_ScoreQuery3 5 go 6 --创建带参数的存储过程 7 create procedure usp_ScoreQuery3 8 @CSharp int=60, 9 @DB int=60 10 as 11 select Students.StudentId,StudentName,C#=CSharp,DB=SQLServerDB 12 from Students 13 inner join ScoreList on Students.StudentId=ScoreList.StudentId 14 where CSharp<@CSharp or SQLServerDB<@DB 15 go 16 --调用带参数的存储过程 17 exec usp_ScoreQuery3 65 --第二个参数没有赋值,则默认 18 exec usp_ScoreQuery3 @DB=65 19 exec usp_ScoreQuery3 default,65 --不使用显示方式赋值 20 exec usp_ScoreQuery3 --两个参数都是用默认参数View Code
问题:查询考试成绩,要求自定义分数线,显示查询列表,并输出缺考总人数、不及格总人数?
1 use StudentManager 2 go 3 if exists(select * from sysobjects where name='usp_ScoreQuery4') 4 drop procedure usp_ScoreQuery4 5 go 6 create procedure usp_ScoreQuery4 --创建带参数的存储过程 7 @AbsentCount int output,--缺考总人数 8 @FailedCount int output,--不及格总人数 9 @CSharp int=60, 10 @DB int=60 11 as 12 select Students.StudentId,StudentName,C#=CSharp,DB=SQLServerDB 13 from Students 14 inner join ScoreList on Students.StudentId=ScoreList.StudentId 15 where CSharp<@CSharp or SQLServerDB<@DB --显示结果列表 16 select @AbsentCount=count(*) from Students 17 where StudentId not in(select StudentId from ScoreList) --查询缺考总人数 18 select @FailedCount=count(*) from ScoreList 19 where CSharp<@CSharp or SQLServerDB<@DB --查询不及格总人数 20 goView Code
输出参数在使用前要首先定义,调用时也要使用output
1 use StudentManager 2 go 3 --调用带参数的存储过程 4 declare @AbsentCount int,@FailedCount int --首先定义输出参数 5 exec usp_ScoreQuery4 @AbsentCount output,@FailedCount output 6 --使用反馈的结果 7 select 缺考总数=@AbsentCount,不及格总数=@FailedCountView Code
1 use StudentManager 2 go 3 if exists(select * from sysobjects where name='usp_ScoreQuery') 4 drop procedure usp_ScoreQuery 5 go 6 create procedure usp_ScoreQuery 7 8 as 9 --查询考试信息 10 select Students.StudentId,StudentName,ClassName,ScoreSum=(SQLServerDB+CSharp) from Students 11 inner join StudentClass on Students.ClassId=StudentClass.ClassId 12 inner join ScoreList on Students.StudentId=ScoreList.StudentId 13 order by ScoreSum DESC 14 --统计分析考试信息 15 select ClassName,C#Avg=AVG(CSharp),DBAvg=AVG(SQLServerDB) 16 from StudentClass 17 inner join Students on StudentClass.ClassId=Students.ClassId 18 inner join ScoreList on ScoreList.StudentId=Students.StudentId 19 group by ClassName 20 go 21 22 exec usp_ScoreQuery --调用存储过程 23 24 25 --if exists(select * from sysobjects where name='usp_ScoreQuery2') 26 --drop procedure usp_ScoreQuery2 27 --go 28 --create procedure usp_ScoreQuery2 29 --@CSharp int, --参数定义 30 --@DB int 31 --as 32 -- select Students.StudentId,StudentName,C#=CSharp, DB=SQLServerDB from Students 33 -- inner join ScoreList on Students.StudentId=ScoreList.StudentId 34 -- where CSharp<@CSharp or SQLServerDB<@DB 35 --go 36 ----调用带参数的SQL语句 37 --exec usp_ScoreQuery2 60,65 --按照参数顺序赋值 38 --exec usp_ScoreQuery2 @DB=65,@CSharp=60 39 40 41 if exists(select * from sysobjects where name='usp_ScoreQuery2') 42 drop procedure usp_ScoreQuery2 43 go 44 create procedure usp_ScoreQuery2 45 @CSharp int=60, 46 @DB int=60 47 as 48 select Students.StudentId,StudentName,C#=CSharp, DB=SQLServerDB from Students 49 inner join ScoreList on Students.StudentId=ScoreList.StudentId 50 where CSharp<@CSharp or SQLServerDB<@DB 51 go 52 --调用带参数的SQL语句 53 exec usp_ScoreQuery2 65 54 exec usp_ScoreQuery2 @DB=65 55 56 exec usp_ScoreQuery2 default,65 57 exec usp_ScoreQuery2 58 59 60 if exists(select * from sysobjects where name='usp_ScoreQuery4') 61 drop procedure usp_ScoreQuery4 62 go 63 create procedure usp_ScoreQuery4 64 @AbsentCount int output,--缺考总人数 65 @FailedCount int output,--不及格总人数 66 @CSharp int=60, 67 @DB int=60 68 as 69 select Students.StudentId,StudentName,C#=CSharp, DB=SQLServerDB from Students 70 inner join ScoreList on Students.StudentId=ScoreList.StudentId 71 where CSharp<@CSharp or SQLServerDB<@DB 72 73 select @AbsentCount=COUNT(*) from Students 74 where StudentId not in(Select StudentId from ScoreList) 75 select @FailedCount=COUNT(*) from ScoreList 76 where CSharp<@CSharp or SQLServerDB<@DB 77 go 78 --调用带输出参数的存储过程 79 declare @AbsentCount int,@FailedCount int --首先定义输出参数 80 exec usp_ScoreQuery4 @AbsentCount output,@FailedCount output 81 --读取输出参数 82 select 缺考总数=@AbsentCount,不及格总数=@FailedCountView Code
END