How to do MySQL bulk insert using stored procedure? - mysql

I'm exclusively using Stored Procedures when doing database operations with MySQL. There is a need to insert thousands of records periodically. Is there a way to pass a collection as a parameter to stored procedure? If there is, how does the stored procedure go about inserting the received parameter as one bulk insert?

My understanding of stored procedures is that the parameters cannot contain any code that will be executable commands like INSERT DELETE UPDATE only variables. So you cannot pass a whole INSERT comand to a stored procedure. Hence store procedures give protection from SQL injection. Here is the excellent and very Turing like explanation of the rationale for the separation of commands and parameters in stored procedures by #Polynomials https://security.stackexchange.com/questions/25684/how-can-i-explain-sql-injection-without-technical-jargon/25710#25710
So batch inserts will either be LOAD DATA INFILE as #O. Jones said in comments above or an INSERT stored procedure where you use the language of your choice to iterate the population of the parameters insert by insert.

Related

MySQL Stored Procedure in SSIS destination

Is it possible to use MySQL stored procedure in DataFlow task destination?
While SQLServer is the source, MySQL DB is the destination, and I would like to use stored procedure to normalize the data. Currently the process uses ADO NET Destination with ADO.NET connection manager to insert data into a single table and it works.
Well, Yes you can use it. But take care about Connection of MySQL and Parameters you passed to Stored Procedure.
Current System and use of StoredProcedure both will work for same.
You can't use a stored procedure as a destination, but you can use a staging table as a destination and then call a stored procedure that normalizes your data as it sends it to the destination tables.

Is it possible to call a stored procedure from a trigger in MySQL?

I have a stored procedure, which upon calling, will update many tables I created.
Now I want the update to be done automatically.
Every Friday, all the databases will be updated with new data. My tables are created base on the tables in these databases, and I want my tables to be updated as well.
So, what I want to achieve is, when the tables in the databases are being updated, it will trigger, or whatever, the stored procedure to update the tables
however, I read that trigger cannot use the CALL statement to invoke stored procedures that return data to the client or that use dynamic SQL, so i am stuck here. is there any other method I can use?

Grouping SQL queries

Sometimes an application requires quite a few SQL queries before it can do anything useful. I was wondering if there is a way to send those as a batch to the database, to avoid the overhead of going back and forth between the client and the server?
If there is no standard way to do it, I'm using the python bindings of MySQL.
PS: I know MySQL has an executemany() function, but that's only for the same query executed many times with different parameters, right?
This process works best on inserts
Make all you SQL queries into Stored Procedures. These eventually will become child stored procedures
Create Master Store procedure to run all other Stored Procedures.
Modify master Stored procedure to accept values required by child Stored Procedures
Modify master Stored procedure to accept commands using "if" statements to know which
child stored procedures to run
If you need return data from Database use 1 stored procedure at the time.

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

how do I create a stored procedure from within a stored procedure

I would like to manage the deploy of stored procedures by first inserting them into a table, then, when it's time to deploy.... use a stored procedure to create all of the stored procedures. (Execute seems to be limited to CRUD)
This is not possible with MySQL. In stored procedures and functions you have even more restrictions in language use then in prepared statements.
The list of acceptable commands for prepared statements (see manual) does not include create procedure.
I'm not sure if this is what you looking for, but its has a good example and a reference at the bottom.
http://forums.mysql.com/read.php?98,219523,219787#msg-219787
Matthew H.