create table student(
id int identity(1,1) primary key ,
cname varchar(50),
gender varchar(20),
score numeric(6,2));
insert into student values('张三','男',44.4)
insert into student values('李四','男',45.5);
insert into student values('小花','女',67.6);
insert into student values('李平','女',98.7);
2、在sql查询分析器里以命令的方式创建存储过程。
create proc whp as
if(
(select avg(score) from student where gender='男')>(select avg(score) from student where gender='女')
)
print('男生平均工资大于女生');
else
print('生平均工资小于女生');
create proc whp1 as
begin
declare @x numeric(6,2),@y numeric(6,2)
set @x=(select avg(score) from student where gender='男')
set @y=(select avg(score) from student where gender='女');
print(@x);
print(@y);
if(@x>@y)
print('男生平均成绩大于女生');
else
print('男生平均成绩小于女生');
end
分格线
再一个简单存储过程,输入学生的姓名,把学生的成绩输出来。相应的存储过程如下:
create proc whp2 @cname varchar(20)=null
as
if @cname is null
begin
print('输入的姓名不能为空');
return;
end
else
begin
select * from student where cname=@cname
end
运行时,whp2 学生姓名
对于一个已经存在的存储过程,如果要想修改,就把创建时的create改成alter就可以对已经存在的存储过程进行修改
语法如下
ALTER PROC <存储过程名>
[(@<参数名><数据类型>……]
AS
SQL语句|语句块