如何将此查询结果集合并到变量中以便以后打印出来?

时间:2018-05-22 12:29:08

标签: sql sql-server tsql

所以我现在有这个

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

但基本上我需要的是像

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

但是这会返回3列并且CAN返回多个ROWS,这显然不会起作用,因为你不能分配多个值,我需要打印我得到多少结果所以如果我得到3个不同的结果我需要打印所有3如果有意义那么在1字符串中

PRINT CONCAT(@ variable1,'ble ble ble')应该返回多行,具体取决于有多少结果,而我无法找到如何实现此目的的方法。

2 个答案:

答案 0 :(得分:1)

如果您只需要标题,那么您可以在查询中连接:

<application 
android:label="@string/your_app_name"
>
....

答案 1 :(得分:0)

您可以尝试使用临时表和循环,如下所示:

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)