Is it possible to INCLUDE other mysql scripts in a composite script? Ideally I do not want to create a stored procedure for the included scripts... For larger projects I would like to maintain several smaller scripts hierarchically and then compose them as required... But for now, I'd be happy to just learn how to include other scripts...
source is a builtin command you can use in the MySQL client tool (which is what you're using to execute the SQL script):
mysql> source otherfile.sql
If you're executing SQL in a stored procedure or with an API, you should know that MySQL client builtins work only in the MySQL client.
MySQL scripts are just a list of commands, to be run in order against the database server. SQL is not a scripting language by any means, so it doesn't behave like one. The only way to "include" other scripts is to concatenate them together when you kick off the script load command.
Related
My hosting provider (JustHost) uses MySQL, and has a bizarre feature in which once you create a stored procedure through PHP MyAdmin, it can never be modified or exported again.
I tried the "SELECT ROUTINE_DEFINITION..." from within PHP MyAdmin but it just returned null. The ONLY way to access this procedure successfully seems to be a Call. It works like a charm with no problems from within PHP MyAdmin and my application, but I cannot access the actual SQL. Is there another SQL script I can run to get the contents? Or, since it works from PHP (i.e. my application), is there a PHP function I can use to echo the contents of the stored procedure onto the browser page?
You should be able to use SQL (either in PHP MyAdmin or writing your own PHP script) to do this. Use SHOW CREATE PROCEDURE to look at the procedure's definition. I'm not sure what version of MySQL you're using, so YMMV with this reference to the manual: http://dev.mysql.com/doc/refman/5.7/en/show-create-procedure.html
The error you're getting in that screen capture implies the SQL you ran cannot see the procedure... Are you running it in the same database where the procedure lives?
Hy.
I'm building a software and then I deploy the sources on to 3 differents mysql databases (hosted on 3 differents places), my question is... is there a way to automatize changes I made in one of the db to the others???
Is there any tool I can use so I do not have to change manually all the databases???
If you want an automated job execution, you must create a scripting tools yourself and execute as your need. But what I see now is you need some way of Database Versioning.
It's hard to understand at first but it's easy enough to dig it down. The versioning process is:
baseline: This is your entire database tables and predefined records in one script. Usually it is recorded on major version such as 1.0.0.sql or 2.0.0.sql. Baseline only executed once.
updates: this is a "patch" for your tables separated in each sql scripts.
view, functions and procedures: each of your views, functions and procedures in separated sql file.
schema_change_log: this table records baseline version and updates patch versions executed in database
The publishing step:
check latest version in schema_change_log
get updates with version bigger than the latest version
execute the updates script
drop all views, functions and procedures
re-apply all views, functions and procedures
I recommend for this to use MySQL Workbench so you can make any changes to the model and then through the menu of Database you can synchronize the changes with your databases.
I have two databases. The first one is a Oracle Database and contains a table that I want to transfer to a MySQL Database. The databases are not on the same server.
Which programming language should I use ?
Is there an other method instead of writing a programm ?
And how can I do it ?
In addition this transfer should happen in a regular frequency. For example every hour.
(I can tell the Task Manager to run a programm at a specific time so that would be no problem if I would have a program.)
I already tried Python, but as I have to install every single module for MySQL and Oracle and the Oracle module is not working I decided to give up on this.
It should be simple and quick.
If I were to want to create a PHP function that does the same thing as the Export tab in phpMyAdmin, how could I do it? I don't know if there is a MySQL function that does this or if phpMyAdmin just builds the export file (in SQL that is) manually. Without shell access. Just using PHP.
I tried the documentation for mysqldump, but that seemed to require using the shell. I'm not quite sure what that even is -- maybe my question is: how do you use shell?
My silly idea is to allow non-technical users to build a site on one server (say a localhost) using MySQL then export the site, database and all, to another server (eg. a remote server).
I think I'm pretty clear on the Import process.
You can check the phpMyAdmin source code (an advantage of open-source software). Check the export.php script and the supporting functions in the libraries/export/sql.php script file.
In summary, what phpMyAdmin does is:
get a list of the tables in the given database (SHOW TABLES FROM...),
get the create query for each table (SHOW CREATE TABLE...),
parse it and extract column definitions from it,
get all data (SELECT * FROM...)
build a query according to column data.
I've written similar code for my own apps (for backup purposes, when the GPL license of phpMyAdmin doesn't allow me to use it), however I use DESCRIBE to get column definitions. I think they rather parse the SHOW CREATE TABLE output because contains more information than DESCRIBE output.
This way to generate SQL sentences requires a bit of care to handle the escaping but it allows for some flexibility, as you can convert types, filter or sanitize data, etc. It is also a lot slower than using a tool like mysqldump and you should take care of not consuming all available memory (write soon, write often, don't keep everything in memory).
If you will implement a migration process (from server to server) maybe it would be easier to do it with some shell scripting and calling mysqldump directly, unless you will do everything with PHP.
I have created a folder that has four scripts namely ScriptA.Sql, ScriptB.Sql, ScriptC.Sql.
The fourth one I want to create MasterScript.Sql that runs all the other scripts in a transaction. How do I accomplish this.
Any ideas and suggestions are appreciated!
I think the best approach is to make in a language other than SQL, such as PowerShell or C# or Python, which interacts well with both files and SQL Server.
However, if you really want to do it as a T-SQL script you can use xp_cmdshell and Osql to call the scripts. Of course, this presumes that xp_cmdshell is enabled on your server, it is disabled by default and can open up security vulnerabilities if enabled.
Edit: Also, depending on your final purpose you could consider converting your scripts into stored procedures and stored procedures invoke each other quite readily in the normal way.