Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Set variable value to array of strings

Tags:

I want to set a variable as a string of values. E.g.

declare @FirstName char(100)
select @FirstName = 'John','Sarah','George'

SELECT * 
FROM Accounts
WHERE FirstName in (@FirstName)

I'm getting a syntax error in the line select @FirstName = 'John','Sarah','George':

Incorrect syntax near ','

Is there any way I can set the variable with many values?

like image 630
HL8 Avatar asked Dec 01 '11 23:12

HL8


People also ask

How do you assign a variable to a string value?

How to create a string and assign it to a variable. To create a string, put the sequence of characters inside either single quotes, double quotes, or triple quotes and then assign it to a variable.

How do you set an array of values in SQL?

You need to use the ARRAY_AGG function to create an array that is the intermediate result of a SELECT statement, and then retrieve the contents of that array into an SQL array variable or parameter. For example: -- INTB IS AN OUT PARAMETER OF ORDINARY ARRAY TYPE INTARRAY. -- COL2 IS AN INTEGER COLUMN.

How do you assign an element to an array?

Assigning values to an element in an array is similar to assigning values to scalar variables. Simply reference an individual element of an array using the array name and the index inside parentheses, then use the assignment operator (=) followed by a value.


1 Answers

declare  @tab table(FirstName  varchar(100))
insert into @tab   values('John'),('Sarah'),('George')

SELECT * 
FROM @tab
WHERE 'John' in (FirstName)
like image 93
code save Avatar answered Oct 14 '22 21:10

code save