SET ROWCOUNT @startRowIndex
SELECT @first_id = id FROM Question q ORDER BY q.id
これはテーブルから最初のIDを取得するのに十分簡単ですが、SQLステートメントが文字列の場合、どうすれば上記と同じ結果を得ることができますか
q.id
から出力を取得する方法を意味しますEXEC sp_executesql @sql
か?
IF @sortCommand like 'sortAsAnswerCount' OR
@sortCommand like 'sortAsNonAnswer'
BEGIN
set @sql=N'WITH LikeCounts
AS
(
SELECT TOP (100) PERCENT q.Id, q.Title, c.CustomerId, c.CreatedOnUtc,
(SELECT COUNT(*)
FROM dbo.CustomerLikeQuestion
WHERE (QuestionId = q.Id)) AS LikeCount
FROM dbo.CustomerContent AS c INNER JOIN
dbo.Question AS q ON c.Id = q.Id
ORDER BY likecount DESC
)
SELECT *, row_number() over (order by likecount desc) as RowNum
fROM LikeCounts'
END