mysql编写存储过程给员工加工资_一个增加员工工资的数据库存储过程
一個面試問題,寫一個SQL語句或者存儲過程,給員工加工資,當員工里面有一半的人沒有達到6000元的時候,所有員工加100,并打印加了多少工資。
存儲過程如下:
--給員工加工資,當員工里面有一半的人沒有達到6000元的時候,所有員工加100,并打印加了多少工資
create table [#t](id int, name char(10),sal int) --創(chuàng)建臨時表
insert into #t
select 1,'alex',1500 union all
select 2,'kelly',5000 union all
select 3,'lily',10000 union all
select 4,'judy',6000 union all
select 5,'tom',5900 union all
select 6,'cherly',4000 union all
select 7,'cherly',3500 union all
select 8,'romeo',7000 union all
select 9,'frank',5500
select * from #t
go
if Exists(Select name From sysobjects Where name='add_sal' And type='P')
Drop Procedure add_sal
Go
create proc add_sal
as
begin
set nocount on
declare @count1 float,@count2 int,@up_sal int
set @up_sal=0
set @count1=(select count(*) from #t)
set @count2=(select count(8) from #t where sal<6000)
while(@count2>(@count1/2))
begin
update #t set sal=sal+100
set @count1=(select count(*) from #t)
set @count2=(select count(8) from #t where sal<6000)
set @up_sal=@up_sal+100
end
print @up_sal
set nocount off
end
go
exec add_sal
go
drop table #t
go
這個存儲過程的運行結(jié)果如下:
id name sal
----------- ---------- -----------
1 alex 1500
2 kelly 5000
3 lily 10000
4 judy 6000
5 tom 5900
6 cherly 4000
7 cherly 3500
8 romeo 7000
9 frank 5500
(所影響的行數(shù)為 9 行)
500
如果各位有興趣的話,也可以試試吧6000這個工資改成參數(shù),應(yīng)該是很簡單的。
總結(jié)
以上是生活随笔為你收集整理的mysql编写存储过程给员工加工资_一个增加员工工资的数据库存储过程的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 实现多线程的3种方式
- 下一篇: GAMS-01 实用注释命令