我必须从电子表格快速将一些数据加载到数据库中,并且需要使用游标。光标有一些奇怪的副作用,我不完全确定为什么会这样。如果有人能解释,我会很感激。
使用order by子句时,光标运行324次(我的数据集的正确次数)。没有顺序,光标只运行了81次。当调查ID在没有select语句的情况下更改时,几乎看起来像是为光标重新评估了select语句。
以下是代码:
declare t cursor for
SELECT SurveyID, attributename, attributevalue FROM tempCAVImport cav
left outer join activitylog al on al.entityid = cav.surveyid and al.systemactivitytypeid = 19
where al.activitylogid is null
order by cav.surveyid --this was not in the statement originally
open t
fetch next from t
into @surveyId, @name, @value
declare @count int = 0
while @@Fetch_Status = 0
Begin
delete from @additionalInfo
insert into @additionalInfo (AttributeName, AttributeValue, EntityId)
select 'ClientAttributeName', @name, @surveyId
insert into @additionalInfo (AttributeName, AttributeValue, EntityId)
select 'ClientAttributeValue', @value, @surveyId
EXEC [InsertActivityLogEntry] null, 'Assigned Extended Attribute', 'Survey', @surveyId, null, @additionalInfo
set @count = @count + 1
print(@count)
fetch next from t
into @surveyId, @name, @value
End
close t
deallocate t
部分数据:
SurveyID AttributeName AttributeValue
11545575 Contacted Since Delivery NO
11545575 Finance Used xxx
11545575 Recommend Dealer NEUTRAL
11545575 Recommend xxx NEUTRAL
11545575 Recommend Finance NEUTRAL
11545575 Unresolved Problems NO
11626821 Contacted Since Delivery YES
11626821 Finance Used xxx
11626821 Recommend Dealer PROMOTER
11626821 Recommend xxx PROMOTER
11626821 Recommend Finance NEUTRAL
答案 0 :(得分:1)
如果[InsertActivityLogEntry]插入到光标所加入的表activitylog中,您的光标将从循环光标插入的代码表中选择。
这肯定会给你带来麻烦,除非你以某种方式控制订单......
也许这就是为什么order by帮助你:)