dbTalk Databases Forums  

updating with cursors

microsoft.public.sqlserver.dts microsoft.public.sqlserver.dts


Discuss updating with cursors in the microsoft.public.sqlserver.dts forum.



Reply
 
Thread Tools Display Modes
  #1  
Old   
shane
 
Posts: n/a

Default updating with cursors - 02-17-2004 , 04:06 AM






could somebody give me an example of how to write a cursor that will up date fields within a table from a table of the same design. this will be used to update existing data and to add new.

Reply With Quote
  #2  
Old   
Allan Mitchell
 
Posts: n/a

Default Re: updating with cursors - 02-17-2004 , 04:22 AM






OK From what I think I understand of your problem you do not need to use a
cursor.

If they are in the same database you can easily do

UPDATES

UPDATE A
SET A.Field = B.Field
FROM TABLE1 A JOIN TABLE2 B
ON A.KeyCol = B.KeyCol

Even if not in the same DB you can use this

INSERTS

INSERT TABLE1(col list)
SELECT <T2 Col List)
FROM TABLE2 T2 LEFT OUTER JOIN TABLE1 T1
ON T2.KeyCol = T1.KeyCol
WHERE T1.KeyCol IS NULL


If ths is not what you mean then can you please expand a little.

--

----------------------------

Allan Mitchell MCSE,MCDBA, (Microsoft SQL Server MVP)
www.allisonmitchell.com - Expert SQL Server Consultancy.
www.SQLDTS.com - The site for all your DTS needs.
I support PASS - the definitive, global community
for SQL Server professionals - http://www.sqlpass.org


"shane" <anonymous (AT) discussions (DOT) microsoft.com> wrote

Quote:
could somebody give me an example of how to write a cursor that will up
date fields within a table from a table of the same design. this will be
used to update existing data and to add new.




Reply With Quote
  #3  
Old   
Noah Ganter
 
Posts: n/a

Default Re: updating with cursors - 02-17-2004 , 08:03 AM



Right, avoid using cursors in SQL Server unless the set is very small. The
whole point of T-SQL is to use "set" processing, not interative processing,
because it is wildly more efficient. I came to SQL from SAS, where
everything is just a looping through each record in a table and I used to
use cursors alot. Try not to.

"Allan Mitchell" <allan (AT) no-spam (DOT) sqldts.com> wrote

Quote:
OK From what I think I understand of your problem you do not need to use
a
cursor.

If they are in the same database you can easily do

UPDATES

UPDATE A
SET A.Field = B.Field
FROM TABLE1 A JOIN TABLE2 B
ON A.KeyCol = B.KeyCol

Even if not in the same DB you can use this

INSERTS

INSERT TABLE1(col list)
SELECT <T2 Col List)
FROM TABLE2 T2 LEFT OUTER JOIN TABLE1 T1
ON T2.KeyCol = T1.KeyCol
WHERE T1.KeyCol IS NULL


If ths is not what you mean then can you please expand a little.

--

----------------------------

Allan Mitchell MCSE,MCDBA, (Microsoft SQL Server MVP)
www.allisonmitchell.com - Expert SQL Server Consultancy.
www.SQLDTS.com - The site for all your DTS needs.
I support PASS - the definitive, global community
for SQL Server professionals - http://www.sqlpass.org


"shane" <anonymous (AT) discussions (DOT) microsoft.com> wrote in message
news:F475DFC4-5F6E-4C13-B88D-20CC94AD5B14 (AT) microsoft (DOT) com...
could somebody give me an example of how to write a cursor that will up
date fields within a table from a table of the same design. this will be
used to update existing data and to add new.





Reply With Quote
  #4  
Old   
shane
 
Posts: n/a

Default Re: updating with cursors - 02-17-2004 , 11:46 PM



thanks for that
When would you use a cursor and a trigger then

Cheer

----- Allan Mitchell wrote: ----

OK From what I think I understand of your problem you do not need to use
cursor

If they are in the same database you can easily d

UPDATE

UPDATE
SET A.Field = B.Fiel
FROM TABLE1 A JOIN TABLE2
ON A.KeyCol = B.KeyCo

Even if not in the same DB you can use thi

INSERT

