Making stored procedures not unique to a particular database - mysql

Okay, I have a theoretical database server that has several databases that contain the same schemas, but different data that can't be consolidated. Would it be possible to create procedures on the server that are database agnostic so that someone can run the procedure and input the database name and other parameters and the procedure calls the proper database.
Similar to this:
CREATE PROCEDURE getCatsForPerson (VARCHAR database, VARCHAR personName)
BEGIN
USE database;
select cats.catName, cats.ownerName from cats
where cats.ownerName = personName;
END
Or should I simply just create the procedure in each database?

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.

Stored procedure to get table data from different database

Can we get table data from multiple database using stored procedure?
Yes you can. You need to access table using database name.
e.g.
Select * From myDb1.Table1
Select * From myDb2.Table2
Make sure you have access to both the database or both the databases are linked if they are on different servers

Are all kinds of procedural code stored on server side, once created? How are they stored?

Are all kinds of procedural code (stored procedure, functions, views
and triggers) stored on database or database management system
server, once created, regardless of how they are created (for
example, typing their definitions interactively in a command-line
client of the database management system)? I am asking this because
in the command-line client of a RDBMS (such as postgresql, mysql, server sql), when typing a query statement, the query statement
isn't stored any where.
in an interactive command-line REPL for a general purpose programming language, when typing the definition of a function or
procedure, the function or procedure will not exist after closing
the session of the REPL.
How are procedures, functions and triggers and views stored?
Are they stored per table, per database or per database management
system server?
If they are stored per database, are they stored in a separate table
from the tables that store the actual data and are in the same database? For example, in every database, besides those tables which store the actual data, is there a
table which stores the stored procedures of the database, a table
the functions of the database, a table the views of the database,
and a table the triggers of the database?
Similar questions if they are stored per table, or per database
management system server.
Thanks.

Where does mysql server store the stored procedure code?

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[[:>:]]'

SQL Server stored procedure for inserting multiple rows in one table and single row in other table

I am in need of a stored procedure for sales transaction. In a single SP I need to store CustomerID in one table and list of products purchased (multiple rows) in another table.
Can any one give me an best example?
Thanks in advance.
Table-Valued Parameters is a new feature introduced in SQL SERVER 2008. In earlier versions of SQL SERVER it is not possible to pass a table variable in stored procedure as a parameter, but now in SQL SERVER 2008 we can use Table-Valued Parameter to send multiple rows of data to a stored procedure or a function without creating a temporary table or passing so many parameters.
You can read about it here
for more information about using it with ado
check this great article
SQL Server 2008 Table-Valued Parameters and C# Custom Iterators: A Match Made In Heaven!
well in the stored procedure you can use any many insert commands as you want in any table you want, as your question is not clear enough that i write the exact stored procedure you want, I'm writing an example.
use [databasename]
go
create procedure [dbo].[my_stored_procedure](#customerid int) as
begin
insert into [customerstable](customerid) values (#customerid)
insert into [someothertable](somefieldname1, somefieldname2) values(somefield1value, somefield2value)
insert into [someothertable2](somefieldname1, somefieldname2) values(somefield1value, somefield2value)
end