I have two variables, 1 varchar named cust_ref
, and 1 int named associated_ids
. What I'm trying to accomplish is the following:
You provide cust_ref
with a value which will usually result in between 1+ rows being returned from the Customer
table. I am concerned with gathering all customer_id
records for that cust_ref
and storing them in the associated_ids
variable seperated by commars.
This is the SQL I have so far, and obviously is only loading one of the customer_id
records into the variable. Based on this example I would like select @associated_ids
to return the following 75458,77397,94955
declare @cust_ref varchar(20) = 'ABGR55532'
declare @associated_ids int
select distinct @associated_ids = customer_id
from dbo.Customer
where cust_ref = @cust_ref
select @associated_ids
select *
from dbo.Customer
where cust_ref = @cust_ref
Here is the results from the above, as you can see there are actually 3 associated_ids
that I need stored in the variable in this example but my command is capturing the largest, I want all 3 seperated by commars.