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.
Author |
Topic |
maddalimurali
Starting Member
2 Posts |
Posted - 2002-11-19 : 09:44:43
|
Hello All,I am have to transfer data between two tables, this sounds quite simple, but I happen to hit by performance issues. Here is my senario, I have to check for the record in the destination table and insert or update the record accordingly. Right now I am using recordset object, for this task. I cann't use update statement as I have to build the set clause, in some cases there may be more than 52 columns. Can any one suggest me a better way of doing this? Right now it takes me more than 4 hrs to update say 20000 records, with 52 columns. This is way too long, I am running this on pIII with 256mb ram.Is there any way I can achieve the same task from SQL queries?Any kind of help is greatly appreciated.Thank you,murali.k.maddali |
|
royv
Constraint Violating Yak Guru
455 Posts |
Posted - 2002-11-19 : 10:05:54
|
Yes, you can do this easily in TSQL and compile it into a stored procedure. If you have to build the set clause, then you can use dynamic sql. There are several articles on this site on stored procedures and dynamic sql. Also, I can tell you right now, you *should* see a performance boost by adding more memory if you did nothing to the code.***************************************Death must absolutely come to enemies of the code! |
 |
|
ValterBorges
Master Smack Fu Yak Hacker
1429 Posts |
|
maddalimurali
Starting Member
2 Posts |
Posted - 2002-11-19 : 10:52:36
|
Hello ValterBorges I have gone throught the links you have send to me, but I donn't think those cases would be usefull in my case, however they where quite informative.Here is my exact situation.I need to transfer data between any two tables, I would be doing this from two dynamic select queries(in the sense these can be any two select queries), based on which I build to recordset objects. the source would be Readonly, forwardCursor. and the destination would be optimistic, staticCursor. I loop through source recordset and find the corresponding record in destination recordset using find or filter on the recordset and do a column by column search to see if there is a data change. and do a batch update on the destination recordset. Here is the sample code I am using, I might have miss some code during copy paste, but this gives pretty much overview of what I am doing.-------------------------Do Until srcRs.EOFWith dstRsIf ((UBound(sIDFieldNameArr) - LBound(sIDFieldNameArr)) > 0) Then.Filter = sFindCriteriaElse.Find sFindCriteria, 0, adSearchForward, adBookmarkFirstEnd IfEnd WithIf dstRs.EOF ThenIf dstRs.Supports(adAddNew) ThendstRs.AddNewFor iCounter = 0 To (iColCount - 1)With srcRs.Fields(iCounter)dstRs(.Name).Value = Trim(.Value)iLoop2 = iLoop2 + 1End WithNext iCounterElse' update the record hereFor iCounter = 0 To (iColCount - 1)With srcRs.Fields(iCounter)sFieldName = Trim(.Name)sSrcFldValue = Trim(.Value) & ""End WithsDstFldValue = Trim(dstRs.Fields(sFieldName).Value) & ""If dstRs.Supports(adUpdate) ThenIf LenB(sDstFldValue) <= 0 And LenB(sSrcFldValue) > 0 ThendstRs(sFieldName).Value = sSrcFldValueiLoop = iLoop + 1ElseIf sDstFldValue <> sSrcFldValue ThendstRs(sFieldName).Value = sSrcFldValueiLoop = iLoop + 1End IfEnd IfNext iCounterEnd IfWith dstRsIf .EditMode <> adEditNone TheniLoop3 = iLoop3 + 1If iLoop3 Mod 10 = 0 Then.UpdateBatchEnd IfEnd IfEnd WithsrcRs.MoveNextIf iLoopCtr Mod 100 = 0 ThenDoEventsEnd IfLoopdstRs.UpdateBatch------------------------- Thank you, murali.k.maddali |
 |
|
ValterBorges
Master Smack Fu Yak Hacker
1429 Posts |
Posted - 2002-11-19 : 11:46:56
|
I need more than ADO code to understand how to improve your process. I need table definitions, sample data, some before and after examples of the data and an pseudo-code breakdown of what you're trying to do.quote: Here is my senario, I have to check for the record in the destination table and insert or update the record accordingly.
I have a table for example that gets imported in a dts task from another database and I need to insert or update so I do the following:/***** INSERT NEW RATES INSERT INTO dbo.Rates (RateID, RateDesc, Active)SELECT A.RateID, A.RateDesc, 1FROM dbo.[Import Rates] As A LEFT OUTER JOIN dbo.Rates As B ON A.[RateID] = B.[RateID] WHERE B.[RateID] Is NullAnd also the following/*****UPDATE EXISTING RATES ****/UPDATE dbo.RatesSET RateDesc = A.RateDesc,Active = 1FROM dbo.[Import Rates] As A INNER JOIN dbo.Rates As B ON A.[RateID] = B.[RateID] quote: I cann't use update statement as I have to build the set clause, in some cases there may be more than 52 columns.
Use dynamic sql to query the INFORMATION_SCHEMA VIEWS, drop and recreate your update and insert statements with the proper columns. |
 |
|
|
|
|
|
|