Cursor for update sql


















To demonstrate how to use a cursor to update records we'll create a running total of the number of Oscars our films have won. To begin with we'll declare a couple of variables and set their default values. The first variable will hold the running total of Oscars while the second will be used to read each individual film's Oscars as the cursor moves through the record set.

Next, we'll declare our cursor, open it and move it to the first record. At the same time we'll read the first film's Oscar wins into the variable we declared earlier. Now we can begin looping through the record set. We'll start by setting up the basic structure of the loop and adding a quick test to ensure that our variable is accumulating the number of Oscars.

The result of executing the code should be a running total of the Oscars in the Messages window:. The full set of code required to make this system work is shown below:. To see the results of this procedure we'll need to select some data from the underlying table. Forgot your password? Search within: Articles Quick Answers Messages. Technical Blog. View Original. Tagged as SQL. Stats Manjuke Fernando Rate me:.

Please Sign up or sign in to vote. Consider the following table: SQL. Copy Code. Manjuke Fernando. My passion lies in building business intelligence and data-based solutions, writing about things I work with and talking about it. New technologies relevant to my line of work interest me and I am often seen playing with early releases of such technologies.

My aspiration over the next several years is to achieve higher competency and recognition in the field of Data Analytics and move into a career of data science. Cursors are generally not a good solution, so any solution such as Mikael Eriksson's is potentially better. However if you really have to use a cursor to do an update then you should mark it as insensitive:. I haven't confirmed this on but certainly on and below, you can expect all kinds of weirdness if you update the data your cursor is defined against, without marking the cursor as insensitive.

How are we doing? Please help us improve Stack Overflow. Take our short survey. Stack Overflow for Teams — Collaborate and share knowledge with a private group.

Create a free Team What is Teams? Collectives on Stack Overflow. Learn more. How to use cursor to update record Ask Question. Asked 10 years ago. Active 2 years, 9 months ago. Viewed 67k times. Igor Borisenko 3, 3 3 gold badges 31 31 silver badges 46 46 bronze badges.



0コメント

  • 1000 / 1000