--创建存储过程.
create or replace procedure test_pro(
userId in nvarchar2, --参数1 in为传入参数,out为传出参数.
userName in test_table.user_name%type, --参数的类型为test_table.user_name这个字段的类型.
userpwd out number) --参数2
is
v_tdCode VARCHAR2(8); --声明变量.
begin
--方法体必须要有.没有用null来占位.
userpwd := 6666;
dbms_output.put_line('用户名:'||userId); --oracle里面用||连接字符串.
--过程里使用MD5.
v_tdCode := tl_Raw.Cast_To_Raw(sys.dbms_obfuscation_toolkit.md5(input_string => 'userPwd'));
end;
--使用sql调用存储过程.
declare
userId varchar(20) := 'aaaa'; --给变量赋值.
userpwd number;
begin
test_pro(userId); --调用并传入参数.
dbms_output.put_line('密码:'||userpwd);
end;
--删除存储过程.
drop procedure test_pro;
IF语句的使用:
--IF
IF exp THEN
NULL;
END IF;
--IF, ELSIF, ELSE
IF exp1 THEN
NULL;
ELSIF exp2 THEN
NULL;
ELSE
NULL;
END IF;
--IF, ELSE
IF exp THEN
NULL;
ELSE
NULL;
END IF;
decode
case when 和IF有差不多的用途.
动态SQL的使用:
CREATE OR REPLACE PROCEDURE test_cursor(
i_gid USER_DATA_BASE.user_data_id%TYPE
) IS
v_sql VARCHAR2(500);
BEGIN
v_sql := 'UPDATE USER_DATA_BASE t set t.USER_DATA_NICK = 123 where t.USER_DATA_ID='||i_gid;
execute immediate (v_sql);
COMMIT;
END;
动态SQL返回游标
DECLARE
sql_str Varchar2(300) := 'select * from bag_data_base';
cur SYS_REFCURSOR;
e bag_data_base%ROWTYPE;
BEGIN
open cur for sql_str;
loop
fetch cur into e;
exit when cur%notfound;
dbms_output.put_line(e.bag_data_id);
end loop;
close cur;
end;
字符串截取:
SELECT substr('This is a test', 6, 2) FROM dual 返回 'is'
substr('SyranMo have a dream', -8, 2)
--Group By having 的使用:
having 相当于where 与where的唯一区别是 当查询语句中有 聚合函数 的时候 就不能用where 了 只能用having
select t.config_dg_data_3d_code from CONFIG_DG_DATA_BASE t GROUP BY t.config_dg_data_3d_code HAVING COUNT(*) > 2;