我有这个临时表定义:
CREATE TABLE #tmpAbsences
(
idCol int IDENTITY (1,1)
, ClientID int
, FROMDate datetime
, ToDate datetime
, NoOfDays int
, NoQualDaysForSSP int
, AveWeeklyEarnings_Override float
, AveWeeklyEarnings_Paid float
, QualifyingDaysInWeek smallint
, ActualQualifyingDays smallint
, Weeks decimal
, TotalWeeksINTmpAbsence decimal
, Linked int
, DateCreated datetime
, SSPDays int
, Location varchar
, SSPPaid float
)
SET IDENTITY_INSERT #tmpAbsences ON
INSERT #tmpAbsences
EXEC sprc_EXPORT_SSP1_LoadtmpAbsencesTable @ClientId
和以下存储过程定义来填充临时表:
CREATE PROC sprc_EXPORT_SSP1_LoadtmpAbsencesTable
@ClientId int
AS
BEGIN
--DECLARE @ClientID int=9814
SELECT
IDENTITY(INT) AS idCol,
@ClientID AS ClientID
--, AB.AbsenceID
, AB.FROMDate
, AB.ToDate
, AB.NoOfDays
, AB.NoQualDaysForSSP
, AB.AveWeeklyEarnings_Override
, AB.AveWeeklyEarnings_Paid
, SSP.QualifyingDaysInWeek
, SSP.ActualQualifyingDays
, CAST(CAST(SSP.ActualQualifyingDays AS DECIMAL(10,2))/CAST(SSP.QualifyingDaysInWeek AS DECIMAL(10,2)) AS DECIMAL(10,2)) AS Weeks
, SUM(CAST(CAST(SSP.ActualQualifyingDays AS DECIMAL(10,2))/CAST(SSP.QualifyingDaysInWeek AS DECIMAL(10,2)) AS DECIMAL(10,2))) OVER(ORDER BY FROMDate ASC) AS TotalWeeksINTmpAbsence
, (CASE WHEN (SELECT COUNT(AB2.ClientId) FROM t_Absence AS AB2 WITH (NOLOCK)
LEFT JOIN t_AbsenceCategory AS ABR2 WITH (NOLOCK) ON (AB2.AbsenceCategoryID = ABR2.AbsenceCategoryID) AND DATEDIFF(DAY, AB2.ToDate, AB.FROMDate) - 1 >= 0 -- Precedes this absence
WHERE AB2.ClientId = @ClientID
AND (DATEDIFF(DAY, AB2.ToDate, AB.FROMDate) - 1 <= 8 * 7) -- but not by more that 8 weeks
AND ABR2.IsSSP > 0 AND ABR.IsSSP > 0
AND (DATEDIFF(DAY, AB2.FROMDate, AB2.ToDate) >= 3
or ABS(DATEDIFF(DAY, AB2.ToDate, AB.FROMDate)) - ISNULL(dbo.fn_GetTotalNonWorkingDays(@ClientID,AB2.ToDate, AB.FROMDate),0) = 1)
) > 0
AND dbo.fn_IsPIW(AB.AbsenceID, 4) = 1 -- this will take care of checking for continous days AND qualifying days
THEN 1 ELSE 0 END) AS 'Linked'
, AB.DateCreated
, SSPDays
, (SELECT Descn FROM t_Locations WITH (NOLOCK) WHERE LocationId = dbo.fn_CheckClientHistory(@ClientID,14,AB.FROMDate)) AS 'Location'
, ISNULL((SELECT SUM(SSPPaid) FROM t_PayrollSSPEmployeePaid AS EP WITH (NOLOCK) WHERE EP.AbsenceID = AB.AbsenceID),0) AS 'SSPPaid'
FROM
t_Absence AS AB WITH (NOLOCK)
LEFT JOIN
t_AbsenceCategory AS ABR WITH (NOLOCK) ON (AB.AbsenceCategoryID = ABR.AbsenceCategoryID)
INNER JOIN
t_PayrollSSPEmployeePaid SSP ON AB.AbsenceID = SSP.AbsenceID
WHERE
ClientID = @ClientID
AND SSP.ActualQualifyingDays>0
ORDER BY
AB.ToDate DESC
END
但我无法让它正常运作......
要么我收到错误,因为IDENTITY函数只能与SELECT...INTO
一起使用,或者我错过了它无法加载正确的数据......
我哪里错了?
答案 0 :(得分:1)
好的,因为错误说你不能直接在select语句中使用IDENTITY( INT )
。或者,您可以在select语句中使用ROW_NUMBER()函数,如下所示 -
CREATE PROC sprc_EXPORT_SSP1_LoadtmpAbsencesTable
@ClientId int
AS
BEGIN
--DECLARE @ClientID int=9814
;WITH cteAbsencesTable
AS
(
SELECT
ROW_NUMBER() OVER(ORDER BY AB.ToDate DESC) AS idCol,
@ClientID AS ClientID
--, AB.AbsenceID
, AB.FROMDate
, AB.ToDate
, AB.NoOfDays
, AB.NoQualDaysForSSP
, AB.AveWeeklyEarnings_Override
, AB.AveWeeklyEarnings_Paid
, SSP.QualifyingDaysInWeek
, SSP.ActualQualifyingDays
, CAST(CAST(SSP.ActualQualifyingDays AS DECIMAL(10,2))/CAST(SSP.QualifyingDaysInWeek AS DECIMAL(10,2)) AS DECIMAL(10,2)) AS Weeks
, SUM(CAST(CAST(SSP.ActualQualifyingDays AS DECIMAL(10,2))/CAST(SSP.QualifyingDaysInWeek AS DECIMAL(10,2)) AS DECIMAL(10,2))) OVER(ORDER BY FROMDate ASC) AS TotalWeeksINTmpAbsence
, (CASE WHEN (SELECT COUNT(AB2.ClientId) FROM t_Absence AS AB2 WITH (NOLOCK)
LEFT JOIN t_AbsenceCategory AS ABR2 WITH (NOLOCK) ON (AB2.AbsenceCategoryID = ABR2.AbsenceCategoryID) AND DATEDIFF(DAY, AB2.ToDate, AB.FROMDate) - 1 >= 0 -- Precedes this absence
WHERE AB2.ClientId = @ClientID
AND (DATEDIFF(DAY, AB2.ToDate, AB.FROMDate) - 1 <= 8 * 7) -- but not by more that 8 weeks
AND ABR2.IsSSP > 0 AND ABR.IsSSP > 0
AND (DATEDIFF(DAY, AB2.FROMDate, AB2.ToDate) >= 3
or ABS(DATEDIFF(DAY, AB2.ToDate, AB.FROMDate)) - ISNULL(dbo.fn_GetTotalNonWorkingDays(@ClientID,AB2.ToDate, AB.FROMDate),0) = 1)
) > 0
AND dbo.fn_IsPIW(AB.AbsenceID, 4) = 1 -- this will take care of checking for continous days AND qualifying days
THEN 1 ELSE 0 END) AS 'Linked'
, AB.DateCreated
, SSPDays
, (SELECT Descn FROM t_Locations WITH (NOLOCK) WHERE LocationId = dbo.fn_CheckClientHistory(@ClientID,14,AB.FROMDate)) AS 'Location'
, ISNULL((SELECT SUM(SSPPaid) FROM t_PayrollSSPEmployeePaid AS EP WITH (NOLOCK) WHERE EP.AbsenceID = AB.AbsenceID),0) AS 'SSPPaid'
FROM
t_Absence AS AB WITH (NOLOCK)
LEFT JOIN
t_AbsenceCategory AS ABR WITH (NOLOCK) ON (AB.AbsenceCategoryID = ABR.AbsenceCategoryID)
INNER JOIN
t_PayrollSSPEmployeePaid SSP ON AB.AbsenceID = SSP.AbsenceID
WHERE
ClientID = @ClientID
AND SSP.ActualQualifyingDays>0
)
SELECT * FROM cteAbsencesTable
ORDER BY idCol
END