Return default value

From PostgreSQL wiki

(Difference between revisions)
Jump to: navigation, search
(New page: {{SnippetInfo|return_default_value function|lang=plpgsql}} Author: Emanuel This function return the default value of a column: <source lang="sql"> CREATE FUNCTION ret_def(text,text,text...)
 

Latest revision as of 19:54, 8 October 2010

Snippets

return_default_value function

Works with PostgreSQL

Any version

Written in

plpgsql

Depends on

Nothing

Author: Emanuel


This function return the default value of a column:

CREATE FUNCTION ret_def(text,text,text) RETURNS text AS $$
SELECT 
  COLUMNS.column_default::text
FROM 
  information_schema.COLUMNS
  WHERE table_name = $2
  AND table_schema = $1
  AND column_name = $3
$$ LANGUAGE sql IMUTABLE;

Just call it at this way:

SELECT ret_def('schema','table','column');
Personal tools