I have a table Depots that looks like this:
| DepotID | DepotName | DepotLocation | DepAlias |
|---|---|---|---|
| 1 | Ouland | Utsacity | Oula |
| 2 | Ingri | Utsacity | Inglas |
| 3 | Turks | Utsacity | Turku |
| 4 | tamps | Uusimaa | Tampere |
| 5 | Kokos | Uusimaa | Kokoola |
| 6 | Kaus | Olomba | Kaukana |
I stored a comma-separated list of columns in a declared variable @ValList
DECLARE @ValList varchar(8000);
SET @ValList = NULL
SELECT @ValList = COALESCE(@ValList + ', ','') + ColumnName
FROM #list
@ValList returns DepotID, DepotName, DepLocation
I want to pass @ValList into a select statement like below
SELECT @ValList FROM Depots
So that I get
| DepotID | DepotName | DepotLocation |
|---|---|---|
| 1 | Ouland | Utsacity |
| 2 | Ingri | Utsacity |
| 3 | Turks | Utsacity |
| 4 | tamps | Uusimaa |
| 5 | Kokos | Uusimaa |
| 6 | Kaus | Olomba |
But I keep getting something like
| (No column name) |
|---|
| DepotID, DepotName, DepLocation |
| DepotID, DepotName, DepLocation |
| DepotID, DepotName, DepLocation |
| DepotID, DepotName, DepLocation |
| DepotID, DepotName, DepLocation |
| DepotID, DepotName, DepLocation |
What am I doing wrong?