Reputation: 21808
Is there a way to declare a simple (persistent) variable as a part of an SQL database?
I am trying to implement by hand a mechanism similar to auto_increment, but not associated with any particular field or table. Each time one of a set of my procedures is called, I would like that variable to be incremented.
I have found that you can create local, session and global variables. However:
The first two are not persistent. You open a new connection and the value will be reset.
The global one is not within the scope of the database, but is a completely global variable; usually used as some configuration setting.
A possible walkaround that I can think of would involve creating a table with just a single row in it. I am looking for a cleaner approach though - if that exists...
Upvotes: 3
Views: 1166
Reputation: 157038
Creating a table is probably your best option, since after all, that is the database server's place where it stores its (persistent) data.
You can also take advantage of all the nice things a database does for you, like locking, auditing, etc.
Upvotes: 4