tags:

views:

20

answers:

1

This (from body of a stored proc) is throwing a syntax error:

  IF (name = in_name)
          SET out_id = temp;
  ELSE
          LOCK TABLE People WRITE;
          INSERT INTO People (Name)
          VALUES (in_name);
          UNLOCK TABLE;
          SELECT LAST_INSERT_ID() INTO out_id 
  END IF

do I have to lock any tables I need at the start of the SP?

+1  A: 

THEN is missing: IF (name = in_name) THEN

But why do you want to lock the table? In this example it doesn't make sense at all, it will just kill overall performance because nobody else can do anything with the table. And the lock doesn't add anything. I don't see the point, but maybe you didn't tell us everything. ;)

Frank Heikens
Thanks Frank - what I am trying to achieve is this: if 'name' exists already, get it's id, if not insert it and return the new id. however, i will have multiple threads attempting to get/insert the same 'name' at the same time - 'name' must be unique but I cant set it as unique because it is a text column - what do you suggest? thanks
MalcomTucker
You can create a index on a `TEXT` column but you must specify a prefix length. See http://dev.mysql.com/doc/refman/5.1/en/indexes.html Obviously, if your `name` column can't be limited in size, it means it store something else than a name :)
ewernli
no, my name column cannot be limited, that is an absolute requirement unfortunately
MalcomTucker
VARCHAR(255) is not a real limit for a name but you can add a unique constraint.
Frank Heikens