How to run an ad-hoc script in PostgreSQL?

后端 未结 3 1748
攒了一身酷
攒了一身酷 2020-12-24 05:20

I\'m trying to run this in PostgreSQL 9.2:

RAISE NOTICE \'hello, world!\';

And the server says:

Error : ERROR:  syntax erro         


        
相关标签:
3条回答
  • 2020-12-24 06:05

    Use an anonymous code block:

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

    Variables are referenced using %:

    RAISE NOTICE '%', variable_name;
    
    0 讨论(0)
  • 2020-12-24 06:10

    simple example:

    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;
    
    0 讨论(0)
  • 2020-12-24 06:17

    raise is PL/pgSQL only.

    http://www.postgresql.org/docs/current/static/plpgsql-errors-and-messages.html

    create or replace function r(error_message text) returns void as $$
    begin
        raise notice '%', error_message;
    end;
    $$ language plpgsql;
    
    select r('an error message');
    NOTICE:  an error message
    
    0 讨论(0)
提交回复
热议问题