- /*获取在指定系中选修某一门课程的学生基本信息和成绩,并按成绩降序排列*/
- CREATE PROC STU_GRADE
- @SDEPT VARCHAR(50),
- @CNAME VARCHAR(10)
- AS
- SELECT STUDENT.SNO 学号,STUDENT.SNAME 姓名,STUDENT.SGENTLE 性别,COURSE.CNAME 课程名,COURSE.CGRADE 学分,SC.GRADE 成绩
- FROM STUDENT JOIN SC
- ON STUDENT.SNO = SC.SNO
- JOIN COURSE
- ON COURSE.CNO = SC.CNO
- WHERE STUDENT.SDEPT=@SDEPT
- AND COURSE.CNAME=@CNAME
- ORDER BY SC.GRADE DESC
- EXEC STU_GRADE '计算机','操作系统'
- EXEC STU_GRADE '计算机','数据结构'
- /*模糊查询*/
- CREATE PROC STU_SNAME
- @SNAME VARCHAR(50)/*在创建存储过程中的变量的时候,变量的类型与长度最好与数据表中的一致,否则可能查不出数据;像这里如果写成10的话就查不出来数据*/
- AS
- SELECT *
- FROM STUDENT
- WHERE SNAME LIKE @SNAME
- EXEC STU_SNAME '张%'
- set statistics io on
- set nocount on
- set statistics time on
- set ansi_nulls on
- select * into qlei from student
- --以下说明了set ansi_nulls on的用法:
- select * from qlei where sname is null
- select * from qlei where sname is not null
- set ansi_nulls on
- select * from qlei where sname = null
- select * from qlei where sname<>null
- set ansi_nulls off
- select * from qlei where sname = null
- select * from qlei where sname<>null
- --以下说明了SET QUOTED_IDENTIFIER ON的用法
- set quoted_identifier on
- select * into "user" from qlei
- select * from "user" where sno='990028'
- select * from [user] where sno='990028'
- set quoted_identifier off
- select * from user where sno='990028'--这样写报错
- select * from [user] where sno='990028'
- select * from [user] where sno="990028"
- --以下是sql中函数的创建及使用方法
- create function test(@Num varchar(20))--@Num 参数
- returns varchar(50) --返回值类型
- as
- begin
- declare @MSG varchar(20)
- if(@Num =1)
- select @MSG ='正确'
- else
- select @MSG ='错误'
- return @MSG
- end
- --调用函数
- select dbo.test(2)
- --创建返回table类型的函数
- create function GetAllStudents()
- returns table
- as
- return (select * from student)
- --调用函数
- select * from GetAllStudents()--这里只能这样写,不能写成像上面的那种形式
本文转自sucre03 51CTO博客,原文链接:http://blog.51cto.com/sucre/420259,如需转载请自行联系原作者