Getting message Review the SQL script to be applied on the database - mysql

I am getting the following message while creating a stored procedure in MySQL Workbench:
"Review the SQL script to be applied on the database"
I have several tables inside the database but the stored procedure I am writing will be
used only for one table. Since, the SQL script of stored procedure is gonna apply on the whole database, I am wondering if it's gonna affect other tables as well? I don't want other tables to get disturbed because of this script.
Please provide your inputs as I am doing this for the first time.
Question #2:
Why do I see "DELIMITER $$" as the first statement while creating a routine before the following statement?
CREATE PROCEDURE `mydatabase`.`myfirstroutine` ()
BEGIN
Thanks

1) MySQL Workbench offers the option to review the generated SQL script before it is sent to the server. This way you can check it for possible problems.
2) The DELIMITER command is usually necessary to switch the current delimiter that ends a single statement (which is by default a semicolon) to something else because the stored procedure code itself needs the semicolon to separate individual commands. However the sp code must be sent as a whole to the server.
A few more details: the DELIMITER keywword is a client keyword only, that means the server doesn't know it and doesn't need it. It's an invention for clients to properly separate sql commands before sending them to the server (you cannot send a list of commands to a server, only individual statements).
In MySQL Workbench however, especially in the object editors where you edit e.g. the sp text, adding the DELIMITER command is essentially nonsense, because there's only this sp code, hence nothing to separate. This might disappear in future version but for now just ignore it.

Related

How to update stored procedures in multiple databases in mysql

I am very new to mysql and I have I a situation where I need to update all my stored procedure in all my database. for example I have 10 database just say:
client_1,client_2,client_3,.....client_10.
Every database have same stored procedure just say:
proc_1,proc_2,proc_3,proc_4.
So if I made any changes to one of my stored procedure then it should get updated in all other database So that I don't have to do it manually.
I know the similar question have been asked but I am looking for some different approach. So what I want is some kind of mysql query or something like that in which we will pass the name of the database like:
client_1, client_3, client_8
and changes will only made to this databases.
I am using heidiSql- 10.2 with MySQL 5.6.
Thanks.
I am not entirely sure what you are wanting to do but I think you want something like this. First save the definition of your stored procedure to a file. Make sure it doesn't contain an schema references like client1.tableA. You want it to be able to run in any copy of your schema correctly. Be sure to follow the syntax rules defined by MySQL
Defining Stored Programs
Then once the stored procedure is saved you can use the mysql command line to run it for each client you want to update.
You would first connect to the database server using the mysql command line. Then issue a USE command to activate the first client database. Then run the script using the SOURCE command. See MySQL Batch Commands Then repeat for each client.
USE client1;
source c:\temp\storedProcedure.sql
USE client2;
source c:\temp\storedProcedure.sql
If this is not exactly what you needed hopefully it gives you some ideas to get you what you need.
Note that you could do the connection to the database and execute these commands via batch file instead of manually if you wanted to.
There are no statements in MySQL that create/drop/alter multiple procedures at once. You can only change one procedure at a time.
You can write an SQL script that includes a series of statements. But it's up to you to write that script.
You may write some script in Python (or other favorite language) that outputs the SQL script.
I don't know HeidiSQL, but I doubt it has any facility to apply the same change to many procedures. Nor does any other MySQL client that I'm aware of.

Call a Stored Procedure in SSIS Data Source

I am trying to call a stored procedure in SSIS OLE- DB DataSource (My Datasource would be SQL Server 2012).
I tried a procedure call SQL statement under SQL Command option but when I click the preview button I am getting an error.
Please guide me how to resolve this error. I goolged but nothing works for me.
I think the issue you are having is in SSIS often takes the first Select statement it finds and tries to validate it for column names, this happens especially with very big procedures. The trick I have found to get this to work is, right off the bat throw something like :
IF 1 = 0
Begin
Select all columns you want
END
This code will never get execute but it hints SSIS to make those columns the ones in the data flow. Just be sure to update this list as you update your last select.

How to return SQL statement that caused an exception from a stored procedure to SSIS

