Conditional to execute sql statement

Asked

Viewed 57 times

-1

In SQL Server I can use a 'if' to check if a table has a column and depending on the return of that logical function, execute or not an instruction, as an example below:

IF COL_LENGTH('MINHA_TABELA', 'MINHA_COLUNA') IS NULL
  BEGIN
    PRINT N'ALTER MINHA_TABELA RENAME COLUMN MINHA_COLUNA'
    ALTER TABLE [dbo].[MINHA_TABELA] ADD [MINHA_COLUNA] [INT] NULL;
  END
GO

I tried to find something similar for Postgres and I couldn’t, someone knows how to do something similar in this kind of bank?

1 answer

1


The same way. Postgres accepts, for example:

IF ... THEN

IF ... THEN ... ELSE

IF ... THEN ... ELSIF ... THEN ... ELSE

An example of If is:

IF parentid IS NULL OR parentid = ''
THEN
    RETURN fullname;
ELSE
    RETURN hp_true_filename(parentid) || '/' || fullname;
END IF;

Click here to read the documentation. It has all kinds of possible examples.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.