2
DECLARE @Companies_List char(25)
SET @Companies_List = ('Amazon','Google','Facebook')

select * 
from companies
where name in @Companies_List

I'm new to SQL Server. I'm trying to create a global set containing the values that I want to use in the SQL where clause, but apparently the above script wouldn't work. Any idea how I should do it? Many thanks!

1
  • The table variable answer is a good idea. The table definition matches the variable you declared as char(25). I would recommend using varchar(25) instead. Commented Oct 6, 2015 at 18:23

1 Answer 1

3

I would create a TABLE variable and do a JOIN to it for a list like this:

DECLARE @Companies_List TABLE 
(
    Name Char(25)
)

INSERT  @Companies_List (Name)
VALUES  ('Amazon'), ('Google'), ('Facebook')

SELECT  C.*
FROM    Companies       C
JOIN    @Companies_List CL  ON  C.Name = CL.Name

If you want something more concrete, you should make a physical TABLE.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.