I am attempting to return a list of all image names as a single result from an MSSQL CURSOR with the following stored procedure:
PROCEDURE [dbo].[GetPhotosByPropId]
@propId bigint
AS
DECLARE @AllPhotos nvarchar(MAX)
DECLARE @PhotoName nvarchar(500)
DECLARE @BaseURL nvarchar(200)
DECLARE @PhotosCursor CURSOR
DECLARE @PhotosCount int
-- Take Photo Count
SET @PhotosCount = (SELECT Count(*) FROM [ART].[dbo].[STR_FK_PropertyPhoto] Where PropId = @propId)
-- Take all photos in Cursor
SET @PhotosCursor = CURSOR FAST_Forward For
SELECT [Photo_File_Name],[Base_URL]
FROM [ART].[dbo].[STR_FK_PropertyPhoto]
WHERE PropId = @propId
-- OPEN CURSOR for first record
OPEN @PhotosCursor
FETCH NEXT FROM @PhotosCursor
INTO @PhotoName, @BaseURL
WHILE @@FETCH_STATUS = 0
BEGIN
SET @AllPhotos = @AllPhotos + ',' + @PhotoName
FETCH NEXT FROM @PhotosCursor
INTO @PhotoName, @BaseURL
END
select @AllPhotos as "Img"
CLOSE @PhotosCursor
DEALLOCATE @PhotosCursor
Go
The expected result is something like '1.jpg, 2.jpg, 3.jpg'
is returned, but instead the outcome is NULL
.