Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

mySQL query with function in a different database?

Posted on 2014-04-14
4
Medium Priority
?
366 Views
Last Modified: 2014-04-21
- I have a database called REFERENCE.  It stores my functions and stored procs,
e,g,  procDoSomeStuff() with code like SELECT stuff FROM localDatabase;
- I have a multitude of databases containing data.
e.g. DATABASE1

I am in DATABASE1 issuing a long list of queries, some using functions.
e.g. CALL REFERENCE.procDoSomeStuff;

Problem is, REFERENCE.procDoSomeStuff() thinks that all its queries should be executed in the scope of REFERENCE.
It looks for REFERENCE.localDatabase rather than DATABASE1.localdatabase as I was expecting:  in other words, it looks like REFERENCE procedures are not really "loaded" into the DATABASE1, they spin off their commands into REFERENCE.  Weird.

So how do I "parametize" the database name?  How do I modify my SP and functions in REFERENCE to accept a database name I supply?  Or if it is easier, convince that all the code in a function should run in DATABASE1

I have lots of databases, but all use the same functions and stored procs, hence the need to share them in a common database I can call.

Many Thanks.
0
Comment
Question by:Francois Koutchouk
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
4 Comments
 
LVL 35

Expert Comment

by:Dan Craciun
ID: 40000830
Probably overkill for your needs, but if you're comfortable with C++ you can write your functions as UDF (user defined functions): http://dev.mysql.com/doc/refman/5.0/en/adding-udf.html

That would have the advantage that your functions would work just like native MySQL functions and will be available for all databases.

HTH,
Dan
0
 
LVL 143

Accepted Solution

by:
Guy Hengel [angelIII / a3] earned 2000 total points
ID: 40001255
I confirm, please read up from here:
http://dev.mysql.com/doc/refman/5.0/en/stored-routines-syntax.html
 As of MySQL 5.0.1, a stored procedure or function is associated with a particular database. This has several implications:
    When the routine is invoked, an implicit USE db_name is performed (and undone when the routine terminates). USE statements within stored routines are not permitted.
.... 

Open in new window

so, you cannot do it with mysql, unless you specify the database you want to use the tables/views/etc in an parameter, and inside the procedure you use dynamic sql ...
http://dev.mysql.com/doc/refman/5.0/en/sql-syntax-prepared-statements.html

which will presumably not be much simpler to implement neither ..
0
 
LVL 2

Author Comment

by:Francois Koutchouk
ID: 40004910
I got it to work by modifying my numerous stored procedures, using the technique:

	SET @myQuery = CONCAT[into a query];
	PREPARE stmt1 FROM @myQuery;
	EXECUTE stmt1;
	DEALLOCATE PREPARE stmt1;

Open in new window


Which leaves the functions.  They now all generate the following error:
Dynamic SQL is not allowed in stored function or trigger

Half way down that page, it confirms that I cannot do dynamic SQL in a function.

Any way around in SQL -- I cannot use an external program (like C++ as Dan suggested, thank you)?
Perhaps turn the functions into procedures, write to a temporary database1.table, then read from that in the continuing code?

And for my own education, why in the world are procedures supporting dynamic SQL and not the functions?
0
 
LVL 143

Expert Comment

by:Guy Hengel [angelIII / a3]
ID: 40005901
mysql was open-source, and volunteers worked on the features. if nobody asked for this feature, or nobody was assigned the task, it simply wasn't developed.
there may also be technical issues on this, as for example in MS SQL it's not possible neither to have dynamic sql in functions ...
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

When table data gets too large to manage or queries take too long to execute the solution is often to buy bigger hardware or assign more CPUs and memory resources to the machine to solve the problem. However, the best, cheapest and most effective so…
Containers like Docker and Rocket are getting more popular every day. In my conversations with customers, they consistently ask what containers are and how they can use them in their environment. If you’re as curious as most people, read on. . .
In this video, Percona Solution Engineer Dimitri Vanoverbeke discusses why you want to use at least three nodes in a database cluster. To discuss how Percona Consulting can help with your design and architecture needs for your database and infras…
In this video, Percona Solutions Engineer Barrett Chambers discusses some of the basic syntax differences between MySQL and MongoDB. To learn more check out our webinar on MongoDB administration for MySQL DBA: https://www.percona.com/resources/we…

721 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question