我正在使用 SQL Server 2005 并具有以下 T-SQL 语句:
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.