How to perform an "IF" by SQL

Asked

Viewed 120 times

1

Hello, how can I do something similar to an if but by sql?
For example: There is a field in the database called dt_ini with no value so far.
I’d like to do, "If dt_ini == ' ' Insert into table (dt_ini) values ..."
Would have to make a query in the database to see the dt_ini is or has the null value if yes Insert into... else ...

  • Ever tried to make a insert-select?

  • I didn’t quite understand the purpose, but the condition is made by the case when: https://dev.mysql.com/doc/refman/5.7/en/case.html

  • 2

    If no SQL? Sorry if I’m talking nonsense, but a Where does not answer you?

  • Where... well thought out, I believe it suits me yes

2 answers

-1

Opa Mrvandaime, try to do the following,

INSERT INTO SUA_TABELA (dt_ini) VALUES WORTH WHERE DT_INI IS NULL;

OR

INSERT INTO SUA_TABELA (dt_ini) VALUES WORTH WHERE DT_INI = "";

  • This syntax is valid in MySQL? I’ve only seen at most insert-select with clause where

  • 1

    I do not think so: https://stackoverflow.com/questions/485039/mysql-insert-where-query

-1

Let me get this straight, do you want to update the dt_ini value of the table itself? if empty?

If so, try this:

UPDATE SUA_TABELA SET DT_INI = VALOR WHERE DT_INI IS NULL

If you are an Insert elsewhere, if dt_ini is empty, try:

INSERT INTO SUA_TABELA (CAMPOS) VALUES (VALORES) WHERE 1 IN (SELECT 1 FROM SUA_TABELA WHERE DT_INI IS NULL)
  • 1

    Have you tested this INSERT? I’ve never seen this syntax or found it in the documentation.]

  • Yes, I’ve used it a few times. In the documentation you’ll find talking about IN compared to another select. In this case, if select brings result, this will be the Insert trigger.

Browser other questions tagged

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