2016-12-07 36 views
2

我正在寫一個PL/PGSQL函數,它會產生一個錯誤。我想打印錯誤RAISE NOTICE聲明,但我不知道如何獲取錯誤代碼?什麼變量保存最後一個錯誤?如何在函數中獲得PL PGSQL錯誤代碼?

這是我的示例代碼:

IF FOUND 
THEN 
    BEGIN 
     insert into app.company(dateinserted,name) values(now(),company_name) returning comnpany_id; 
     return company_id; 
    EXCEPTION 
     WHEN OTHERS THEN 
      RAISE NOTICE 'Insert failed with...'; 
      return -2; 
    END; 
ELSE 
    RETURN -1; 
END IF; 

此代碼將返回COMPANY_ID如果插入成功,如果失敗打印錯誤。

回答

3
... 
EXCEPTION 
    WHEN OTHERS THEN 
     RAISE NOTICE 'Insert failed with error code %', SQLSTATE; 
... 

更多in the documentation.