Questions tagged [database-cursor]

Use this tag for programming-related questions about database cursors. If your question is specific to a particular database, tag the database type too.

830 questions
199
votes
5 answers

Why do you need to create a cursor when querying a sqlite database?

I'm completely new to Python's sqlite3 module (and SQL in general for that matter), and this just completely stumps me. The abundant lack of descriptions of cursor objects (rather, their necessity) also seems odd. This snippet of code is the…
Shaun Mitchell
  • 2,306
  • 2
  • 15
  • 10
195
votes
17 answers

SQL Call Stored Procedure for each Row without using a cursor

How can one call a stored procedure for each row in a table, where the columns of a row are input parameters to the sp without using a Cursor?
Johannes Rudolph
  • 35,298
  • 14
  • 114
  • 172
129
votes
13 answers

Why do people hate SQL cursors so much?

I can understand wanting to avoid having to use a cursor due to the overhead and inconvenience, but it looks like there's some serious cursor-phobia-mania going on where people are going to great lengths to avoid having to use one. For example, one…
Steven A. Lowe
  • 60,273
  • 18
  • 132
  • 202
109
votes
10 answers

Output pyodbc cursor results as python dictionary

How do I serialize pyodbc cursor output (from .fetchone, .fetchmany or .fetchall) as a Python dictionary? I'm using bottlepy and need to return dict so it can return it as JSON.
Foo Stack
  • 2,185
  • 7
  • 24
  • 25
89
votes
2 answers

Get Multiple Values in SQL Server Cursor

I have a cursor containing several columns from the row it brings back that I would like to process at once. I notice most of the examples I've seeing on how to use cursors show them assigning a particular column from the cursor to a scalar value…
kingrichard2005
  • 7,179
  • 21
  • 86
  • 118
85
votes
5 answers

How to assign a select result to a variable?

How do I store a selected field value into a variable from a query and use it in an update statement? Here is my procedure: I'm writing a SQL Server 2005 T-SQL stored procedure which does the following: gets list of invoices id's from invoice table…
phill
  • 13,434
  • 38
  • 105
  • 141
79
votes
3 answers

Looping Over Result Sets in MySQL

I am trying to write a stored procedure in MySQL which will perform a somewhat simple select query, and then loop over the results in order to decide whether to perform additional queries, data transformations, or discard the data altogether. …
Dereleased
  • 9,939
  • 3
  • 35
  • 51
78
votes
11 answers

Can I loop through a table variable in T-SQL?

Is there anyway to loop through a table variable in T-SQL? DECLARE @table1 TABLE ( col1 int ) INSERT into @table1 SELECT col1 FROM table2 I use cursors as well, but cursors seem less flexible than table variables. DECLARE cursor1 CURSOR FOR…
Kuyenda
  • 4,529
  • 11
  • 46
  • 64
78
votes
8 answers

SQLite Android Database Cursor window allocation of 2048 kb failed

I have a routine that runs different queries against an SQLite database many times per second. After a while I would get the error "android.database.CursorWindowAllocationException: - Cursor window allocation of 2048 kb failed. # Open Cursors = "…
alex
  • 1,250
  • 2
  • 12
  • 19
72
votes
2 answers

SQL Server: how to add new identity column and populate column with ids?

I have a table with huge amount of data. I'd like to add extra column id and use it as a primary key. What is the better way to fill this column with values from one 1 to row count Currently I'm using cursor and updating rows one by one. It takes…
Sergejs
  • 2,540
  • 6
  • 32
  • 51
65
votes
11 answers

Why is it considered bad practice to use cursors in SQL Server?

I knew of some performance reasons back in the SQL 7 days, but do the same issues still exist in SQL Server 2005? If I have a resultset in a stored procedure that I want to act upon individually, are cursors still a bad choice? If so, why?
Kilhoffer
  • 32,375
  • 22
  • 97
  • 124
60
votes
2 answers

Viewing an Android database cursor

Would anyone know how I can view what a cursor has in it during debugging so that I can determine the functionality of my database helper? It keeps acting like it's returning data, but then when I attempt to use the cursor.isNull(0) method, I keep…
Skittles
  • 2,866
  • 9
  • 31
  • 37
60
votes
7 answers

Using a cursor with dynamic SQL in a stored procedure

I have a dynamic SQL statement I've created in a stored procedure. I need to iterate over the results using a cursor. I'm having a hard time figuring out the right syntax. Here's what I'm doing. SELECT @SQLStatement = 'SELECT userId FROM…
Micah
  • 111,873
  • 86
  • 233
  • 325
57
votes
5 answers

What are the benefits of using database cursor?

It is based on the interview question that I faced. Very short definition can be It can be used to manipulate the rows returned by a query. Besides the use of the cursor (Points are listed here on MSDN), I have a question in my mind that if we…
Vikas
  • 24,082
  • 37
  • 117
  • 159
57
votes
1 answer

Why do I get "A cursor with the name already exists"?

I have this trigger: CREATE TRIGGER CHECKINGMAXQTYDAYSVACANCY ON TDINCI AFTER INSERT AS DECLARE @incidentCode int, @dateStart datetime, @dateEnd datetime, @daysAccumulated int, @maxDaysAvailable int …
Erick Asto Oblitas
  • 1,399
  • 3
  • 21
  • 47
1
2 3
55 56