Re: execute if statement - Mailing list pgsql-general

From Peter Kroon
Subject Re: execute if statement
Date
Msg-id CAOh+DOkeYvuj70XssBg9ALKpmuCfmVNv3xM48-6MOZm55vN=ew@mail.gmail.com
Whole thread Raw
In response to Re: execute if statement  (Peter Kroon <plakroon@gmail.com>)
Responses Re: execute if statement  (John R Pierce <pierce@hogranch.com>)
List pgsql-general
To give a better idea:

DO $$
DECLARE v_string text = 'raise notice ''%'', ''this could be sql with if statement...'';';
BEGIN
if 1=1 then
raise notice '%', ' first notice has been raised...';
end if;
EXECUTE v_string;--this fails
END;
$$
LANGUAGE plpgsql;

I do not wish to create a function for each query I have.


2012/12/1 Peter Kroon <plakroon@gmail.com>
Mmmmm.......

How do I execute dynamic sql that starts with an if statement.
I'm converting mssql code to pgsql.


2012/12/1 Raymond O'Donnell <rod@iol.ie>
On 01/12/2012 19:11, Peter Kroon wrote:
> How can I achieve this?
>
> EXECUTE '
>
> if 1=1 then
> raise notice ''%'', ''notice has been raised...'';
> end if;
>
> ';

I think EXECUTE used like this is available only in a function:

  create or replace function my_function()
  returns void
  as
  $$
  begin
    execute ....
  end;
  $$
  language plpgsql;

In "ordinary" SQL, EXECUTE executes a prepared statement.

Ray.


--
Raymond O'Donnell :: Galway :: Ireland
rod@iol.ie


pgsql-general by date:

Previous
From: Peter Kroon
Date:
Subject: Re: execute if statement
Next
From: Raymond O'Donnell
Date:
Subject: Re: execute if statement