'How to update multiple comma seperated values in a single column in sql

Question on SQL

Suppose there is a table.

sql


Solution 1:[1]

I can't reproduce your syntax error with the information you have provided so I suspect you have mistyped something somewhere.

However, see the comments - this is the wrong way to store your data. Perhaps these code snippets will help.

You need a table to contain the Team and a table to contain the People. You then need a separate table to link the two together.

create table #Teams (TeamId int identity(1,1), TeamName nvarchar(50));
create table #Members (MemberId int identity(1,1), MemberName nvarchar(50));
create table #TeamMembers (MemberId int, TeamId int);

E.g.

-- create your team first
insert into #Teams (TeamName) values ('Warriors');

-- create your people next
insert into #Members (MemberName) values
('John'),('Alexa'),('Tony');

-- Now (and only now) link members to teams
insert into #TeamMembers (MemberId, TeamId) values
(1, 1),(2,1),(3,1)

To get your data all reported together start with these joins

select t.TeamName, m.MemberName
from #Teams t
join #TeamMembers tm on t.TeamId = tm.TeamId
join #Members m on tm.MemberId = m.MemberId;

Things you may need to do your own research for:

  • One to Many, Many to Many relationships
  • Database normalisation
  • If you really want a comma separated list then "sql generate comma separated list"

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 CHill60