A SQL function:
可在<条码>A上操作多篇发言。 条款或BEGIN ATOECA; .END; 声明 获取带有电离器多种参数的多重价值。 rel=“nofollow noreferer”>PL/pgSQL职能: a)
只能有几类参数。
各项参数的具体内容如下:$1
,$2
, ......
可在<条码>、<条码>、<>条码>、<>条码>和<条码>> >。
可在以下网址上向打电话者提交最后发言:.RETURN statement not using AS
条款或使用<条码>BEGIN ATO中等收入国家;......END; 说明。
可在向打电话者作最后发言后再加一份::RETURN statement,不使用AS
条款或使用<条码>BEGIN ATO中等收入国家;......END; 说明。
不能有<条码>BEGIN ......END; 声明,由于存在错误,而PL/pgSQL功能可以有多种说明。 * 注意,<条码>BEGIN END; 声明不是transaction。
不能有DECLARE 条款,而PL/pgSQL的功能则没有出现错误。
不能控制与例如<代码>BEGIN;和COMMIT;
的交易,因为有错误。
如果存在错误,则进行单一交易,则进行滚动支持。
* < rel=“nofollow noreferer” The doc interpret a SOL function in details and my interpret a PL/pgSQL function.
例如,你创建<代码>测试代码>表格如下:
CREATE TABLE test (
num INTEGER
);
然后插入num
>的行文2
。 如下所示:
INSERT INTO test (num) VALUES (2);
现在,你可以创建<条码>附加功能(INTEERT),在<条码>上添加<条码>、<条码>、<条码>和<<条码>对电传机添加<条码>。 * 您必须制定<代码>RETURNS <type>条或OUT
或>。 SlectT num from test;
是收益价值,其类型实际上与> 相匹配。 数值也为INTEERT
,这样,如果类型的tn匹配RETURNS <type>
则有错误,最后,你必须设定<代码>SQL
至>>>>>>LANGUAGE
,以形成一种结构功能:
CREATE FUNCTION addition(value INTEGER) RETURNS INTEGER AS $$
UPDATE test SET num = num + value;
SELECT num FROM test;
$$ LANGUAGE SQL;
或者,你可以使用<条码>,代之以<条码>,从<条码>中选取。
CREATE FUNCTION addition(value INTEGER) RETURNS INTEGER AS $$
UPDATE test SET num = num + value RETURNING num;
$$ LANGUAGE SQL; -- ↑ Here ↑
接着,请在<条码>后插入<条码>+(3)<>>>> > >。
postgres=# SELECT addition(3);
addition
----------
5
(1 row)
postgres=# SELECT num FROM test;
num
-----
5
(1 row)
And, you can use VOID
type to return nothing as shown below:
CREATE FUNCTION addition(value INTEGER) RETURNS VOID AS $$
UPDATE test SET num = num + value; -- ↑ Here
SELECT num FROM test;
$$ LANGUAGE SQL;
Then, you can call addition(3)
with SELECT
statement, then 3
is added to num
and nothing is returned as shown below:
postgres=# SELECT addition(3);
addition
----------
(1 row)
postgres=# SELECT num FROM test;
num
-----
5
(1 row)
并且,你可以回馈一份没有使用<代码>AS的声明的数值。 下述条款。 * 待印发。 条款载于。 错误:
CREATE FUNCTION addition(value INTEGER) RETURNS INTEGER LANGUAGE SQL
RETURN 2 + value;
页: 1 页: 1 如下文所示条款,但仍然是一项结构功能:
CREATE FUNCTION addition(value INTEGER) RETURNS INTEGER /* LANGUAGE SQL */
RETURN 2 + value;
接着,请将<代码>addition(3) with SlectT
statement, 后加3
。
postgres=# SELECT addition(3);
addition
----------
5
(1 row)
And, you can use BEGIN ATOMIC; ... END;
statement in a SQL function as shown below. *In BEGIN ATOMIC; ... END;
statement, you can return a value with SELECT
statement, RETURNING
clause or RETURN
statement.
CREATE FUNCTION addition(value INTEGER) RETURNS INTEGER LANGUAGE SQL
BEGIN ATOMIC; -- Here
UPDATE test SET num = num + value;
SELECT num FROM test;
END; -- Here
Or:
CREATE FUNCTION addition(value INTEGER) RETURNS INTEGER LANGUAGE SQL
BEGIN ATOMIC;
UPDATE test SET num = num + value;
SELECT num INTO value FROM test;
RETURN value
END;
Or:
CREATE FUNCTION addition(value INTEGER) RETURNS INTEGER LANGUAGE SQL
BEGIN ATOMIC;
UPDATE test SET num = num + value RETURNING num;
END;
页: 1 LANGUAGE在使用BEGIN ATOMIC; ......END;
时,如下文所示,它仍然是一项结构功能:
CREATE FUNCTION addition(value INTEGER) RETURNS INTEGER /* LANGUAGE SQL */
BEGIN ATOMIC;
...
END;
Then, you can call addition(3)
with SELECT
statement, then 3
is added to num
and nothing is returned as shown below:
postgres=# SELECT addition(3);
addition
----------
5
(1 row)
postgres=# SELECT num FROM test;
num
-----
5
(1 row)
And, you can use $1
(num1
), $2
(num2
), ... as the aliases of parameters as shown below. *My answer explains the aliases of paremeters and how to declare local variables in detail:
CREATE FUNCTION addition(num1 INTEGER, num2 INTEGER) RETURNS INTEGER AS $$
SELECT $1 + $2;
$$ LANGUAGE SQL;
页: 1
CREATE FUNCTION addition(INTEGER, INTEGER) RETURNS INTEGER AS $$
SELECT $1 + $2;
$$ LANGUAGE SQL;
然后,请打电话addition(2, 3)
with SlectT
statement,然后打电话5<>。
postgres=# SELECT addition(2, 3);
addition
----------
5
(1 row)
Be careful, using BEGIN ... END;
statement in a SQL function gets the error as shown below while a PL/pgSQL function doesn t get error:
CREATE FUNCTION addition(value INTEGER) RETURNS INTEGER AS $$
BEGIN
UPDATE test SET num = num + value;
SELECT num FROM test;
END;
$$ LANGUAGE SQL;
ERROR: syntax error at or near "UPDATE"
并且,如果使用与例如<代码>BEGIN;和COMMIT;
in akou function中的交易,则出现以下错误。 * 包含<代码> VOID的功能。 字体有误,但称职结构功能有误:
CREATE FUNCTION addition(value INTEGER) RETURNS INTEGER AS $$
BEGIN;
UPDATE test SET num = num + value;
SELECT num FROM test;
COMMIT;
$$ LANGUAGE SQL;
ERROR: return type mismatch in function declared to return integer
DETAIL: Function s final statement must be SELECT or INSERT/UPDATE/DELETE RETURNING.
CONTEXT: SQL function "addition"
而且,如果使用<代码>DECLARE,则在一项职能中出现如下错误:
CREATE FUNCTION addition() RETURNS INTEGER AS $$
DECLARE
value INTEGER := 9;
UPDATE test SET num = num + value;
SELECT num FROM test;
$$ LANGUAGE SQL;
ERROR: syntax mis at or near “INTEERT”
此外,您可使用<代码>IN,OUT
和INOUT
参数,其功能如下所示。
。
CREATE FUNCTION addition(IN value INTEGER) RETURNS INTEGER AS $$
UPDATE test SET num = num + value;
SELECT num FROM test;
$$ LANGUAGE SQL;
接着,请在<条码>后插入<条码>+(3)<>>>> > >。
postgres=# SELECT addition(3);
addition
----------
5
(1 row)
postgres=# SELECT num FROM test;
num
-----
5
(1 row)
。 * 无需制定<代码>RETURNS <type>条,如果你设置了OUT/code>,参数 * 如果你制定了<条码>OUT
para amount,你可以确定<条码>RETURNS <type>条:
CREATE FUNCTION addition(OUT value INTEGER) /* RETURNS INTEGER */ AS $$ -- ↑ Here -- ↑ Unset ↑
UPDATE test SET num = num + 3;
SELECT num FROM test;
$$ LANGUAGE SQL;
接着,请在<条码>上添加<>条码>(<>+>>>和<条码>。
postgres=# SELECT addition();
addition
----------
5
(1 row)
postgres=# SELECT num FROM test;
num
-----
5
(1 row)
But, calling addition(3)
gets the error as shown below because an OUT
parameter cannot get a value from the caller as I said above:
postgres=# SELECT addition(3);
ERROR: function addition(integer) does not exist
LINE 1: SELECT addition(3);
^
HINT: No function matches the given name and argument types. You might need to add explicit type casts.
并且,你仍然可以制定<代码>RETURNS <type>、/code>参数的条款,但这些参数的类型必须与以下所示类型相同:
CREATE FUNCTION addition(OUT value INTEGER) RETURNS INTEGER AS $$
UPDATE test SET num = num + 3; -- ↑ Here -- ↑ Here
SELECT num FROM test;
$$ LANGUAGE SQL;
因此,如果其类型不相同,则有以下错误:
CREATE FUNCTION addition(OUT value VOID) RETURNS INTEGER AS $$
UPDATE test SET num = num + 3; -- ↑ Here -- ↑ Here
SELECT num FROM test;
$$ LANGUAGE SQL;
经常资源:由于产出参数,功能结果类型必须无效
。
CREATE FUNCTION addition(INOUT value INTEGER) /* RETURNS INTEGER */ AS $$ -- ↑ Here -- ↑ Unset ↑
UPDATE test SET num = num + value;
SELECT num FROM test;
$$ LANGUAGE SQL;
接着,请在<条码>后插入<条码>+(3)<>>>> > >。
postgres=# SELECT addition(3);
addition
----------
5
(1 row)
postgres=# SELECT num FROM test;
num
-----
5
(1 row)