Question

I was wondering if anyone can tell me if this function follows ANSI SQL standard, and for future reference are there any resources that i can use to find if my SQL conforms to ANSI standards. And the DBMS i am using is MySQL.

CREATE FUNCTION INCREMENT()
RETURNS INT
BEGIN
    DECLARE oldVal INT;
    DECLARE newVal INT;
    SELECT currentVal INTO oldVal FROM atable FOR UPDATE;
    SET newVal=oldVal +1;
    UPDATE atable SET currentVal=newVal;
    RETURN newVal;
END;
Was it helpful?

Solution

Found a validation tool that can check if your SQL conforms to SQL 2003.
http://developer.mimer.com/validator/parser200x/index.tml

OTHER TIPS

Based on this comment

basically i need to retrieve the new updated value if i do UPDATE table SET current = current + 1 and then perform a select to get the new value, this could cause issues as what if another transaction updates between the update and select.

I don't think your problem has anything at all to do with SQL standards. I think your problem has to do with transactions and with transaction isolation levels.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top