I’m using SQL Server 2005 and have the following T-SQL statement:
DECLARE
@MP VARCHAR(500)
SELECT
@MP = COALESCE(@MP + ',','') + [Name] + ',' + '(' + [Political Party] + ')'
FROM [MPs]
WHERE [MPs].[Region] ='Wales'
UPDATE myTable
SET [Names and parties] =
(SELECT @MP
WHERE myTable.[Local Region] ='Wales')
This works fine and will populate myTable with @MP where ‘Wales’ is present; however if I run the statement again, this time with say ‘Scotland’, all the previously updated entries for ‘Wales’ will then become NULL in myTable.
I think I’m missing something here – using a different variable name for @MP for the second search doesn’t work.
I think you’re probably looking for this: