如何循环SQL查询

时间:2017-08-10 03:31:20

标签: sql sql-server loops

我有下面的代码,它只返回数据透视表中的最高结果,即:

<f_fileNotesDate> <f_fileNotesTime> <f_fileNotesTime> <f_fileNotesCreatedBy>

我想要做的是循环它以便它返回所有行(基于@f_fileNotesDate进行猜测,所以当有一个日期返回该行时它将是:

<f_fileNotesDate> <f_fileNotesTime> <f_fileNotesTime> <f_fileNotesCreatedBy>
<f_fileNotesDate> <f_fileNotesTime> <f_fileNotesTime> <f_fileNotesCreatedBy>
<f_fileNotesDate> <f_fileNotesTime> <f_fileNotesTime> <f_fileNotesCreatedBy>

但是我尝试过的所有内容都失败了我的启动代码:

DECLARE @f_fileNotesDate DATE, @f_fileNotesTime TIME, @f_fileNotesDesc 
NVARCHAR(100), @f_fileNotesCreatedBy NVARCHAR(25)
SELECT 

ISNULL(f_fileNotesDate, '') as f_fileNotesDate, 
ISNULL(f_fileNotesTime, '') as f_fileNotesTime, 
ISNULL(f_fileNotesDesc, '') as f_fileNotesDesc,
ISNULL(f_fileNotesCreatedBy, '') as f_fileNotesCreatedBy

FROM (
SELECT field_value, field_name
FROM [RWADMIN].[RV_ACTIVITY_FIELDS] WHERE activity_id = ${ID_LIST}
) d 

pivot (
MAX(field_value)
FOR field_name in (f_fileNotesDate, f_fileNotesTime, f_fileNotesDesc, 
f_fileNotesCreatedBy)
) piv;

0 个答案:

没有答案