很难说出您的代码实际上在做什么,但您提到您的存储过程正在使用游标并且您的存储过程很慢。我编写存储过程以使用临时表,并按照以下示例所示遍历临时表。当我使用游标时,这使我的 procs 在几秒钟到几分钟内运行,而它们过去需要几分钟到几小时。如果您正在使用游标并且分析表明存储过程中的游标代码很慢,请尝试使用临时表。
你永远不会回到游标。
-- create a temporary table
DECLARE @FrontDeskRows table (
id int,
Arrival datetime,
Departure datetime,
CheckedIn int,
OwnerID varchar(50),
GuestID varchar(50),
[LName] varchar (256),
[FName] varchar (256),
[Address] varchar (256),
[City] varchar (256),
[State] varchar (256),
[Zip] varchar (256),
[phone] varchar (256),
[Status] int )
-- load your temporary table
INSERT INTO @FrontDeskRows
SELECT id,
Arrival,
Departure,
CheckedIn,
OwnerID,
GuestID,
[LName],
[FName],
[Address],
[City],
[State],
[Zip],
[phone],
[Status]
FROM FrontDesk
ORDER BY Id ASC
DECLARE @arrival as DateTime
DECLARE @departure as DateTime
DECLARE @id as int
-- loop over the temprary table
SELECT @id = (SELECT MIN(id) FROM @FrontDeskRows)
SELECT @arrival = Arrival FROM @FrontDeskRows Where id = @id
SELECT @departure = Departure FROM @FrontDeskRows Where id = @id
WHILE @id IS NOT NULL
BEGIN
-- PROCESS EACH ROW HERE
-- get the next item in the temporary table
SELECT @id = (SELECT MIN(id) FROM @FrontDeskRows WHERE id > @id)
SELECT @arrival = Arrival FROM @FrontDeskRows Where id = @id
SELECT @departure = Departure FROM @FrontDeskRows Where id = @id
END