我在我的存储过程中更改了模式,我遇到了特定临时表的错误:
SQL71502:过程:[LS1]。[Report_ChildPAXData_Summary]有一个 未解决的对象#baggage
的引用
它指向的行在下面:
IF OBJECT_ID('tempdb..#baggage') IS NOT NULL
DROP TABLE #baggage
这是声明问题吗?只是想知道如何解决这个错误,因为我会在其他存储过程中遇到更多临时表。
更新:如评论中所述,发布了整个存储过程:
/*
Executed as part of /Support/ChildPAXList report.
*/
CREATE Procedure [LS1].[Report_ChildPAXData_Summary]
@firstDatetimeToLoad datetime = NULL
,@lastDatetimeToLoad datetime = NULL
AS
BEGIN
IF OBJECT_ID('tempdb..#tempPersonDetail') IS NOT NULL
DROP TABLE #tempPersonDetail
IF OBJECT_ID('tempdb..#baggage') IS NOT NULL
DROP TABLE #baggage
IF OBJECT_ID('tempdb..#FlightsToCheck') IS NOT NULL
DROP TABLE #FlightsToCheck
CREATE TABLE #tempPersonDetail
(
PKCreateDate datetime,
Flight_DepartureDateTime DATETIME,
FlightNumber NVARCHAR(50) ,
Dep_AirportCode CHAR(3),
Arr_AirportCode CHAR(3),
Person_PersonTypeId INT,
person_Name varchar(101), --Forename varchar(50) + '\' char + surname varchar(50)
TDI_DOB DATETIME,
Person_DOB DATETIME,
TDI_Yr INT,
Per_yr INT,
Person_Age INT,
Person_TitleID INT,
adult BIT NULL,
child BIT NULL,
infant BIT NULL,
female BIT NULL,
male BIT NULL,
)
if(@firstDatetimeToLoad is null)
BEGIN
SELECT @firstDatetimeToLoad = DATEADD(HOUR,3 , cast(cast(GETDATE()+1 as date) as datetime))
END
if (@lastDatetimeToLoad is null)
BEGIN
SELECT @lastDatetimeToLoad = DATEADD(HOUR,27,@firstDatetimeToLoad)
END
--stage people detail
INSERT INTO #tempPersonDetail
(PKCreateDate,Flight_DepartureDateTime,FlightNumber,Dep_AirportCode ,Arr_AirportCode,Person_PersonTypeId,person_Name, TDI_DOB, Person_DOB,TDI_Yr,Per_yr, Person_Age, Person_TitleID,infant, female, male)
select PK.createddate
,FLT.DepartureDateTime
,FLT.FlightNumber
,dep.AirportCode
,arr.AirportCode
,per.PersonTypeId
,per.firstname + '/' + per.surname [person_Name]
,TDI.DateOfBirth
,per.DateOfBirth
,CONVERT( int, FLOOR( DATEDIFF( hour, TDI.DateOfBirth, GETDATE() ) / 8766.0 ) ) AS TDI_Yr --age in years based on TDI table
,CONVERT( int, FLOOR( DATEDIFF( hour, per.DateOfBirth, GETDATE() ) / 8766.0 ) ) as Per_yr --age in years based on Person table
,per.age
,per.TitleID
,case when per.personTypeid = 3 then 1 --set as infant if persontypeid = 3
end
,case when per.TitleId in (2,3,4) then 1 --set as female depending on title id
end
,case --set as male depending on title
when per.TitleId not in (2,3,4) then 1
end
from Reservations_Live.dbo.Flight flt
inner join Reservations_Live.dbo.FlightReservation fr (nolock) on fr.FlightId = flt.FlightId --and flt.flightnumber = @FlightNumber
inner join Reservations_Live.dbo.Product p (nolock) on p.ProductId = fr.Productid
inner join Reservations_Live.dbo.ProductPerson pp (nolock) on pp.ProductId = p.productId
and pp.StatusId not in (7,8)
inner join Reservations_Live.dbo.Package pk on p.PackageId = pk.PackageId
inner join Reservations_Live.dbo.Person per (nolock) on per.PersonId = pp.PersonId
inner join Reservations_Live.dbo.FlightSector sec (nolock) on sec.FlightSectorId = flt.FlightSectorID
inner join Reservations_Live.dbo.Airport dep (nolock) on dep.AirportID = sec.DepartureAirportID
inner join Reservations_Live.dbo.Airport arr (nolock) on arr.AirportID = sec.ArrivalAirportID
LEFT OUTER JOIN Reservations_Live.dbo.TravelDocumentInformation TDI ON (TDI.ProductPersonID = pp.ProductPersonId)
--inner join #flightstocheck ftc on cast(flt.departuredatetime as date) = ftc.FlightDepartureDate
-- and flt.flightnumber = ftc.flightnumber
WHERE flt.DepartureDateTime between @firstDatetimeToLoad and @lastDatetimeToLoad
and flt.flightstatusId <> 6 -- 6 -> cancelled flight
--update staged people data to the correct child status
-- children over 11 to be considered as adult for purposes of fuel calculation
update #tempPersonDetail
set child = CASE WHEN Person_PersonTypeId = 2 THEN
CASE
--We first look at DOB in the TDI info
WHEN CONVERT(CHAR(10), ISNULL(TDI_DOB, GETDATE()), 103) = CONVERT(CHAR(10), GETDATE(), 103) THEN
CASE --if there is no DOB we than look at the person age
WHEN ISNULL(Person_Age, 0) = 0 THEN --If the age is not defined
CASE
--This is the case in which we do not have any info
--to base our decision on so we leave the person type unchanged
WHEN CONVERT(CHAR(10), ISNULL(Person_DOB, GETDATE()), 103) = CONVERT(CHAR(10), GETDATE(), 103) THEN 1 --CHILD
ELSE
CASE -- If there is no age defined we than try person DOB
--If the passenger is older than 11 we override the person type to Adult
WHEN CONVERT( int, FLOOR( DATEDIFF( hour, Person_DOB, GETDATE() ) / 8766.0 ) ) > 11 THEN 0 --ADULT
--otherwise we leave it as a child
ELSE 2 --CHILD
END
END
--If the age is defined
WHEN Person_Age> 11 THEN 0 -- ADULT --If the passenger is older than 11 we override the person type to Adult
ELSE 1 --CHILD --otherwise we leave it as a child
END
ELSE --If the DOB is present in TDI
CASE
--If the passenger is older than 11 we override the person type to Adult
WHEN CONVERT( int, FLOOR( DATEDIFF( hour, TDI_DOB, GETDATE() ) / 8766.0 ) ) > 11 THEN 0 --ADULT
--otherwise we leave it as a child
ELSE 1 --CHILD
END
END
END
--If the passenger is not a child we leave it's person type unchanged
--update to be adult if adult person type (1), or child type (2) but not set to true (as over 11 years old)
update #tempPersonDetail
set adult = 1
where Person_PersonTypeId = 1 or ( Person_PersonTypeId = 2 and child <> 1)
--select *, iif((cast(pkcreatedate as date) = cast(person_DOB as date)) and TDI_DOB is null and Person_PersonTypeId = 2, 1, 0 ) [Unreliable] from #tempPersonDetail
--where child=1
--order by flight_departuredatetime, flightnumber;
;with CTE_FlightData
AS
(
select *
,iif(adult = 1 and male = 1, 1, 0 ) [Adult Male]
,iif(adult =1 and female = 1, 1, 0) [Adult Female]
,iif((cast(pkcreatedate as date) = cast(person_DOB as date)) and TDI_DOB is null and Person_PersonTypeId = 2 , 1, 0 ) [Unreliable]
from #tempPersonDetail
)
select
flight_departuredatetime
,flightnumber
,count(1) [Total PAX]
,sum(iif(child = 1, 1,0)) [Children]
,sum(iif(infant =1, 1, 0))[Infants]
,sum([adult male]) [Adult Male]
,sum([adult female]) [Adult Female]
,sum([Unreliable]) [Potential Unreliable Children]
from CTE_FlightData
group by flight_departuredatetime, flightnumber
order by flight_departuredatetime, flightnumber
END
答案 0 :(得分:0)
您可以在临时表上使用表变量。所以你不需要关心删除表,因为它是一个变量:
DECLARE @tempPersonDetail table
(
PKCreateDate datetime,
Flight_DepartureDateTime DATETIME,
FlightNumber NVARCHAR(50) ,
Dep_AirportCode CHAR(3),
Arr_AirportCode CHAR(3),
Person_PersonTypeId INT,
person_Name varchar(101), --Forename varchar(50) + '\' char + surname varchar(50)
TDI_DOB DATETIME,
Person_DOB DATETIME,
TDI_Yr INT,
Per_yr INT,
Person_Age INT,
Person_TitleID INT,
adult BIT NULL,
child BIT NULL,
infant BIT NULL,
female BIT NULL,
male BIT NULL,
)
请参阅difference between a temp table and table variable in SQL Server?的这篇文章。