Sql server 为什么在派生表的存储过程中使用UNION?
我不是存储过程的作者,我想知道为什么他们在从派生表中选择时在Sql server 为什么在派生表的存储过程中使用UNION?,sql-server,tsql,union,Sql Server,Tsql,Union,我不是存储过程的作者,我想知道为什么他们在从派生表中选择时在SELECT语句中使用UNION 如果我注释掉整个UNION ALL SELECT语句,我会得到基本相同性能的相同结果 所以我只是想知道为什么它会在那里?这是什么把戏? 下面是整个存储过程,以防我丢失某些内容 ALTER PROCEDURE [dbo].[rptActivityLog] --'1/1/2016', '2/3/2016' (@DateFrom datetime = null, @DateTo datetime
SELECT
语句中使用UNION
如果我注释掉整个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
与
tblQuotes
的联接存在差异,看起来该联接旨在联接两个不同的数据集(一个用于QuoteGUIDs
,另一个用于ControlGUIDs
)嗯,与tblQuotes的联接存在差异,看起来该联接旨在联接两个不同的数据集(一个是QuoteGuid,一个是ControlGuid,不管它们的意思是什么…)啊错过了…哦!!!!你是对的!!Dinnt注意到了。谢谢你不客气!好眼力!我期待着发现差异,但仍然没有看到。