Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

560
Views
¿Cómo CREAR FUNCIÓN SI NO EXISTE?

¿Hay una manera fácil de hacer CREAR FUNCIÓN SI NO EXISTE ? Tengo varios esquemas y estoy preparando un script que creará objetos que faltan en el esquema de destino. El plan es ejecutar un script para verificar si el objeto existe, no hacer nada si no existe, lo creará. 'CREAR ALGO SI NO EXISTE' funciona perfectamente con secuencias de tablas y otros, sin embargo, no puede encontrar la solución para funciones. Soy del mundo Tsql y tiene esta verificación. Sin embargo, parece que Postgres 9.6 no lo tiene. ¿Hay alguna manera fácil de evitar esta limitación?

over 4 years ago · Santiago Trujillo
3 answers
Answer question

0

Puede envolver definiciones de función con bloque anónimo y manejar la excepción de nombre duplicado:

 create function f(int) returns int language sql as 'select $1';

✓

 do $$ begin create function f (int) returns int language sql as 'select $1'; end; $$
ERROR: la función "f" ya existe con los mismos tipos de argumentos
CONTEXTO: instrucción SQL "crear función f (int)
 devuelve int
 lenguaje sql
 como 'seleccione $1'"
Función PL/pgSQL inline_code_block línea 3 en declaración SQL
do $$ begin create function f (int) returns int language sql as 'select $1'; exception when duplicate_function then null; end; $$

✓

db<>violín aquí

over 4 years ago · Santiago Trujillo Report

0

Puede usar create or replace , pero eso no funciona si cambia la firma de la función.

La otra alternativa es usar drop function if exists seguida de una create function . Solo asegúrese de establecer los privilegios necesarios después de eso nuevamente.

over 4 years ago · Santiago Trujillo Report

0

esto es lo que tengo que hacer

 IF EXISTS (SELECT FROM information_schema.routines WHERE routine_schema = 'schema_name' AND rouitine_name = 'vw_cmp') THEN raise notice 'Routine vw_cmp EXISTS'; ELSE -- create proc SQL END IF;
over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!