INSERT TABLE1(col list
SELECT <T2 Col List
FROM TABLE2 T2 LEFT OUTER JOIN TABLE1 T
ON T2.KeyCol = T1.KeyCo
WHERE T1.KeyCol IS NUL


If ths is not what you mean then can you please expand a little

--

---------------------------

Allan Mitchell MCSE,MCDBA, (Microsoft SQL Server MVP
www.allisonmitchell.com - Expert SQL Server Consultancy
www.SQLDTS.com - The site for all your DTS needs
I support PASS - the definitive, global communit
for SQL Server professionals - http://www.sqlpass.or


"shane" <anonymous (AT) discussions (DOT) microsoft.com> wrote in messag
news:F475DFC4-5F6E-4C13-B88D-20CC94AD5B14 (AT) microsoft (DOT) com..
Quote:
could somebody give me an example of how to write a cursor that will u
date fields within a table from a table of the same design. this will b
used to update existing data and to add new





Reply With Quote
  #5  
Old   
Allan Mitchell
 
Posts: n/a

Default Re: updating with cursors - 02-18-2004 , 12:48 AM



A cursor is generally,not always, slower despite the bad press. Even Joe
Celko concedes this point when calculating running totals in one of his
publications. I only ever use a cursor when the logic is so complicated,
the data I am working with is small and there is most definitely no other
way of doing it. I will explore woraround using temp tables first before
cursors.

Triggers

I use triggers to implement complicated business logic when enforcing a
relationship. They are essentially a special type of stored proc. Remember
though that they may increase the transaction time for your other
statements.



--

Allan Mitchell MCSE,MCDBA, (Microsoft SQL Server MVP)
www.allisonmitchell.com - Expert SQL Server Consultancy.
www.SQLDTS.com - The site for all your DTS needs.
I support PASS - the definitive, global community
for SQL Server professionals - http://www.sqlpass.org


"shane" <anonymous (AT) discussions (DOT) microsoft.com> wrote

Quote:
thanks for that.
When would you use a cursor and a trigger then?

Cheers

----- Allan Mitchell wrote: -----

OK From what I think I understand of your problem you do not need to
use a
cursor.

If they are in the same database you can easily do

UPDATES

UPDATE A
SET A.Field = B.Field
FROM TABLE1 A JOIN TABLE2 B
ON A.KeyCol = B.KeyCol

Even if not in the same DB you can use this

INSERTS

INSERT TABLE1(col list)
SELECT <T2 Col List)
FROM TABLE2 T2 LEFT OUTER JOIN TABLE1 T1
ON T2.KeyCol = T1.KeyCol
WHERE T1.KeyCol IS NULL


If ths is not what you mean then can you please expand a little.

--

----------------------------

Allan Mitchell MCSE,MCDBA, (Microsoft SQL Server MVP)
www.allisonmitchell.com - Expert SQL Server Consultancy.
www.SQLDTS.com - The site for all your DTS needs.
I support PASS - the definitive, global community
for SQL Server professionals - http://www.sqlpass.org


"shane" <anonymous (AT) discussions (DOT) microsoft.com> wrote in message
news:F475DFC4-5F6E-4C13-B88D-20CC94AD5B14 (AT) microsoft (DOT) com...
could somebody give me an example of how to write a cursor that
will up
date fields within a table from a table of the same design. this will
be
used to update existing data and to add new.






Reply With Quote
  #6  
Old   
shane
 
Posts: n/a

Default Re: updating with cursors - 02-18-2004 , 06:21 PM



Thankyo

----- Allan Mitchell wrote: ----

A cursor is generally,not always, slower despite the bad press. Even Jo
Celko concedes this point when calculating running totals in one of hi
publications. I only ever use a cursor when the logic is so complicated
the data I am working with is small and there is most definitely no othe
way of doing it. I will explore woraround using temp tables first befor
cursors

Trigger

I use triggers to implement complicated business logic when enforcing
relationship. They are essentially a special type of stored proc. Remembe
though that they may increase the transaction time for your othe
statements



--

Allan Mitchell MCSE,MCDBA, (Microsoft SQL Server MVP
www.allisonmitchell.com - Expert SQL Server Consultancy
www.SQLDTS.com - The site for all your DTS needs
I support PASS - the definitive, global communit
for SQL Server professionals - http://www.sqlpass.or


"shane" <anonymous (AT) discussions (DOT) microsoft.com> wrote in messag
news:91329030-29F2-4F24-BBC6-291BB539159F (AT) microsoft (DOT) com..
Quote:
thanks for that
When would you use a cursor and a trigger then
Cheer
----- Allan Mitchell wrote: ----
OK From what I think I understand of your problem you do not need t
use
cursor
If they are in the same database you can easily d
UPDATE
UPDATE
SET A.Field = B.Fiel
FROM TABLE1 A JOIN TABLE2
ON A.KeyCol = B.KeyCo
Even if not in the same DB you can use thi
INSERT
INSERT TABLE1(col list
SELECT <T2 Col List
FROM TABLE2 T2 LEFT OUTER JOIN TABLE1 T
ON T2.KeyCol = T1.KeyCo
WHERE T1.KeyCol IS NUL
If ths is not what you mean then can you please expand a little
--
---------------------------
Allan Mitchell MCSE,MCDBA, (Microsoft SQL Server MVP
www.allisonmitchell.com - Expert SQL Server Consultancy
www.SQLDTS.com - The site for all your DTS needs
I support PASS - the definitive, global communit
for SQL Server professionals - http://www.sqlpass.or
"shane" <anonymous (AT) discussions (DOT) microsoft.com> wrote in messag
news:F475DFC4-5F6E-4C13-B88D-20CC94AD5B14 (AT) microsoft (DOT) com..
could somebody give me an example of how to write a cursor tha
will u
date fields within a table from a table of the same design. this wil
b
used to update existing data and to add new


Reply With Quote
Reply




Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off



Powered by vBulletin Version 3.5.3
Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.