Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Persistent variable in an SQL database?

Tags:

sql

mysql

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...

like image 678
CygnusX1 Avatar asked Feb 04 '26 15:02

CygnusX1


1 Answers

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.

like image 50
Patrick Hofman Avatar answered Feb 06 '26 04:02

Patrick Hofman



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!