We have a SSIS package that calls a stored procedure through an EXECUTE SQL TASK component. The stored procedure contains a LOT of different pieces of sql code that gets build dynamically and then executed via exec strSQL within the stored procedure. The whole system is built that way and we cannot redesigned at this point. The problem is that when something fails within the stored procedure is hard to figure out from SSIS what SQL statement from the stored procedure caused the exception/failure. What we have right now and is working is the package onError event with code to read the System::ErrorDescription which is helpful to display the error in SSIS and then send an email with the error, this is working. What I'm looking for to add is to have a System Variable or some other way to display the actual SQL (the one that caused the exception within the stored procedure) in SSIS to I can include that in the email. Any ideas? Thanks.
I have a solution. Table variables are not roled back in a catch block and rollback statement.
So put the sql statements before they run into a table varaible with an nvarchar (max) datatype. Make sure your proc uses try catch blocks and transactions. In the catch block, perform a rollback if need be and then insert the contents of the table variable and a datetime to a logging table. NOw you have a record of exactly what queries were run. You can also create a separate table varaible to store the data you are attempting to insert or update if that is also an issue.
When you run a package by using F5 and a SQL statement fails you can check the execution results tab, but unfortunately this only shows the first line or two of your SQL statement.
Instead of running the package by using F5, run it using Crtl+F5. This will open a terminal window and run the package as though it was called from the command line. As each task runs it will output log information, if the task uses a SQL statement and it fails it will output the full SQL statement.
Ctrl+F5 is called 'Start Without Debugging' yet I always think it is a better to debug a package.

How do I save a stored procedure in SQL Server 2008 R2?

I am writing a SQL Server stored procedure for the first time and am unclear on how I can "save" my stored procedure so that it appears under Programmability, Stored Procedures in the Object tree.
The CREATE PROCEDURE procedureName statement creates the procedure.
You just need to execute it once and it will save the procedure to your database.
Make sure to select the correct database you want to save the procedure to, either by selecting it in the top left hand corner of SQL Server Management Studio, or by putting the following at the top of your code:
USE databaseName
Also note, if there are any syntax errors, it won't "save" the procedure.
While you are learning SQL Server and Management Studio, you may find it very helpful to become familiar with the built-in templates for creating everything from databases to tables to stored procedures and more. You locate the templates in Template Explorer under the View menu.
The first example in this walk-through with screenshots shows how to use the template for creating a stored procedure. That template includes a placeholder for the schema name (often just dbo).
You will also want to include a USE statement to make sure that the stored procedure is created in the correct database.
In addition to helping you to learn proper coding practice, using these templates can be a real time-saver and help you to avoid typos and syntax errors even after you becomem proficient in SQL.
And when you get really good at it, you can create your own templates.
Edit: Here is a very basic CREATE PROCEDURE statement:
USE MyDatabase
GO
CREATE PROCEDURE dbo.MyProcedure
AS
SELECT FirstName, LastName, Address, City
FROM Customers
ORDER BY LastName
GO
After you run that, you can run this line to check that the procedure has been created and that it is working correctly:
EXEC dbo.MyProcedure
you just use a create statement:
http://msdn.microsoft.com/en-us/library/aa258259(v=sql.80).aspx

MySQL Stored Procedure that Evaluates Whether Another Procedure Runs or Not

I am writing a stored procedure that needs to execute a .sql source file. The MySQL stored procedure must run the .sql script and provide a return value based on whether the .sql file exists, if an error occurs or if it exists and executes without errors. Can anyone provide me with some direction on the proper syntax to use in my stored procedure to call the .sql file? Is it simply CALL myfile.sql?
Thanks,
Sid
There are no commands in MySQL language to read a .sql file.
The "source" keyword is an internal command of the MySQL command line.
If you want to execute one SQL command from a file, you can do the following:
1) read the file into a table, using the LOAD DATA INFILE command (but some security restrictions apply);
2) load the first record from that file into a variable;
3) create a prepared statement from the variable;
4) execute the prepared statement.
The above procedure is fraught with restrictions and problems, however.
The first and most notable one is that allowing execution of generic code is a security liability. The second one is that you will have little control on what you are executing.
If you really must, you could use MySQL Proxy (http://launchpad.net/mysql-proxy), which can perform the above operations with much more control and flexibility on each step.