如何将此 sql 服务器游标转换为基于设置的游标以提高速度?
How to convert this sql server cursor to set-based for speed?
如果条件在一个 table 中为真,我必须插入多个 table,即
Table
Person
tableID PersonUniqueNumber
1 123
2 1234
3 121
4 12
5 113333
还有一个table
RentedHousesDetail
HouseId(tableId) HouseName HouseLocation ISOK
1 A CA NO
2 B DT NULL
3 C NY NULL
4 D CA
5 E CA
和其他table
Table
加利福尼亚之家
Table
状态绿色
所以,我必须为每个人做的是,我必须查看他在 RentedHousesDetail 中的住所位置是否为 CA,然后我必须在 table CALIFORNIAHOUSE 中进行单行插入 RentedHousesDetail.ID和 STATUSGREEN 并将 RentedHousesDetail.ISOK 列更新为 NO.
table中有几千行,所以我写了一个游标,例如
DECLARE variables
DECLARE cursorName CURSOR -- Declare cursor
LOCAL SCROLL STATIC
FOR
select PERSON.ID of those rows only where we have CA in RentedhouseDetails
OPEN cursorName -- open the cursor
FETCH NEXT FROM cursorName
INTO variables
WHILE @@FETCH_STATUS = 0
BEGIN
FETCH NEXT FROM cursorName
FOR EACH ROW that we have from cursor, insert into CALIFORNIAHOUSE and STATUSGREEN and update RentedHousesDetail.ISOK to NO
END
CLOSE cursorName -- close the cursor
DEALLOCATE cursor
请告诉我在 Person 和 Rentedhousedetails table 中的数千行上使用游标是否可以?如何将其转换为基于集合的操作以提高速度?
如果您使用临时 table,则非常简单。没有理由为此使用游标,下面的性能应该超过基于游标的解决方案。
显然,您需要填写伪代码的空白。
Create Table #PersonIDs (PersonID int Not Null Primary Key Clustered);
Insert Into #PersonIDs
Select Person.ID --- of those rows only where we have CA in RentedHouseDetails
Insert Into CALIFORNIAHOUSE
Select PersonID From #PersonIDs;
Insert Into STATUSGREEN
Select PersonID From #PersonIDs;
Update rhd
Set ISOK = 'No'
From RentedHousesDetail As rhd
Join #PersonIDs On rhd.PersonID = #PersonIDs.PersonID;
Drop Table #PersonIDs;
我觉得这里没有必要使用游标。
首先,只有在 RentedhouseDetails 中有 CA 的那些行中,您才需要 select PERSON.ID
喜欢
select p.id from Person p JOIN RentedHousesDetail r ON p.ID=r.ID
where r.HouseLocation='CA'
然后将所有记录插入 CALIFORNIAHOUSE 和 STATUSGREEN table
像这样
Insert into CALIFORNIAHOUSE
select p.id from Person p JOIN RentedHousesDetail r ON p.ID=r.ID
where r.HouseLocation='CA'
和
Insert into STATUSGREEN
select p.id from Person p JOIN RentedHousesDetail r ON p.ID=r.ID
where r.HouseLocation='CA'
AND 最后更新 table RentedHousesDetail where HouseLocation='CA' as 'NO'
像这样
update RentedHousesDetail set ISOK='NO' from Person p JOIN RentedHousesDetail r ON p.ID=r.ID
where r.HouseLocation='CA'
如果条件在一个 table 中为真,我必须插入多个 table,即
Table
Person
tableID PersonUniqueNumber
1 123
2 1234
3 121
4 12
5 113333
还有一个table
RentedHousesDetail
HouseId(tableId) HouseName HouseLocation ISOK
1 A CA NO
2 B DT NULL
3 C NY NULL
4 D CA
5 E CA
和其他table
Table 加利福尼亚之家
Table 状态绿色
所以,我必须为每个人做的是,我必须查看他在 RentedHousesDetail 中的住所位置是否为 CA,然后我必须在 table CALIFORNIAHOUSE 中进行单行插入 RentedHousesDetail.ID和 STATUSGREEN 并将 RentedHousesDetail.ISOK 列更新为 NO.
table中有几千行,所以我写了一个游标,例如
DECLARE variables
DECLARE cursorName CURSOR -- Declare cursor
LOCAL SCROLL STATIC
FOR
select PERSON.ID of those rows only where we have CA in RentedhouseDetails
OPEN cursorName -- open the cursor
FETCH NEXT FROM cursorName
INTO variables
WHILE @@FETCH_STATUS = 0
BEGIN
FETCH NEXT FROM cursorName
FOR EACH ROW that we have from cursor, insert into CALIFORNIAHOUSE and STATUSGREEN and update RentedHousesDetail.ISOK to NO
END
CLOSE cursorName -- close the cursor
DEALLOCATE cursor
请告诉我在 Person 和 Rentedhousedetails table 中的数千行上使用游标是否可以?如何将其转换为基于集合的操作以提高速度?
如果您使用临时 table,则非常简单。没有理由为此使用游标,下面的性能应该超过基于游标的解决方案。
显然,您需要填写伪代码的空白。
Create Table #PersonIDs (PersonID int Not Null Primary Key Clustered);
Insert Into #PersonIDs
Select Person.ID --- of those rows only where we have CA in RentedHouseDetails
Insert Into CALIFORNIAHOUSE
Select PersonID From #PersonIDs;
Insert Into STATUSGREEN
Select PersonID From #PersonIDs;
Update rhd
Set ISOK = 'No'
From RentedHousesDetail As rhd
Join #PersonIDs On rhd.PersonID = #PersonIDs.PersonID;
Drop Table #PersonIDs;
我觉得这里没有必要使用游标。 首先,只有在 RentedhouseDetails 中有 CA 的那些行中,您才需要 select PERSON.ID 喜欢
select p.id from Person p JOIN RentedHousesDetail r ON p.ID=r.ID
where r.HouseLocation='CA'
然后将所有记录插入 CALIFORNIAHOUSE 和 STATUSGREEN table
像这样
Insert into CALIFORNIAHOUSE
select p.id from Person p JOIN RentedHousesDetail r ON p.ID=r.ID
where r.HouseLocation='CA'
和
Insert into STATUSGREEN
select p.id from Person p JOIN RentedHousesDetail r ON p.ID=r.ID
where r.HouseLocation='CA'
AND 最后更新 table RentedHousesDetail where HouseLocation='CA' as 'NO'
像这样
update RentedHousesDetail set ISOK='NO' from Person p JOIN RentedHousesDetail r ON p.ID=r.ID
where r.HouseLocation='CA'