Return Styles: Pseud0ch, Terminal, Valhalla, NES, Geocities, Blue Moon.

Pages: 1-

SQL92[EXPERTS]

Name: Anonymous 2010-04-16 12:53

Hey /prog/

Is it legal to modify a table while there's a read cursor open on it? Is it defined behavior?

For example, in some pseudo-code:

result = sql("SELECT * FROM faggot_table WHERE faggot_type=5;");
for each (result) {
  if (whatever) sql("DELETE FROM faggot_table WHERE faggot_id=3;");
}

The reason I ask is because I tested with several database engines from the ENTERPRISE to the toy, and it works on some but errors out in others. So I can't use it in the general case.

However I'd like to know if it should work and some databases suck, if it shouldn't but is supported on some, or if it mustn't and it works by chance on some.

(It could work in some because they prefetch results, and maybe if they prefetch everything the cursor is already closed the moment you read the first row)

Name: Anonymous 2010-04-16 15:15

Is it legal to modify a table
No, and you can go to jail for that. So you won't pass Go and won't collect $200

Name: Anonymous 2010-04-16 16:36

          ∧_∧   / ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄
          ( ´∀`) < If you modify the table, what will I sit on?
        /    |    \________
       /       .|     
       / "⌒ヽ |.イ |
   __ |   .ノ | || |__
  .    ノく__つ∪∪   \
   _((_________\
    ̄ ̄ヽつ ̄ ̄ ̄ ̄ ̄ ̄ | | ̄
   ___________| |
    ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄| |

Name: Anonymous 2010-04-16 16:41

>>3

An [aa] tag.

Name: Anonymous 2010-04-16 16:45

>>1
You know about cursors, so why do you want to do something stupid like that?

Name: Anonymous 2010-04-16 17:54

Legal or not you should almost never use a cursor like that. Just write the delete query differently.

Name: Anonymous 2010-04-16 18:32

>>5,6
Usually I'd agree with you, but what if the condition for deletion can't be evaluated in SQL? (remember: SQL is not touring-complete)

Random example, you suddenly want to delete all the rows whose faggot_prime is not, in fact, a prime number. You can't to that directly in SQL (or maybe you can, don't make me pick another example).

You can:

* select to a temp table, read the temp table row by row issuing deletes on the main table if the condition is met

* read row by row generating a list, then delete using that list (a bit better if you don't expect to delete too much)

* do it cowboy style such as in the example above, reading row by row and deleting immediately when the condition is met

I can confirm that some of the SQL servers where the last option works internally implement cursors by generating a temporary table, so they're doing the first option for you whether you like it or not.

Name: Anonymous 2010-04-16 18:53

>>7
So do it the first way then. Jesus.

Name: Anonymous 2010-04-16 18:54

>>7
You create a new table storing all prime numbers.

Name: Anonymous 2010-04-16 19:00

I was once (circa 2000) told to modify PostgreSQL to add a certain kind of functionality to triggers (this was before PL/pgSQL etc were supported.) It was not exactly the best solution, but I'll say this: the PostgreSQ

Name: >>10 2010-04-16 19:03

L code was very, very clean, especially for something of its size and complexity.

I managed to typo into the reply button.

Name: Anonymous 2010-04-17 2:12

A single logical post may span

Name: Anonymous 2010-04-17 2:12

several physical posts

Name: Anonymous 2010-04-17 2:57

why are you even trying to do use your database to do this anyway? why not generate your data using tools that were designed for such purposes and then stick the data in the database at the end?

Name: Anonymous 2010-04-17 4:07

/prog/ at its finest: nobody is able or willing to answer the actual question.

The question is whether the behavior is valid in general, not how to overcome it, neither how to design the system such as to make the problem irrelevant.

"Can you cast a float as an integer in C? I have this weird compiler where it doesn't work"

"Well you shouldn't be doing that in the first place but you can always sprintf it and then atoi it back"

Name: Anonymous 2010-04-17 5:59

The default cursor behavior is to lock the row currently fetched so you can update it. Due to this, if you try to delete that specific row without utilizing the cursor it should fail. To avoid this you can set your cursor to READ_ONLY. If you would like to discuss different engines' behavior please mention which ones.

Name: Anonymous 2010-04-17 8:58

>>15
you can always sprintf it and then atoi it back
Leah Culver quality!

Name: Anonymous 2010-04-17 22:42

>>15
I'm sure that many of us are able, but trollan is much more interesting.

Name: Anonymous 2011-02-03 1:06

Name: Anonymous 2011-02-03 7:44

Name: Anonymous 2011-02-04 13:04


Don't change these.
Name: Email:
Entire Thread Thread List