Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2008 Forums
 Transact-SQL (2008)
 adding index changed the data??

Author  Topic 

edyl
Starting Member

35 Posts

Posted - 2011-12-21 : 18:06:41
Hello Everyone,

I am stumped that adding just an index to a column changed the data. I am using a while loop code below to recursively flatten out the hierarchy in the data.


declare @lastcount int
declare @lastcycle int

Select HierarchyData.Name, HierarchyData.MemberName, 0 as [Cycle] INTO #list
FROM HierarchyData

SET @lastcount = @@rowcount
SET @lastcycle = 0

while @lastcount > 0
BEGIN
INSERT INTO #list
SELECT Members.Name, Child.MemberName as [MemberName], @lastcycle+1 as [Cycle]
FROM #list Members
JOIN HierarchyData Child ON Members.MemberName = Child.Name
LEFT JOIN #list cycletest ON Members.Name = cycletest.Name AND Child.MemberName = cycletest.Membername
WHERE Members.Cycle = @lastcycle AND NOT (Members.Name = Child.MemberName) AND cycletest.Name is null

SET @lastcount = @@rowcount

SET @lastcycle = @lastcycle + 1
END


This works for us and gives us the results that we need. However it runs for a very long time. So we thought of adding indexes to the columns.

I added the indexes on the temp table #list just before putting it through the while loop like shown below.


create index idx_list on #list(Name)
create index idx_listmem on #list(MemberName)


It runs very fast but all the data do not get populated. Looks like adding a index to the temp table changes the way the script runs.

Any ideas suggestions would be greatly appreciated.

Thanks
edyl

X002548
Not Just a Number

15586 Posts

Posted - 2011-12-21 : 18:09:33
>> I am stumped that adding just an index to a column changed the data

No

Brett

8-)

Hint: Want your questions answered fast? Follow the direction in this link
http://weblogs.sqlteam.com/brettk/archive/2005/05/25/5276.aspx


Want to help yourself?

http://msdn.microsoft.com/en-us/library/ms130214.aspx

http://weblogs.sqlteam.com/brettk/

http://brettkaiser.blogspot.com/


Go to Top of Page

Lamprey
Master Smack Fu Yak Hacker

4614 Posts

Posted - 2011-12-21 : 18:30:02
It's possible (although very rare) for the the result Result to be different, but the actual Data should not be changed.
Go to Top of Page
   

- Advertisement -