0

so I have this currently

SET @title = (SELECT title FROM fnBorrowerBooks(@name, 'G3'))
SET @dateBorrowed = (SELECT DateBorrowed FROM fnBorrowerBooks(@name, 'G3'))
SET @dateReturned = (SELECT DateReturned FROM fnBorrowerBooks(@name, 'G3'))

but basically what I would need is something like

SET @variable 1 = (SELECT * FROM fnBorrowerBooks(@name, @Genre)

But this returns 3 columns and CAN return multiple ROWS which obviously won't work because you can't assign multiple values, I need to print how many results I get so if I get 3 different results I need to print all 3 as in 1 string if that makes sense so

PRINT CONCAT(@variable1, ' ble ble ble') should return me multiple lines depending on how many results there are, and I was unable to find a way on how to achieve this.

SiwyJ
  • 29
  • 6

2 Answers2

1

If you only need the titles then you can concatenate within your query:

DECLARE @Title NVARCHAR(3000) 

SELECT 
    @Title = COALESCE(@Title + ', ', '') + title
FROM 
    dbo.fnBorrowerBooks(@name, @genre)


SELECT @title
Ross Bush
  • 14,648
  • 2
  • 32
  • 55
0

You could try using a temp table and a loop, like this:

CREATE TABLE #t
(tKey int  IDENTITY(1,1) PRIMARY KEY,
 Column1 varchar(100),
 Column2 varchar(100),
 ...)

INSERT INTO #t
SELECT * --the columns you select here need to be in the column list above
FROM fnBorrowerBooks(@name, @Genre)

DECLARE @Count int
DECLARE @MyString varchar(max)

SET @Count = 1
SET @MyString = ''

WHILE @Count <= (SELECT MAX(Key) FROM #t)
BEGIN
    SET @MyString += (SELECT Column1 + ', ' + Column2 + ... FROM #t WHERE tKey = @Count)
    SET @Count += 1
END

PRINT(@MyString)
kjmerf
  • 4,275
  • 3
  • 21
  • 29