Viewing contents of stored procedure from PHP - mysql

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?

Related

Does a MySQL backup save custom functions, events, and queries?

I successfully did a backup of my database, dropped the DB in workbench and created a new one, and attempted to restore it. First, it didn’t restore until I ran the creation of a custom function first (which is something I use in multiple views), and when I did restore it, the event I created wasn’t present.
FUNCTION library.daysout does not exist
I can understand why a query I had open would not be restored (although if you can tell me how to do that, please do,) but how can I make sure my custom function and event restore (especially since the DB doesn’t restore till I run the creation of the function before)?
A default database dump or export does not include stored procedures or stored functions by default.
MySQL Workbench has a checkbox to include stored procedures and functions in an export. You have to check this, and it's not checked by default.
This option is shown in the screenshots in the manual here: https://dev.mysql.com/doc/workbench/en/wb-admin-export-import-management.html Although it isn't described clearly in the text.
Likewise if you use the command-line mysqldump tool, you must use the --routines option to include stored procedure and stored function definitions in the export. This option is documented here: https://dev.mysql.com/doc/refman/8.0/en/mysqldump.html#option_mysqldump_routines

Is it possible to run a stored procedure using sqlcmd and save the output into an Excel or .csv file?

I have a stored procedure which outputs 2 tables. I would like to save the output into two .csv files or into one Excel file. Is it possible to do so?
I am not sure how much it can help you, but you can create a DLL to generate CSV by excepting some input data and use that dll inside extended stored procedure. Call that store procedure using sqlcmd.
Extended stored procedure
But be aware Microsoft is suggesting to not use extended stored procedure as it will not be available in future.
Important
This feature will be removed in a future version of Microsoft SQL Server. Do not use this feature in new development work, and modify applications that currently use this feature as soon as possible. Use CLR Integration instead.

Can't run queries in database console from php

I've just readl the Database Code Completion while working in PHP on your web support. So I've configured the database, I can complete the queries with ctrl+enter but I can't run query on console. I send you some pictures to explain the problem.
I've the database configured, so I can check the tables and all the dates, and run queries directly at the console, but can't send from php the query.
Thanks for all, Manu.
You need to connect to the DB before you run any queries on it (example in MySQL here: http://www.w3schools.com/php/php_mysql_connect.asp, don't forget to close it when you don't need it anymore. )
The result is not plain text, you need to go through it in a loop (example including the connection here: http://www.w3schools.com/php/php_mysql_select.asp ).

SQL Server: stored procedure saves with errors

I am using SQL Server 2008 and SSMS 2012. I have a stored procedure that references a table that does not exist. The editor displays red underlines on the offending table to indicate that something is wrong.
However when I execute the query, I get the message
Command(s) completed successfully.
This is extremely annoying. I also connected to the engine from another machine and it experienced the same problem, which implies its on the server, not ssms. Is there some kind of setting on the database that determines whether the database checks the syntax of stored procedures? PLEASE HELP!
Clarification:
I know that the syntax is wrong. The problem is that SSMS allows me to execute the CREATE or ALTER statement without error even when it references a table that does not exist. I want it to fail. Usually it does, but for some reason it suddenly stopped giving errors. I want it to give me errors. How do I do this?
Your syntax is fine and that is checked when you create the stored procedure. The existence of tables is however not checked until the stored procedure is compiled and that happens when the stored procedure is executed.
What's going on is that the IDE in the management studio hasn't had the schema model refreshed. Since the local instance of SSMS doesn't know the table exists, it throws a redline under the table name; when you actually run the sproc/query, the code sent to the database evaluates properly and runs.
To refresh the SSMS local data, try pressing Ctrl-Shift-R, as described here.
Edit:
You might want to look into Deferred Name Resolution
You will not get an error message when you CREATE or ALTER, but you can check your SPs for missing dependencies with a script afterwards.
Please check my answer to the related question here (I just post a link to avoid duplication):
I'm looking for a reliable way to verify T-SQL stored procedures. Anybody got one?

composing multiple mysql scripts

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.