1
Well my doubt is this, I’m beginner in sql serves, i am running a trial and in it using the CASE method
the following code shows what I am doing:
ALTER PROCEDURE [dbo].[STP_S_Cliente_Busca](
@Busca varchar(255),
@Opao varchar(255)
)
AS
BEGIN
SELECT
*
FROM
tb_cliente
WHERE
CASE
WHEN @Opao = 'NOME' THEN Nome LIKE '%'+ @Busca +'%'
WHEN @Opao = 'STATUS' THEN Status_Cliente LIKE '%'+@Busca+'%'
WHEN @Opao = 'CNPJ' THEN CNPJ LIKE '%'+@Busca+'%'
WHEN @Opao = 'EMAIL' THEN Email LIKE '%'+@Busca+'%'
WHEN @Opao = 'TELEFONE' THEN Whatsapp LIKE '%'+@Busca+'%'
END
ORDER BY
Nome DESC
END
But for some reason does not run, I’ve done this way in mysql and it worked normally, I believe that in sql server have some different detail, but from what I saw in searches done on the internet the case seems right. Am I executing the rule correctly? Or is it really wrong?
I really want to understand what’s wrong.
I tested this way, it seems to work when the values are int for example the status, but when I tried to do a search for Name returned me an unexpected error, where it says: Failed to convert the varchar value 'TEST 1' to the type of data int. tried to put a CONVERT VARCHAR plus error persists
– Jéssica Martins
I like your solution, Silvio.
– José Diz
Jessica, I think converting to sweep after Then should solve, but if so I think it would be better to go for something like Jose Diz suggested, or maybe use separate Cases for int and varchar columns.
– imex
Valeu José Diz.
– imex