PL/SQL: Local function definition in a public procedure / function

Occasionally, we will need to define local used functions in a public procedure or a function. Fortunately, PL/SQL supports embedded functions. I spent more than an hour trying to find the correct way to use this feature. Yes. forgive me, I am a little dumb. According to my retries, the best position to put your local functions is sharp before the BEGIN symbol. And here's a sample.

set serveroutput on;
CREATE OR REPLACE PROCEDURE tmp12345(v_full_name IN VARCHAR2) AS
	localv varchar2(100);
	-- local function definitaions start ---
	FUNCTION FETCH_REAL_NAME(v_statement_name IN VARCHAR2) RETURN VARCHAR2
	AS
		pattern VARCHAR2(100) := '^(Dr|Ms|Mx|Mr|Mrs)\s+';
		full_name varchar2(100);
	BEGIN
		-- replace possible multiple spaces to one space
		full_name := TRIM(REGEXP_REPLACE(v_statement_name, '\s+',  ' '));
		IF (REGEXP_LIKE(full_name, pattern, 'i')) THEN
			-- REGEXP_REPLACE(string, pattern [, replacement_string [, start_position [, nth_appearance [, match_parameter ] ] ] ]) 
			RETURN REGEXP_REPLACE(full_name, pattern, '', 1, 0, 'i');
		ELSE
			RETURN full_name;
		END IF;
	END;
	FUNCTION FETCH_MIDDLE_NAME(full_name IN VARCHAR) RETURN VARCHAR AS
		middle_name VARCHAR2(100);
	BEGIN
		middle_name := SUBSTR(full_name, INSTR(full_name, ' ') + 1, INSTR(full_name, ' ', 1, 2) - INSTR(full_name, ' ') - 1);
		IF (LENGTH(middle_name) > 10) THEN
			-- return initial instead
			RETURN SUBSTR(middle_name, 1, 1);
		ELSE
			RETURN middle_name;
		END IF;
	END;
	-- local functions definitaions end ---
BEGIN
	-- main logic here
	localv := v_full_name;
	DBMS_OUTPUT.PUT_LINE(localv);
	DBMS_OUTPUT.PUT_LINE(FETCH_MIDDLE_NAME(FETCH_REAL_NAME(v_full_name)));
END;
/

MySQL IF(EXPRESSION, A, B) equivalent in PL/SQL (Oracle)

In MySQL, we can

SELECT IF(A IS NULL, "something", A) FROM some_table;

If you want to do a similar thing in PL/SQL, you need to

SELECT (CASE WHEN A IS NULL THEN "something" ELSE A END) FROM some_table;

If you want to get the first non-null value from several fields, you have a better choice than using the CASE keyword.

SELECT COALESCE(A, B) FROM some_table

see more information about the Oracle COALESCE function

Vodafone ultrahub pihole local DNS setup

ultrahub-pihole-setup.jpg

The key is to tick off the DNS Proxy switch, otherwise, DNS clients won't obtain the local DNS value as their DNS.

香港老电影《甜蜜蜜》

tian-mi-mi.jpg

追着一位推友的脚步,上周日在家里看了这部香港老电影《甜蜜蜜》。剧情挺虐,编剧挺狠,不过还好给了大团圆的结局。请原谅我,我是剧透专家。艺术嘛,源于生活高于生活,可以理解。

怎么说呢,人这一辈子,有许多偶然,也有许多必然。你不经意间的决定,你只是一闪念但却诉诸了行动,都成就了今天的你。既然有缘来到这个世界,既然有缘来到这个地方,有缘认识这些人,那就努力的生活吧!一个人的努力固然重要,最终能不能成功还是要靠运气。然而只有努力的生活,一个人才有可能接住那或好或坏的运气。

有大腿傍是好事也是坏事。失去可以依靠的大腿,是坏事也是好事。人若是一辈子都不曾独立的活过,那该是多么苦痛的悲哀。

最后感叹一句,人啊,还是年轻的时候好看!

《自卑与卓越》书摘

作者【奥】阿弗雷德·阿德勒

2013年读了这本书,这是当时的书摘。存这里备个份。

2013-03-30
人生的意义即“对整体作出贡献”。

2013-04-01
人的目标一旦建立,随后便会开始自我管理。

2013-04-01
人生的真谛就在于奉献与合作。

2013-04-01
他们对于人生的理解已经表现在他们的行为之中,如果他们不对自己的思想加以改变,行为自然也不会改变。

2013-04-01
正因为无人可以理解他们的痛苦,所以他们总是越来越自我。

…more