这是第三题的大概解题思路,中文名称全部换成实际的表名和列明即可。
create procedure 存储过程名称
@Number nvarchar(50) --员工编号
as
begin
declare @CountNumber as varchar(10),@name as varchar(50), @time as datetime,@message as varchar(100)
select @CountNumber=COUNT(*) from 员工表 where 员工编号=@Number and 部门<>10 and 入职时间 > (select top 1 入职时间 from 员工表 where 部门=10 order by 入职时间 desc)
if @CountNumber>0--输出员工、入职时间
begin
select @name=员工姓名,@time=入职时间 from 员工表 where 员工编号=@Number
print '员工姓名:'+@name+' 入职时间:'+@time
end
else--员工保存在
begin
set @message= '员工编号不存在'
print '结果:'+@message
end
end
这是第四题的思路
--第一种循环:利用游标循环
declare @id int,@value nvarchar(100);
begin
declare c_test_main cursor fast_forward for select 员工姓名,工资 from emp;--查出需要的集合放到游标中
open c_test_main;--打开游标
while 1=1 --开始循环
begin
fetch next from c_test_main into @id,@value; --赋值到变量中
if(@@fetch_status!=0)break;--如果没有结果退出循环
if @value<1200
begin
print '员工:'+@id+'该加工资了'
end
else if @value>=1200 and @value<2100
begin
print '员工:'+@id+'还行'
end
else
begin
print '员工:'+@id+'有钱人'
end
end
close c_test_main --关闭游标
deallocate c_test_main --释放游标
end
go
--第二种循环:特定ID循环,类似For循环
declare @max int
declare @i int
select ROW_NUMBER() over (order by id) as 'Id',员工姓名,工资 into #temp from emp
select @max=max(Id) from #temp
set @i = 1
while (@i <= @max)
begin
begin
declare @name int
declare @money int
select @name=员工姓名,@money=工资 from #temp where ID = @i
if @money<1200
begin
print '员工:'+@name+'该加工资了'
break
end
else if @money>=1200 and @money<2100
begin
print '员工:'+@name+'还行'
break
end
else
begin
print '员工:'+@name+'有钱人'
break
end
end
set @i = @i + 1
end
go
第3题:
1、创建员工表,插入数据
-- ----------------------------
-- Table structure for emp_info
-- ----------------------------
DROP TABLE IF EXISTS `emp_info`;
CREATE TABLE `emp_info` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`emp_no` int(11) NULL DEFAULT NULL,
`emp_name` varchar(10) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NULL DEFAULT NULL,
`dept_no` int(2) NULL DEFAULT NULL,
`hiredate` date NULL DEFAULT NULL,
`salary` int(10) NULL DEFAULT NULL,
PRIMARY KEY (`id`) USING BTREE
) ENGINE = InnoDB AUTO_INCREMENT = 6 CHARACTER SET = utf8mb4 COLLATE = utf8mb4_general_ci ROW_FORMAT = Dynamic;
-- ----------------------------
-- Records of emp_info
-- ----------------------------
INSERT INTO `emp_info` VALUES (1, 1001, '张三', 10, '2021-08-10', 1000);
INSERT INTO `emp_info` VALUES (2, 1002, '李四', 10, '2021-08-20', 1200);
INSERT INTO `emp_info` VALUES (3, 1003, '王五', 10, '2021-08-30', 1500);
INSERT INTO `emp_info` VALUES (4, 1004, '赵六', 20, '2021-07-29', 1800);
INSERT INTO `emp_info` VALUES (5, 1005, '田七', 20, '2021-09-29', 2500);
2、编写存储过程
drop PROCEDURE if EXISTS proc_emp;
delimiter //
CREATE PROCEDURE proc_emp (IN empNo INT )
begin
declare empCount INT;#条件:员工编号
declare empCountCondition INT;#条件:员工编号、不是10部门的员工、入职日期比任何一个10部门员工晚
declare empName varchar(10);
declare empHiredate date;
declare message varchar(10);
select COUNT(*) into empCount from emp_info where emp_no=empNo;
if empCount=0 then
set message= '员工编号不存在';
select message as '结果';
else
select COUNT(*),emp_name,hiredate into empCountCondition,empName,empHiredate from emp_info where emp_no=empNo and dept_no<>10 and hiredate > (select max(hiredate) from emp_info where dept_no=10);
if empCountCondition>0 then
select empName as '员工姓名',empHiredate as '入职时间';
else
select NULL as '结果';
end if;
end if;
end //
delimiter ;
3、执行存储过程
call(1000); --不存在
call(1001);--1001、1002、1003输出NULL
call(1004);输出NULL
call(1005);输出名字,入职日期
第4题
接着第三题继续
1、编写存储过程:
drop procedure if EXISTS evaluate_salary;
delimiter //
create procedure evaluate_salary()
begin
select
emp_name,
(case
when (salary<1200) then '该加工资了'
when (salary>=1200 and salary<2100) then '还行'
-- else 'NB'
end) as '工资评价'
from emp_info;
end //
delimiter;
call evaluate_salary();
```sql
2、调用存储过程
call evaluate_salary();