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
 General SQL Server Forums
 New to SQL Server Programming
 updating datas

Author  Topic 

rudba
Constraint Violating Yak Guru

415 Posts

Posted - 2009-05-12 : 10:04:27
Is there anyway to update Field3 (with adding Field1,Field1 if there are same value in Field2) with out using cursor.
I used the cursor but i have data like more than 5 Millions so its taking time .
the following records output looks like this:
ID Field1 Field2 Field3
1 Abc A100 Abc,Mnop
2 Xyz B200 Xyz,AAAA
3 Mnop A100 Abc,Mnop
4 TT C300
5 AAAA B200 Xyz,AAAA


declare @tbl1 table
(ID INT,
Field1 varchar(20),
Field2 varchar(10),
Field3 varchar(max)
)

INSERT INTO @tbl1
SELECT 1, 'Abc','A100',null

INSERT INTO @tbl1
SELECT 2,'Xyz', 'B200',null

INSERT INTO @tbl1
SELECT 3,'Mnop', 'A100',null

INSERT INTO @tbl1
SELECT 4, 'TT','C300',null

INSERT INTO @tbl1
SELECT 5,'AAAA', 'B200',null

ID Field1 Field2 Field3
1 Abc A100 Abc,Mnop
2 Xyz B200 Xyz,AAAA
3 Mnop A100 Abc,Mnop
4 TT C300
5 AAAA B200 Xyz,AAAA

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2009-05-12 : 10:07:50
are you using sql 2005?
Go to Top of Page

rudba
Constraint Violating Yak Guru

415 Posts

Posted - 2009-05-12 : 10:21:12
yes sql 2005 with sp2
Go to Top of Page

madhivanan
Premature Yak Congratulator

22864 Posts

Posted - 2009-05-12 : 10:58:55
http://www.sqlteam.com/forums/topic.asp?TOPIC_ID=81254

Madhivanan

Failing to plan is Planning to fail
Go to Top of Page

rudba
Constraint Violating Yak Guru

415 Posts

Posted - 2009-05-12 : 11:24:26
I tried to emplement on my taks but could not get the right result.
Anybody have idea?
Go to Top of Page

rudba
Constraint Violating Yak Guru

415 Posts

Posted - 2009-05-12 : 17:29:50
Guys, any one got the best solution?
I tried this way. But still taking time, is this good way?


SELECT t1.Id, t1.Field2,STUFF((SELECT ',' + t2.Field1 FROM @tbl1 AS t2 WHERE t2.Field2 = t1.Field2 FOR XML PATH('')), 1, 1, '') AS NewField
INTO #MyTemp FROM @tbl1 AS t1

select * from #MyTemp

UPDATE @tbl1
SET Field3=#MyTemp.NewField
FROM @tbl1, #MyTemp
WHERE @tbl1.ID=#MyTemp.ID

select * from @tbl1
drop table #MyTemp
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2009-05-13 : 10:24:38
looks fine. how long does query takes to execute currently?
Go to Top of Page

rudba
Constraint Violating Yak Guru

415 Posts

Posted - 2009-05-13 : 10:27:38
quote:
Originally posted by visakh16

looks fine. how long does query takes to execute currently?



it takes 35 minutes but still not complete so i killed the process.
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2009-05-13 : 10:33:04
are you using this alone or in a batch?
Go to Top of Page

rudba
Constraint Violating Yak Guru

415 Posts

Posted - 2009-05-13 : 11:09:57
quote:
Originally posted by visakh16

are you using this alone or in a batch?



using alone
Go to Top of Page
   

- Advertisement -