How to insert multiple email addresses within one variable, in SQL?

Asked

Viewed 42 times

0

For example:

If instead of just typing an email address in the variable below, I typed 30 email addresses, which I would have to change in my query below?

DECLARE @email as NVARCHAR
SET @email = '[email protected]'
 
Select * FROM Padarias WHERE email = @email
  • 1

    I don’t think it’s a good idea to work with multi-valued attributes. Rethink your strategy.

  • 1

    From what I understand https://stackoverflow.com/questions/21160456/sql-server-procedure-declare-a-list

  • Thank you Motta, the solution presented in this link serves my case! A strong.

1 answer

0

If you are using from version 2016 onwards SQL Server, just use the native function string_split. See below:

DECLARE @email as NVARCHAR(max)
SET @email = '[email protected],[email protected],[email protected],[email protected]@padaria.com'--e-mails de exemplo

Select * 
FROM Padarias 
WHERE email in(select value
                from string_split(@email, ',')--> O delimitador deve ser o mesmo usado para separar os e-mails inseridos na variável
                )

Browser other questions tagged

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