SQL query in custom field

I need to create a Custom Field which will be the identifier of tickets within one queue. When I move ticket to this queue, action script should run sql query which find last value for this CF and insert value+1. Could anyone help me with the script action?