

这是第三题的大概解题思路,中文名称全部换成实际的表名和列明即可。
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