35.2. ���� PL/pgSQL ��һЩ��ʾ

��PL/pgSQL ��������һ���÷����Ǽ򵥵�ʹ����ϲ�����ı��༭��������ĺ�����Ȼ��������һ������̨� �� psql װ����Щ����������������ַ����� ��ô�� CREATE OR REPLACE FUNCTION д�����Ǹ������⡣��������Ϳ��������ļ��Ը��º������塣���磺

CREATE OR REPLACE FUNCTION testfunc(integer) RETURNS integer AS $$
    ....
END;
$$ LANGUAGE plpgsql;

������ psql ��ʱ�����������������װ�ػ������������ĺ��������ļ�

\i filename.sql

Ȼ�����Ϸ��� SQL ������Ըú�����

����һ������PL/pgSQL����ĺ÷�������һ�� GUI �����ݿ���ʹ��ߣ�������ʵ���˹������Կ�����ʩ�����֡� ���ֹ����е�һ�־��� pgaccess����Ȼ���������ġ� ��Щ����ͨ���ṩ��һЩ�����õĹ��ܣ��������ݵ����ţ����ؽ��͵��Ժ������򵥵ȡ�

35.2.1. ��������

PL/pgSQL �����Ĵ��붼���� CREATE FUNCTION ����һ���ִ��ı��ķ�ʽ�����ġ� ����������߰�Χ�����ŵij��淽ʽд�ִ��ı�����ô�κκ������ڵĵ����Ŷ�����д˫�ݣ����Ƶ��Ƿ�б��Ҳ����˫�ݡ� ˫�����ŷdz���ζ���ڸ����ӵij����£�������ܻ�����������⣬ ��Ϊ������׷����Լ���Ҫ��������������������š� ���ǽ�������"��Ԫ����Χ"���ִ��ı���д�����塣 ������ Section 4.1.2.2���� ʹ����Ԫ����Χ��ʱ����Ӳ���Ҫ���κ�����д˫�ݣ� ֻ��ҪΪÿ�����Ű�ΧǶ��ѡ��һ����ͬ����Ԫ���Ű�Χ�ָ������ɡ� ���磬�������ôд CREATE FUNCTION ����

CREATE OR REPLACE FUNCTION testfunc(integer) RETURNS integer AS $PROC$
          ....
$PROC$ LANGUAGE plpgsql;

������������������� SQL ������ʹ�õ����Ű�Χ�ı��ִ��� �� $$ �ָ���Щ�����������ִ���װ�� SQL �����Ƭ�ϡ� �������Ҫ�԰��� $$ ���ı��������Ű�Χ�������ʹ�� $Q$ �ȵȡ�

�����ͼ����ʾ���㲻ʹ����Ԫ����Χ��ʱ������д�����š� ������ڰ���Ԫ������֮ǰ�����Ű�Χ�Ĵ���ת����ij�ֿ���������ʽӦ�û����á�

1 ��������

��ʼ/���������壬���磺

CREATE FUNCTION foo() RETURNS integer AS '
	....
' LANGUAGE plpgsql;

�ں������ڲ����κ�λ�ã��ʺŶ������ɶԳ��֡�

2 ��������

���ں������ڵ��ִ��ı������磺

a_output := ''Blah'';
SELECT * FROM users WHERE f_name=''foobar'';

����Ԫ����Χ�ķ������ֻҪд

a_output := 'Blah';
SELECT * FROM users WHERE f_name='foobar';

����������� PL/pgSQL ���������������Ķ�����

4 ��������

������ں���������ִ�������Ҫһ�������ţ����磺

a_output := a_output || '' AND name LIKE ''''foobar'''' AND xyz''

a_output ���ǣ� AND name LIKE 'foobar' AND xyz��

ʹ����Ԫ����Χ�ķ����������д

a_output := a_output || $$ AND name LIKE 'foobar' AND xyz$$

ҪС����������Ԫ��Χ�ķָ���������ֻ�� $$��

6 ��������

���һ���ں���������ִ��еĵ���������ִ�������βǰ�������� ���磺

a_output := a_output || '' AND name LIKE ''''foobar''''''

a_output ��ֵ���ǣ� AND name LIKE 'foobar'��

����Ԫ����Χ�ķ�������Щ��

a_output := a_output || $$ AND name LIKE 'foobar'$$

10 ��������

�������Ҫ���ִ������������������ţ�������һ���� 8 ���ˣ��� ���������������ź͸��ִ������Ľ�β�������ּ� 2 ������ �����ֻ����дһ���������������ĺ�����ʱ���� Example 35-5 ������������Ҫ��ô�ɡ����磺

a_output := a_output || '' if v_'' ||
    referrer_keys.kind || '' like ''''''''''
    || referrer_keys.key_string || ''''''''''
    then return ''''''  || referrer_keys.referrer_type
    || ''''''; end if;'';

a_output ����ֵ���ǣ�

if v_... like ''...'' then return ''...''; end if;

ʹ����Ԫ����Χ�ķ��������

a_output := a_output || $$ if v_$$ || referrer_keys.kind || $$ like '$$
    || referrer_keys.key_string || $$'
    then return '$$  || referrer_keys.referrer_type
    || $$'; end if;$$;

���Ǽ���ֻ��Ҫ�� a_output ��ŵ����ţ� ��Ϊ��ʹ��ǰ���ᱻ�������Ű�Χ��

����һ���������÷�б�����ݺ���������ĵ����ţ�������д˫�ݡ� �������������ᷢ���Լ�д�˺ܶ����� \'\' �����Ķ����� ������ ''''����Щ����Ϊ������Щ����Щ��ȴ����ô����