Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Actually, you <strong>cannot</strong> use <code>FOUND</code> with <code>EXECUTE</code>. <a href="http://www.postgresql.org/docs/current/interactive/plpgsql-statements.html#PLPGSQL-STATEMENTS-DIAGNOSTICS" rel="nofollow noreferrer">The manual</a>:</p> <blockquote> <p>Note in particular that <code>EXECUTE</code> changes the output of <code>GET DIAGNOSTICS</code>, but does not change <code>FOUND</code>.</p> </blockquote> <p>There are a couple of other things that might be improved. First of all, your original is open to SQL injection. I suggest:</p> <pre><code>CREATE OR REPLACE FUNCTION my_schema.sp_delete_row_table(table_name regclass , id_column text , id_value int , OUT del_ct int) AS $func$ BEGIN EXECUTE format ('DELETE FROM %s WHERE %I = $1', table_name, id_column); USING id_value; -- assuming integer columns GET DIAGNOSTICS del_ct = ROW_COUNT; -- directly assign OUT parameter EXCEPTION WHEN OTHERS THEN del_ct := 0; END $func$ LANGUAGE plpgsql; </code></pre> <p><a href="http://www.postgresql.org/docs/current/interactive/functions-string.html#FUNCTIONS-STRING-OTHER" rel="nofollow noreferrer"><code>format()</code></a> requires Postgres 9.1 or later. You can replace it with string concatenation, but be sure to use escape the column name properly with <a href="https://stackoverflow.com/questions/10705616/table-name-as-a-postgresql-function-parameter/10711349#10711349"><code>quote_ident()</code></a>!<br> The rest works for 8.4 as well.</p> <p>Closely related answers: </p> <ul> <li><a href="https://stackoverflow.com/questions/8449011/dynamic-sql-execute-as-condition-for-if-statement/8449341#8449341">Dynamic SQL (EXECUTE) as condition for IF statement</a> </li> <li><a href="https://stackoverflow.com/questions/10705616/table-name-as-a-postgresql-function-parameter/10711349#10711349">Table name as a PostgreSQL function parameter</a></li> </ul>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload