Update com If ou Case?
Tem como fazer um Update com If ou Case?
update tabela set
case
when campo = 1 then campo = 20
where codigo = 1
ou
update tabela set
if condicao > 0
campo = 1
where codigo > 20
update tabela set
case
when campo = 1 then campo = 20
where codigo = 1
ou
update tabela set
if condicao > 0
campo = 1
where codigo > 20
Guigosnet
Curtidas 0
Respostas
Fabianosales
01/08/2008
Tem sim.
A sintaxe ANSI (testei somente no firebird, mas deve funcionar para a maioria dos bancos relacionais) é:
A sintaxe ANSI (testei somente no firebird, mas deve funcionar para a maioria dos bancos relacionais) é:
UPDATE TBL SET CODIGO = CASE CONDICAO WHEN 0 THEN NULL WHEN 1 THEN 1000 WHEN 2 THEN 2000 WHEN 3 THEN 3000 WHEN 4 THEN 4000 WHEN 5 THEN 5000 END
GOSTEI 0