日期:2014-05-18  浏览次数:20716 次

创建存储过程,使用游标计算本科及以上学历的员工在总员工数中所占的比例
create database YGGL
use YGGL
  create table Employees (
  EmployeeID char(6) primary key not null,
  Name varchar(6) not null,
  Education varchar(4) not null,
  Birthday datetime not null,
  Sex bit not null default 1,
  WorkYear tinyint null,
  Address varchar(40),
  PhoneNumber char(12) null,
  Pepartment char(3) not null  
)

insert into Employees values('000001','王林','大专','1966-01-23',1,8,'中山路32-1-508','83355668',2);
insert into Employees values('010008','伍容华','本科','1976-03-28',1,3,'北京东路100-2','83321321',1);
insert into Employees values('020010','王向容','硕士','1982-12-09',1,2,'四牌楼10-0-108','83792361',1);
insert into Employees values('020018','李丽','大专','1960-07-30',0,6,'中山东路102-2','83413301',1);
insert into Employees values('102201','刘明','本科','1972-10-18',1,3,'虎距路100-2','83606608',5);
insert into Employees values('102208','朱骏','硕士','1965-09-28',1,2,'牌楼巷5-3-106','84708817',5);
insert into Employees values('108991','钟敏','硕士','1979-08-10',0,4,'中山路10-3-105','83346722',3);
insert into Employees values('111006','张石兵','本科','1974-10-01',1,1,'解放路34-1-203','84563418',5);
insert into Employees values('210678','林涛','大专','1977-04-02',1,2,'中山北路24-35','83467336',3);
insert into Employees values('302566','李玉珉','本科','1968-09-20',1,3,'热和路209-3','58765991',4);
insert into Employees values('308759','叶凡','本科','1978-11-18',1,2,'北京西路3-7-52','83308901',4);
insert into Employees values('504209','陈林琳','大专','1969-09-03',0,5,'汉中路120-4-12','84468158',4);


select * from Employees

  create table Departments(
  DepartmentID int not null primary key identity(1,1),
  DepartmentName char(20) not null,
  Note varchar(100) null
)

insert into Departments values(1,'财务部',null);
insert into Departments values(2,'人力资源部',null);
insert into Departments values (3,'经理办公室',null);
insert into Departments values(4,'研发部',null);
insert into Departments values(5,'市场部',null);

create table Salary(
  EmployeeID char(6) primary key not null,
  InCome float not null,
  OutCome float not null,
)
   
 insert into Salary values('000001',2100.8,123.09)
  insert into Salary values('010008',1582.62,88.03);
  insert into Salary values('102201',2569.88,185.65);
  insert into Salary values('111006',1987.01,79.58);
  insert into Salary values('504209',2066.15,108.0);
  insert into Salary values('302566',2980.7,210.2);
  insert into Salary values('108991',3259.98,281.52);
  insert into Salary values('020010',2860.0,198.0);
  insert into Salary values('020018',2347.68,180.0);
  insert into Salary values('308759',2531.98,199.08);
  insert into Salary values('210678',2240.0,121.0);
  insert into Salary values('102208',1980.0,100.0);

要求要使用游标 求大神救我

------解决方案--------------------
SQL code


declare 
    @edu varchar(10),
    @part_count int,
    @all_count int
---------------------------定义游标
declare mycursor cursor for 
  select 
      distinct
      Education,
      COUNT(Education) over(partition by Education) as part_count,
      COUNT(Education) over() as all_count    
  from Employees 
---------------------------打开游标
open mycursor
---------------------------抓取数据
fetch next from mycursor into @edu,@part_count,@all_count
---------------------------判断是否存在数据
while @@FETCH_STATUS=0
begin
    print @edu+'占总人数比例:'+convert(varchar(100),convert(numeric(38,2),@part_count/1.0/@all_count*100))+'%'
    ---------------------------抓取数据
    fetch next from mycursor into @edu,@part_count,@all_count
End
clos