473,385 Members | 2,029 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

UPDATE CURSOR - declaring and use.

I need to do something relatively simple…

I need to update a table using a cursor. (I may have to create a
stored procedure for doing this…)

I need to declare an update cursor, fetch the cursor and update the
data (and presumably close the cursor and de-allocate it…

The update query is as follows… Would anyone there know how to
declare the cursor for update and use it?

UPDATE A
SET A.Field1 =
(SELECT B.Field1
FROM B INNER JOIN A ON A.id = B.id)

I need to know how to declare the cursor and fetch it.

Can anyone give me an example of the code I need for the SQL Server?

Thanks!
Jul 20 '05 #1
4 30423
It's usually best to avoid using cursors at all. If you post your DDL, some
sample data and show your required result someone should be able to suggest
a cursor-free alternative.

I think the basic syntax you are looking for is as follows

UPDATE A
SET A.col1 =
(SELECT B.col1
FROM B WHERE id = A.id)

assuming ID is unique in B.

--
David Portas
SQL Server MVP
--
Jul 20 '05 #2
I appreciate your answer. Actually the syntax of the update query that
I have shown works fine.

What I need to do is update a table based on information from a second
table. Since the relationship in one-to-many, I can potentially have
many rows returned for the update query.

Unless there is some other way to update multiple records, I need to
declare an update cursor, and what I was hoping someone could tell me
was the exact syntax for it... I read the "Help" documentation and made
a few trys...but an example would really help.

Thanks!
*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!
Jul 20 '05 #3
Carol Berry (ca***@123marbella.com) writes:
I appreciate your answer. Actually the syntax of the update query that
I have shown works fine.

What I need to do is update a table based on information from a second
table. Since the relationship in one-to-many, I can potentially have
many rows returned for the update query.

Unless there is some other way to update multiple records, I need to
declare an update cursor, and what I was hoping someone could tell me
was the exact syntax for it... I read the "Help" documentation and made
a few trys...but an example would really help.


There are. But for all these types of questions, your chances to get
relevant help improves if you include:

o CREATE TABLE statements for your table.
o INSERT statements with sample data.
o The desired result from the sample data.

This permits people to post a tested solution, and saves them from guessing
what you are really looking for.
--
Erland Sommarskog, SQL Server MVP, so****@algonet.se

Books Online for SQL Server SP3 at
http://www.microsoft.com/sql/techinf...2000/books.asp
Jul 20 '05 #4
Yes you can use an UPDATE statement based on a one/many -to-many join
between tables. Here's an example from the Pubs database, which updates each
Author with his/her YTD sales.

ALTER TABLE Authors ADD ytd_sales INTEGER

UPDATE Authors
SET ytd_sales =
(SELECT COALESCE(SUM(T.ytd_sales),0)
FROM TitleAuthor AS A
JOIN Titles AS T
ON A.title_id = T.title_id
WHERE A.au_id = Authors.au_id)

If you require more help then, as Erland says, we really need more
information (http://www.aspfaq.com/5006).

--
David Portas
SQL Server MVP
--
Jul 20 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: John Moore | last post by:
Hi, I normally work with Java but I'm interested in using Python as well, particularly for little tasks like doing some massaging of data in a MySQL database. Below is my first attempt. I'm sure...
10
by: technocrat | last post by:
Hi, I am trying to declare and cursor and thn load from that cursor into another table. Since I have almost 4 million records, I cant do it without the cursor which reduces the time by almost...
5
by: Paul M | last post by:
Hi All, I've been tasked with writing a z/OS C program to read and update a table (DB/2 v8). This Table contains a single row with a single column of data. This program (which will run as a...
19
by: Steve | last post by:
ASP error number 13 - Type mismatch with SELECT...FOR UPDATE statement I got ASP error number 13 when I use the SELECT...FOR UPDATE statement as below. However, if I use SELECT statement without...
12
by: Lucky | last post by:
Hi guys! i want to create one cursor in the t-sql. the problem is i want to use stored procedure instead of select command in cursor. can anyone tell me how can i use stored procedure's o/p to...
2
by: mokazawa1 | last post by:
Hi, I'm using stored procedures in DB2 UDB 8.1.2. In this stored, I execute a select for update command, opening a cursor. Then I update the rows using fetch and current of. The problem is that...
9
by: Frank Swarbrick | last post by:
New to SQL here... We have a CURSOR declared thusly: EXEC SQL DECLARE ALL-ADJSTMTS-CSR CURSOR FOR SELECT ACCT.ACCOUNT_ID , ACCT.APPL_ID , ACCT.BRANCH_NUMBER , ACCT.CATEGORY_CODE
3
by: Michel Esber | last post by:
Hi all, DB2 V8 LUW FP 15 There is a table T (ID varchar (24), ABC timestamp). ID is PK. Our application needs to frequently update T with a new value for ABC. update T set ABC=? where ID...
1
by: vituko | last post by:
plpgsql (postgresql 8.3 but I can upgrade) I can open a cursor with a dynamic query (table / column variable) : -open cursor for execute '...' ; But if I want do updates... - execute 'update...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.