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?
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.
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.
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.
declare @tab table(FirstName varchar(100))
insert into @tab values('John'),('Sarah'),('George')
SELECT *
FROM @tab
WHERE 'John' in (FirstName)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With