Storage Process for MSSQL employees to increase salaries

Source: Internet
Author: User

Add a salary to an employee. when half of the employees do not reach 6000 yuan, add 100 to all employees and print the salary.

Code

  --  Add a salary to an employee. when half of the employees do not reach 6000 yuan, add 100 to all employees and print the salary.  
-- Author: [DBA] Xiaoqi
Create Table [ # T ] (ID Int , Name Char ( 10 ), Sal Int ) -- Create temporary table
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

(The number of affected rows is 9 Rows)

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

(The number of affected rows is 9 Rows)

500





 

-- If you are interested, try changing the salary of 6000 to a parameter. It should be very simple.

Custom Search

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.