Where does mysql server store the stored procedure code? - mysql

In MS SQL Server, it lets you search across all the stored procedures for the instance of some text. Does this exist in MySQL?
This is useful for trying to understand what the impact of changing the schema would be.
"Show me all the code that involves TableX"
How would you query across all stored procedures?

The procedures are stored in table mysql.proc. The code is in column body.
So you could query:
SELECT db, name, body FROM mysql.proc WHERE body REGEXP '[[:<:]]tablename[[:>:]]'

Related

how to convert a sql server store procedure to mysql procedure?

I have to convert store procedure from that is written in sql server, to mysql procedure. I know the their general differences like AS, GO and ; but still mysql procedure has not the same result as sql server. It is noteworthy that tables and their data are the same. can anyone say other differences between them?
I will thank you if you tell me correct answer in this case
I figure out for using mysql parameters like IN b INT in procedure code, I must use b directly! not #b like sql server

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.

Msql: on MYSQL Query Browser and MYSQL WorkBench

I am currently learning mysql workbench, but I have gotten a book that uses mysql query browser. However, when I am trying to download query browser, it is stated that it is discontinued. So I am currently using workbench. However, I find it difficult to create procedure. What is workbench equivalence of procedure? Thanks!
If you are referring to a stored procedure, then those are listed in the schema navigator on the left panel. Expand the desired schema to see sections for Tables, Views, Stored Procedures, and Functions. Right-clicking on Stored Procedures displays an option to "Create Stored Procedure".
Is this what are you looking for?
If so, write-click on Stored Procedures and select Create.
Otherwise please clarify your problem.

Grouping SQL queries

Sometimes an application requires quite a few SQL queries before it can do anything useful. I was wondering if there is a way to send those as a batch to the database, to avoid the overhead of going back and forth between the client and the server?
If there is no standard way to do it, I'm using the python bindings of MySQL.
PS: I know MySQL has an executemany() function, but that's only for the same query executed many times with different parameters, right?
This process works best on inserts
Make all you SQL queries into Stored Procedures. These eventually will become child stored procedures
Create Master Store procedure to run all other Stored Procedures.
Modify master Stored procedure to accept values required by child Stored Procedures
Modify master Stored procedure to accept commands using "if" statements to know which
child stored procedures to run
If you need return data from Database use 1 stored procedure at the time.

I cant see SQL Queries in MySQL Stored Procedure!

I have created numbers of stored procedures in MySQL through remote access. And those procedures worked well.
After a few days when I connected to Database through remote access. All stored procedures were alive but while was trying to call those procedures, I realized that SQL queries inside procedures had gone. Anyone knows the reason?
If SHOW CREATE PROCEDURE (see docs) isn't showing you the procedures (with SQL queries inside) you probably have some permission problems -- you need to be the owner of the procedure or have SELECT access to the mysql.proc table.