2013-09-16 20 views
30

我想在PostgreSQL的9.2運行此:如何在PostgreSQL中引發通知?

RAISE NOTICE 'hello, world!'; 

而且服務器說:

Error : ERROR: syntax error at or near "RAISE" 
LINE 1: RAISE NOTICE 'hello, world!' 
     ^

爲什麼?

回答

54

使用匿名code block

DO language plpgsql $$ 
BEGIN 
    RAISE NOTICE 'hello, world!'; 
END 
$$; 

Variables are referenced使用%

RAISE NOTICE '%', variable_name; 
+0

這正是我需要:) – yegor256

+2

爲了使它更短,您可以刪除換行符和*語言plpgsql * – Ruut

+0

@ruut在pg 9.6版本中,我使用的是我經常遇到的錯誤,試圖創建功能,我忘記了指定語言'錯誤:沒有指定語言'也許它以前是默認的? – Davos

0

簡單的例子:

CREATE OR REPLACE FUNCTION test()  
RETURNS TRIGGER AS 
' 
DECLARE 


num int; 

BEGIN 
IF TG_OP = ''INSERT'' THEN 
select count(*) into num from test_table; 
IF num >= 1 THEN 
RAISE WARNING ''Cannot Insert more than one row''; 
RETURN OLD; 
END IF; 
ELSE 
RETURN NEW; 
END IF; 

END; 
' LANGUAGE plpgsql;