[em11] IF EXISTS(SELECT TABLE_NAME FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_NAME='employees') DROP TABLE employees go create table employees ( employee_id int, name varchar(50), sex varchar(50), department varchar(50), salary money ) go insert into employees values(1,'陈有朋','男','项目部',6000) insert into employees values(2,'孙晓晴','女','项目部',8000) insert into employees values(3,'张晓峰','男','录入部',1000) insert into employees values(4,'慕容雪','男','检验部',1500) insert into employees values(5,'陈秋萍','女','办公室',1000) go --1.定义一个变量,并将其赋值为员工数据表中全体员工的工资最大值,并打印输出。 declare @max_salary int select @max_salary=max(salary) from employees print @max_salary go select * from employees --2.在员工数据表中,如果有员工在办公室工作,那么输出这些员工的姓名;否则输出信息,说明没有员工在办公室工作。 --假设办公室工作人员只有一人 declare @message varchar(30),@name varchar(20) if exists( select * from employees where department='办公室') begin select @name=name from employees where department='办公室' print '下列人员在办公室工作:'+@name end else begin set @message='没有人在办公室工作。' print @message end go --利用游标,有多名员工工作在办公室 declare @e_name varchar(50) if((select count (*) from employees where department='办公室')>0) begin print '下列人员在办公室工作:' declare e_cursor cursor for select name from employees where department='办公室'order by name open e_cursor FETCH NEXT from e_cursor into @e_name print @e_name while @@FETCH_STATUS=0 BEGIN FETCH NEXT from e_cursor into @e_name if @@FETCH_STATUS=0 begin print @e_name print @@FETCH_STATUS end end close e_cursor deallocate e_cursor end go --3.在员工数据表中,判断是否有员工的工资少于2000,如果有,则将所有员工的工资增加200,直到所有员工的工资都多于2000或有员工的工资超过了10000。(while语句的练习) while exists(select * from employees where salary<2000) begin update employees set salary=salary+200 if(select max(salary) from employees)>10000 begin print '有人工资超过了10000' break end end --4.声明一个变量,为money类型,并为其赋值80,然后到pubs数据库的titles表中检索价格值大于此变量值的记录有多少条。 use pubs declare @condition money set @condition=80.00 select count(*) from titles where price>@condition go --5.将northwind数据库中employees表中的名字(firstname ,lastname)和ID号联系起来显示。例如:5-Steven Buchanan use northwind select convert(varchar(8),employeeid)+'-'+firstname+' '+lastname as [name] from employees go --6.声明一个变量,如果这个变量大于100显示‘@var>100’,如果小于20则显示‘@var<20’,如果它在20与50之间显示‘20<=@var<=50’,如果它在50与100之间显示‘50<=@var<=100’。 declare @var int set @var=61 if @var>50 if @var>100 print '@var>100' else print '50<=@var<=100' else if @var<20 print '@var<20' else print '50<=@var<=100' go --7.创建一个员工表,如下代码所示.更新员工的工资,工作级别为1的上调8%,工作级别为2的上调7%,工作级别为3的上调6%,工作级别为4的上调5%. use pubs go IF EXISTS(SELECT TABLE_NAME FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_NAME = 'emp') DROP TABLE emp go create table emp ( e_name char(10), e_wage money, job_level int) go insert emp values('l1',2000,3) insert emp values('l2',4000,1) insert emp values('l3',3000,2) insert emp values('l4',1500,4) go update emp set e_wage= case when job_level=1 then e_wage*1.08 when job_level=2 then e_wage*1.07 when job_level=3 then e_wage*1.06 when job_level=4 then e_wage*1.05 end select * from emp
[em11][em11][em11][em11] |