头大如斗:不知是我还是MS-SQL有神经病?一、问题说明: 重新调整关联分类:把独立的关联分类记录,直接绑定到对应的分类记录中。
绑定完成的关联记录不再有用直接删除! 为了实现这个目的,采用CURSOR,把所有关联记录读进去,然后循环处理,
但是发现这个CURSOR记录有些神经病,读到var中的值有时不正确!如果不存到
CURSOR中,我用select语句列出来记录完全正确,一经CURSOR遍历循环读取就不
对了,真是晕倒!在循还部份放了跟踪代码,发现显示的结果和select的结果不
同,前半部份正确,往后面就错乱掉了总的记录还不到1W条;
二、代码如下:
-- 3、绑定关联分类编号Declare @CategoryID varchar(12), @Relation varchar(250),@RelationId varchar(12)-- 读取全部关联记录
DECLARE _Cursor CURSOR FOR
Select
rtrim(CategoryID),rtrim(Relation)
From
Category
Where
Relation <>'0'OPEN _CursorFETCH NEXT FROM _Cursor
INTO @CategoryID , @RelationWHILE @@FETCH_STATUS = 0
BEGIN
-- 计算要关联的节点编号
set @RelationId = left(@CategoryId, len(@CategoryId)-2) -- 检查需要标注关联分类的记录是否存在
if(exists(select * from Category where CategoryId=@Relation))
Begin
-- 更新:允许存在多个关联分类
Update
dbo.Category Set Relation=Case Relation
When '0' THEN
@RelationId
Else
@RelationId +','+Relation
End
where
CategoryId=@Relation -- 跟踪var情况
Select @CategoryId as CategoryId, @Relation as Relation, @RelationId as RelationId
-- '删除关联记录: '
Delete dbo.Category where CategoryId = @CategoryId and Relation=@Relation
End FETCH NEXT FROM _Cursor
INTO @CategoryID , @Relation ENDDEALLOCATE _Cursor
哪位大哥帮帮忙,看看是不是我的代码有问题?
绑定完成的关联记录不再有用直接删除! 为了实现这个目的,采用CURSOR,把所有关联记录读进去,然后循环处理,
但是发现这个CURSOR记录有些神经病,读到var中的值有时不正确!如果不存到
CURSOR中,我用select语句列出来记录完全正确,一经CURSOR遍历循环读取就不
对了,真是晕倒!在循还部份放了跟踪代码,发现显示的结果和select的结果不
同,前半部份正确,往后面就错乱掉了总的记录还不到1W条;
二、代码如下:
-- 3、绑定关联分类编号Declare @CategoryID varchar(12), @Relation varchar(250),@RelationId varchar(12)-- 读取全部关联记录
DECLARE _Cursor CURSOR FOR
Select
rtrim(CategoryID),rtrim(Relation)
From
Category
Where
Relation <>'0'OPEN _CursorFETCH NEXT FROM _Cursor
INTO @CategoryID , @RelationWHILE @@FETCH_STATUS = 0
BEGIN
-- 计算要关联的节点编号
set @RelationId = left(@CategoryId, len(@CategoryId)-2) -- 检查需要标注关联分类的记录是否存在
if(exists(select * from Category where CategoryId=@Relation))
Begin
-- 更新:允许存在多个关联分类
Update
dbo.Category Set Relation=Case Relation
When '0' THEN
@RelationId
Else
@RelationId +','+Relation
End
where
CategoryId=@Relation -- 跟踪var情况
Select @CategoryId as CategoryId, @Relation as Relation, @RelationId as RelationId
-- '删除关联记录: '
Delete dbo.Category where CategoryId = @CategoryId and Relation=@Relation
End FETCH NEXT FROM _Cursor
INTO @CategoryID , @Relation ENDDEALLOCATE _Cursor
哪位大哥帮帮忙,看看是不是我的代码有问题?
1、
这个上面 set @CategoryID =null;set @Relation=null;
FETCH NEXT FROM _Cursor
INTO @CategoryID , @Relation END
2、close _cursor
DEALLOCATE _Cursor
Category
这就要求游标是静态的。
DECLARE _Cursor CURSOR FOR
Select
rtrim(CategoryID),rtrim(Relation)
From
Category
Where
Relation <>'0'
---〉
DECLARE _Cursor CURSOR STATIC FOR
Select
rtrim(CategoryID),rtrim(Relation)
From
Category
Where
Relation <>'0'
试下