How to select the session variables created in MySQL? - mysql

I have created a variable named value(set #value="variable123") and used it in many queries.
its working fine, but how can I select these variables (like procedures we have a statement show procedure status).
When I executed "show variables", I did not get variables which I have created.

Actually, you can not do it - because of variables definition principles.

Related

In MySQL, how can I have a stored procedure query the tables from the calling database instead of the database where the stored procedure is defined

For the sake of simplification, let's say I have 2 databases with data, db_data_1 and db_data_2 which have the same set of tables and I have a 3rd database where my stored procedures are defined, say db_sp. Let's say my stored procedure is called my_sp
When I try to call db_sp.my_sp from either db_data_1 or db_data_2, I get an error saying that the tables referenced in db_sp.my_sp don't exist.
How can I have db_sp.my_sp query the tables in the calling database vs the database where my_sp is defined (namely db_sp)
Thanks.
You must qualify the table names in your query with the database name in the stored procedure. SELECT col FROM db_data_1.tbl instead of SELECT col FROM tbl, for example.
The documentation says this:
USE statements within stored routines are not permitted. When a routine is invoked, an implicit USE db_name is performed (and undone when the routine terminates). The causes the routine to have the given default database while it executes. References to objects in databases other than the routine default database should be qualified with the appropriate database name.
Why is this so? It seems like a big pain in the xxx neck.
A big use of stored code is the hiding of data from unprivileged users. You can GRANT MySQL users access to stored procedures without granting access to the underlying tables. This restriction ties the tables to the procedures.
A user who has privileges only in the test database shouldn't be able to do this sort of thing.
USE production;
CALL test.get_all_user_private_data();
And, if you're USEing one database and you run stored code that's in a second database, it gets the data from that second database.
Your solution is to consider your stored code (procedures, functions) to be part of the schema definition for each database. They go along with your other data definition operations like CREATE TABLE. Don't try to put them in their own "code library" database, but put them in each database where they're needed.

Where does user-defined variables will be stored into mysql 8.0.16?

Suppose I defined two variables in mysql as follows:
SET #STUDENT_NAME = 'ABCD';
SET #STUDENT_AGE = 25;
Now, When I'm was selecting these two variables value using following:
SELECT #STUDENT_NAME, #STUDENT_AGE;
It was perfectly showing corresponding values, even if I switched the database.
But when I restarted mysql and tried to select these values, It was showing null values of these variables, So where would it be stored actually when I had set values?
Per the manual (emphasis mine):
https://dev.mysql.com/doc/refman/8.0/en/user-variables.html
User-defined variables are session specific. A user variable defined by one client cannot be seen or used by other clients. (Exception: A user with access to the Performance Schema user_variables_by_thread table can see all user variables for all sessions.) All variables for a given client session are automatically freed when that client exits.

How to pre-set MySQL user variables in config files

MySQL allows usage of User-defined variables How to declare a variable in MySQL?
Is there a way for me to pre-set one such constant, so that it's automatically ready to use in PHP scripts, CLI clients and whatnot? As if every session were automatically started with a
SET #whatever = 2.52;
Create a table, that keeps this value and select from it everywhere, when you need it. That will be a simple subselect in sql queries.

Why my MariaDB user-defined variables have trash values?

I'm using MariaDB and I have a problem. This is the code:
https://github.com/analca3/TFG/blob/master/Consultas/extractReplays.sql#L346
I'll explain the problem: I'm using a procedure which has a cursor to iterate over ID's on a table. With each ID, I execute a procedure that extracts some data to a table. But at this procedure I'm using some user-defined variables, and when I use them at second time, I get trash values on them. I know these variables are session-living but I'm resseting all them.
If I execute the internal procedure, close the connection and execute another time, the result table has correct data. What can it be? I'm setting all variables with CROSS JOIN statement. I tried to use local procedure variables but I couldn't use them as counters.

execute stored procedure automatically when a select query want run in mysql

I have a application that connected to my database and execute query on it.
Also I have a stored procedure.
Now I want every time that application try to execute a query on my database, my procedure run and effect (change) results of application query.
Is it possible? And how?
note that my database is mysql and I can not modify the application code.
You might want to take a look into triggers:
http://dev.mysql.com/doc/refman/5.0/en/triggers.html
-- update --
I can think of a different possibility. It's ugly:
You could hide your table behind a view by renaming the table and creating a view with it's original name, then manipulate the data in the select statement of the view. You just have to be sure the view returns the column names the original query did.