0

Ich habe diese temporäre Tabellendefinition:INSERT in temporäre Tabelle mit Identitätsspalte von Stored Procedure

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 

und die unter Stored Procedure Definition der temporären Tabelle zu füllen:

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 

Aber ich kann nicht bekommen es funktioniert einwandfrei ...

Entweder bekomme ich einen Fehler, weil die IDENTITY-Funktion nur mit einem SELECT...INTO verwendet werden kann oder wenn ich das vermisse es f ails, um die richtigen Daten zu laden ...

Wohin gehe ich falsch?

+2

Nun können Sie 'Row_Number()' Funktion anstelle dieses - 'IDENTITY (INT) AS idCol' in Ihrer SELECT-Anweisung etwas wie folgt - SELECT ROW_NUMBER() OVER (ORDER BY AB.ToDate DESC) Wie idCol, AB.FROMDate, AB.ToDate, .... ' –

Antwort

1

Okay, wie der Fehler sagte, können Sie IDENTITY(INT) nicht direkt in der Select-Anweisung verwenden. Alternativ können Sie die ROW_NUMBER() Funktion in Ihrer select-Anweisung verwenden, so etwas wie diese -

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