为什么UNION在派生表中的存储过程中使用?

时间:2017-04-27 19:12:22

标签: sql-server tsql union

我不是存储过程的作者,我想知道为什么他们在从UNION语句中使用SELECT时从派生表中进行选择...

如果我注释掉整个UNION ALL SELECT语句,我得到的结果基本相同。

所以我只是想知道它为什么存在?它有什么样的伎俩? 下面是整个存储过程,以防我遗漏了什么

ALTER PROCEDURE [dbo].[rptActivityLog] --'1/1/2016', '2/3/2016'
   (@DateFrom datetime = null,
    @DateTo datetime = null,
    @UserGuid uniqueidentifier = null,
    @CurrentUserGuid uniqueidentifier = NULL)
AS
    SET NOCOUNT ON
    SET TRANSACTION ISOLATION LEVEL READ UNCOMMITTED

    DECLARE @UserID SMALLINT

    SELECT @UserID = UserID 
    FROM tblUsers 
    WHERE (UserGUID = @UserGuid)

    DECLARE @ValidOfficeGuids TABLE (OfficeGuid uniqueidentifier primary key)

    --if user is in tblUserQuotingOffice then use only that Office 
    --otherwise they will have access to all offices
    IF EXISTS (SELECT OfficeGuid 
               FROM tblUserQuotingOffice
               WHERE UserGuid = @CurrentUserGuid)  
    BEGIN
        INSERT INTO @ValidOfficeGuids
            SELECT OfficeGuid 
            FROM tblUserQuotingOffice
            WHERE UserGuid = @CurrentUserGuid
    END
    ELSE
    BEGIN
        INSERT INTO @ValidOfficeGuids
            SELECT OfficeGUID 
            FROM tblClientOffices
    END

    DECLARE @compareDateFrom DATETIME 
    set @compareDateFrom = CAST(CONVERT(VARCHAR(50), @DateFrom, 101) AS DATETIME)

    declare @compareDateTo datetime
    set @compareDateTo = DateAdd(ms, -2, DateAdd(d, 1, CAST(CONVERT(VARCHAR(50), DATEADD(day, 7, @DateTo), 101) AS DATETIME)))

    --First get the log entries
    declare @logResults table
    (
        ID int primary key not null
        , IdentifierGuid uniqueidentifier
    )

    insert into @logResults
    select 
            l.ID
            , l.IndentifierGuid
        from 
            tblLog l
        where
            l.ActionDate between @compareDateFrom and @compareDateTo
            and l.IndentifierGuid is not null

    select 
        distinct
            T.UserName
            , T.ControlNo
            , T.InsuredPolicyName
            , Replace(Replace(T.[Action],Char(10),''),Char(13),'') as [Action]
            , T.ActionDate
            , T.LineName as LOB
        from
    (
        select 
                u.UserName
                , q.ControlNo
                , q.InsuredPolicyName
                , l.[Action]
                , l.ActionDate
                , ll.LineName   
                , l.UserID 
            from    
                @logResults r
                inner join tblLog l on r.ID = l.ID      
                inner join tblUsers u on l.UserID = u.UserID
                inner join tblQuotes q on r.IdentifierGuid = q.QuoteGUID
                inner join lstLines ll on q.LineGUID = ll.LineGUID  
--      WHY DO WE USE BELOW UNION  STATEMENT??????????????????????????????????
        union
        select 
                u.UserName
                , q.ControlNo
                , q.InsuredPolicyName
                , l.[Action]
                , l.ActionDate
                , ll.LineName   
                , l.UserID
            from    
                @logResults r
                inner join tblLog l on r.ID = l.ID      
                inner join tblUsers u on l.UserID = u.UserID
                inner join tblQuotes q on r.IdentifierGuid = q.ControlGUID
                inner join lstLines ll on q.LineGUID = ll.LineGUID      
    ) T     
    WHERE IsNull(@UserID, T.UserID) =  T.UserID
    order by            
        T.ActionDate    

1 个答案:

答案 0 :(得分:2)

make的联接存在差异,看起来联合用于联合两个不同的数据集(一个用于tblQuotes,另一个用于QuoteGUIDs