返回SQL Server存储过程的SELECT UNION中的多个行

时间:2015-02-14 18:03:42

标签: sql sql-server stored-procedures

我有以下存储过程返回大量行。我想将行数限制为50.如何在SQL Server数据库的UNION SELECT查询中执行此操作?

CREATE PROCEDURE [dbo].[SD_photos]
    (@IDuser NVARCHAR(50))
AS
BEGIN
    SET NOCOUNT ON;

    SELECT 
        photoFileName 
    FROM 
        photos 
    WHERE 
        IDuser = @IDuser

    UNION ALL 

    SELECT 
        photoFileName 
    FROM 
        photos2 
    WHERE 
        IDuser = @IDuser 
END

1 个答案:

答案 0 :(得分:1)

这似乎是MS SQL Server,因此您可以使用TOP语法:

SELECT TOP 50 *
FROM
 (
   SELECT photoFileName FROM photos WHERE IDuser = @IDuser
   UNION ALL 
   SELECT photoFileName FROM photos2 WHERE IDuser = @IDuser
 ) dt