Oracle-(if/case/以及模拟注册登录)练习-20131015
生活随笔
收集整理的這篇文章主要介紹了
Oracle-(if/case/以及模拟注册登录)练习-20131015
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
--作業
--1、 ?輸入部門編號,按照下列加薪比例執行(用if-elsif 和case兩種方法實現)。--deptno ?raise(%)--10 ?5%--20 ?10%--30 ?15%--40 ?20%--加薪比例以現有的sal為標準
--方法一select deptno,ename,sal,(case deptnowhen 10 then sal * 0.05when 20 then sal * 0.1when 30 then sal * 0.15when 40 then sal * 0.2end) raise_sal from emp;
--方法二 ?declarecursor c is select deptno, ename, sal from emp;beginfor v_temp in c loopdbms_output.put(v_temp.deptno || ' ?' || v_temp.ename || ' ?' || v_temp.sal ?|| ' + ');if v_temp.deptno = 10 thendbms_output.put_line(v_temp.sal * 0.05 || ' = ' || (v_temp.sal + v_temp.sal * 0.05));elsif v_temp.deptno = 20 thendbms_output.put_line(v_temp.sal * 0.1 || ' = ' || (v_temp.sal + v_temp.sal * 0.1));elsif v_temp.deptno = 30 thendbms_output.put_line(v_temp.sal * 0.15 || ' = ' || (v_temp.sal + v_temp.sal * 0.15));elsif v_temp.deptno = 40 thendbms_output.put_line(v_temp.sal * 0.20 || ' = ' || (v_temp.sal + v_temp.sal * 0.20));end if;end loop;end;
--2、接受2個數相除,并顯示結果,如果除數為0,則顯示錯誤提示。
select (casewhen &除數 = 0 then '除數不能為零'when &除數 <> 0 then '&被除數 / &除數' || ' = ' || to_char(&被除數 / &除數)end) 除法結果 from dual;
--3、自己創建一張userinfo表,包含兩個字段username,password,--表中的記錄信息取自emp表ename,empno字段,寫一個PL/SQL程序,--模擬登陸的過程,--用戶分別輸入用戶名和密碼,對于登陸成功和失敗分別給出提示信息.
--刪表drop table userinfo cascade constraint;--建表create table userinfo (username varchar2(20),password varchar2(20));--插數據declarecursor c is select ename,empno from emp;beginfor v_temp in c loopinsert into userinfo values(v_temp.ename,v_temp.empno);end loop;commit;end;--查詢數據select * from userinfo;--登陸功能create or replace procedure p_userinfo_login(v_t_us_na in userinfo.username%type,v_t_us_pa in userinfo.password%type) is--v_us_na in userinfo.username%type,v_us_pa in userinfo.password%type--v_us_na in varchar2(20),v_us_pa in varchar2(20)v_us_na userinfo.username%type := v_t_us_na;v_us_pa userinfo.password%type := v_t_us_pa;cursor c is select * from userinfo;begindbms_output.put_line('******登陸功能******');for v_temp in c loopif v_temp.username = v_us_na thenv_us_na := '密碼錯誤';if v_temp.password = v_us_pa thenv_us_pa := '登陸成功';exit;end if;end if;end loop;if v_us_pa = '登陸成功' thendbms_output.put_line(v_us_pa);elsif v_us_na = '密碼錯誤' thendbms_output.put_line(v_us_na);elsedbms_output.put_line('用戶不存在');end if;end;--執行方法exec p_userinfo_login('SMITH','7369');
--4、用userinfo表,寫一個PL/SQL程序,--模擬注冊的過程,--用戶分別輸入用戶名和密碼,對于登陸成功和失敗分別給出提示信息.
--注冊功能create or replace procedure p_userinfo_register(v_t_us_na in userinfo.username%type,v_t_us_pa in userinfo.password%type,v_t_us_paa in userinfo.password%type) isv_us_na userinfo.username%type := v_t_us_na;v_us_pa userinfo.password%type := v_t_us_pa;v_us_paa userinfo.password%type := v_t_us_paa;v_count number;begindbms_output.put_line('******注冊功能******');select count(*) into v_count from userinfo where username = v_us_na;if v_count = 0 thenif v_us_pa is not null and v_us_paa is not null then--問題 null和null是不能比較的if v_us_pa = v_us_paa theninsert into userinfo values(v_us_na,v_us_pa);dbms_output.put_line('注冊成功');commit;elsedbms_output.put_line('密碼不一致');end if;elsedbms_output.put_line('密碼不能為空');end if;elsedbms_output.put_line('用戶名重復');end if;end;--執行方法exec p_userinfo_register('SMITH','7369','73691');--查詢數據select * from userinfo;
--1、 ?輸入部門編號,按照下列加薪比例執行(用if-elsif 和case兩種方法實現)。--deptno ?raise(%)--10 ?5%--20 ?10%--30 ?15%--40 ?20%--加薪比例以現有的sal為標準
--方法一select deptno,ename,sal,(case deptnowhen 10 then sal * 0.05when 20 then sal * 0.1when 30 then sal * 0.15when 40 then sal * 0.2end) raise_sal from emp;
--方法二 ?declarecursor c is select deptno, ename, sal from emp;beginfor v_temp in c loopdbms_output.put(v_temp.deptno || ' ?' || v_temp.ename || ' ?' || v_temp.sal ?|| ' + ');if v_temp.deptno = 10 thendbms_output.put_line(v_temp.sal * 0.05 || ' = ' || (v_temp.sal + v_temp.sal * 0.05));elsif v_temp.deptno = 20 thendbms_output.put_line(v_temp.sal * 0.1 || ' = ' || (v_temp.sal + v_temp.sal * 0.1));elsif v_temp.deptno = 30 thendbms_output.put_line(v_temp.sal * 0.15 || ' = ' || (v_temp.sal + v_temp.sal * 0.15));elsif v_temp.deptno = 40 thendbms_output.put_line(v_temp.sal * 0.20 || ' = ' || (v_temp.sal + v_temp.sal * 0.20));end if;end loop;end;
--2、接受2個數相除,并顯示結果,如果除數為0,則顯示錯誤提示。
select (casewhen &除數 = 0 then '除數不能為零'when &除數 <> 0 then '&被除數 / &除數' || ' = ' || to_char(&被除數 / &除數)end) 除法結果 from dual;
--3、自己創建一張userinfo表,包含兩個字段username,password,--表中的記錄信息取自emp表ename,empno字段,寫一個PL/SQL程序,--模擬登陸的過程,--用戶分別輸入用戶名和密碼,對于登陸成功和失敗分別給出提示信息.
--刪表drop table userinfo cascade constraint;--建表create table userinfo (username varchar2(20),password varchar2(20));--插數據declarecursor c is select ename,empno from emp;beginfor v_temp in c loopinsert into userinfo values(v_temp.ename,v_temp.empno);end loop;commit;end;--查詢數據select * from userinfo;--登陸功能create or replace procedure p_userinfo_login(v_t_us_na in userinfo.username%type,v_t_us_pa in userinfo.password%type) is--v_us_na in userinfo.username%type,v_us_pa in userinfo.password%type--v_us_na in varchar2(20),v_us_pa in varchar2(20)v_us_na userinfo.username%type := v_t_us_na;v_us_pa userinfo.password%type := v_t_us_pa;cursor c is select * from userinfo;begindbms_output.put_line('******登陸功能******');for v_temp in c loopif v_temp.username = v_us_na thenv_us_na := '密碼錯誤';if v_temp.password = v_us_pa thenv_us_pa := '登陸成功';exit;end if;end if;end loop;if v_us_pa = '登陸成功' thendbms_output.put_line(v_us_pa);elsif v_us_na = '密碼錯誤' thendbms_output.put_line(v_us_na);elsedbms_output.put_line('用戶不存在');end if;end;--執行方法exec p_userinfo_login('SMITH','7369');
--4、用userinfo表,寫一個PL/SQL程序,--模擬注冊的過程,--用戶分別輸入用戶名和密碼,對于登陸成功和失敗分別給出提示信息.
--注冊功能create or replace procedure p_userinfo_register(v_t_us_na in userinfo.username%type,v_t_us_pa in userinfo.password%type,v_t_us_paa in userinfo.password%type) isv_us_na userinfo.username%type := v_t_us_na;v_us_pa userinfo.password%type := v_t_us_pa;v_us_paa userinfo.password%type := v_t_us_paa;v_count number;begindbms_output.put_line('******注冊功能******');select count(*) into v_count from userinfo where username = v_us_na;if v_count = 0 thenif v_us_pa is not null and v_us_paa is not null then--問題 null和null是不能比較的if v_us_pa = v_us_paa theninsert into userinfo values(v_us_na,v_us_pa);dbms_output.put_line('注冊成功');commit;elsedbms_output.put_line('密碼不一致');end if;elsedbms_output.put_line('密碼不能為空');end if;elsedbms_output.put_line('用戶名重復');end if;end;--執行方法exec p_userinfo_register('SMITH','7369','73691');--查詢數據select * from userinfo;
總結
以上是生活随笔為你收集整理的Oracle-(if/case/以及模拟注册登录)练习-20131015的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: golang获取程序运行路径
- 下一篇: 生成文件的另一种思路——共享文件同步