postgresql Postgres函数创建-错误:没有指定语言SQL状态:42 P13

sy5wg1nm  于 5个月前  发布在  PostgreSQL
关注(0)|答案(3)|浏览(64)

我是新的功能。我试图通过以下链接创建一个函数-http://www.postgresqltutorial.com/creating-first-trigger-postgresql/。但它给出了一些错误。代码块和错误如下所示。
代码块:

CREATE OR REPLACE FUNCTION log_last_name_changes()
  RETURNS trigger AS
$BODY$
BEGIN
 IF NEW.last_name <> OLD.last_name THEN
 INSERT INTO employee_audits(employee_id,last_name,changed_on)
 VALUES(OLD.id,OLD.last_name,now());
 END IF;
 
 RETURN NEW;
END;
$BODY$

字符串
错误:
错误:未指定语言
SQL状态:42 P13
接下来我可以尝试什么?

qij5mzcb

qij5mzcb1#

试试这个方法:

CREATE OR REPLACE FUNCTION log_last_name_changes()
RETURNS trigger AS
$BODY$
BEGIN
IF NEW.last_name <> OLD.last_name THEN
INSERT INTO employee_audits(employee_id,last_name,changed_on)
VALUES(OLD.id,OLD.last_name,now());
END IF;

RETURN NEW;
END;
$BODY$

LANGUAGE plpgsql VOLATILE -- Says the function is implemented in the plpgsql language; VOLATILE says the function has side effects.
COST 100; -- Estimated execution cost of the function.

字符串

ccrfmcuu

ccrfmcuu2#

如果到达这里,因为你的function给了你同样的错误(像我一样)..这里有一个过度烘焙的例子,使用一个函数来更新右下角的tweet“blurb”与“hello world”。

重要提示:语言在最后一个空格之前(见下文)。这在可接受的解决方案中很容易被忽略。

| ID(序列号)|pub_id(text)|tweet(文本)|
| --|--|--|
| 1 |ABC| Hello World|
| 2 |def| blurb|

-- Optional drop if replace fails below.
drop function if exists sync_tweets(text, text);

create or replace function sync_tweets(
    src_pub_id text, -- function arguments
    dst_pub_id text
) returns setof tweets as -- i.e. rows. int, text work too
$$
declare
    src_id    int; -- temp function variables (not args)
    dest_id   int;
    src_tweet text;
begin
    -- query result into a temp variable
    src_id := (select id from tweets where pub_id = src_pub_id);

    -- query result into a temp variable (another way)
    select tweet into src_tweet from tweets where id = src_id;

    dest_id := (select id from tweets where pub_id = dst_pub_id);
    update tweets set tweet=src_tweet where id = dest_id;

    return query -- i.e. rows, return 0 with return int above works too
        select * from tweets where pub_id in (src_pub_id, dst_pub_id);
end
$$ language plpgsql; -- need the language to avoid ERROR 42P13

-- Run it!
select * from sync_tweets('abc', 'def');

/*
  Outputs
   __________________________________________________ 
  |  id (serial)  |  pub_id (text)  |  tweet (text)  |
  |---------------|-----------------|----------------|
  |  1            |  abc            |  hello world   |
  |  2            |  def            |  blurb         |
  --------------------------------------------------
*/

字符串

3j86kqsm

3j86kqsm3#

下面是同样的错误:
错误:未指定语言
当我创建PL/pgSQL function时,如下所示:

CREATE FUNCTION my_func() RETURNS INTEGER AS $$
BEGIN
RETURN 2;
END;
$$;

字符串
所以,我设置LANGUAGE plpgsql如下所示,然后错误就解决了:

CREATE FUNCTION my_func() RETURNS INTEGER AS $$
BEGIN
RETURN 2;
END; -- ↓ Here ↓
$$ LANGUAGE plpgsql;


或者:

CREATE FUNCTION my_func() RETURNS INTEGER LANGUAGE plpgsql AS $$
BEGIN                                    -- ↑ ↑ Here ↑ ↑
RETURN 2;
END;
$$;

相关问题