Insert Values on Input Variable Array - mysql

I have the following input of a list of clients I get from an external application that comes into a variable in a MySQL stored procedure such as this:
"2841,2212,1231,xxxx,...,1221"
...called input_clients
I want to take this list and insert it all in a temporary table with the field client_id.
I am wondering if someone can either point me to an existing script or show me how I can format this in a stored procedure that input so it will work as
INSERT INTO select_Clients(client_id) VALUES (input_client1), (input_client2)
because currently input_client stores all those clients in 1 string.

You can create a prepared statement inside the procedure as string and then execute it:
SET #input = '2841,2212,1231,1221';
DROP TEMPORARY TABLE IF EXISTS select_Clients;
CREATE TEMPORARY TABLE select_Clients(client_id INT);
SET #sql = CONCAT('INSERT INTO select_Clients(client_id) VALUES (', REPLACE(#input, ',', '),('), ')');
PREPARE stmt FROM #sql;
EXECUTE stmt;
http://rextester.com/FPDW88636

Related

MySQL values statement as a parameter?

is it possible to insert into a MySQL table with the whole VALUES block of code being a parameter?
Parameter would be:
("(638) 833-5496","enim.curabitur#hotmail.couk","Spain"),
("(293) 742-0229","odio.semper#yahoo.net","Belgium"),
("1-265-156-4304","tincidunt.dui.augue#outlook.net","Ireland"),
("1-833-780-2553","scelerisque.scelerisque#aol.com","France"),
("(619) 691-0656","ac.risus.morbi#icloud.org","Costa Rica");
Insert statement would be
INSERT INTO `myTable` (`phone`,`email`,`country`)
VALUES
{parameter}
Is it possible to do as an Insert statement, stored procedure, or anything?
I was able to solve this by utilizing the EXECUTE command and passing the string as a variable and then compiling it into one SQL statement.
CREATE DEFINER=`admin`#`%` PROCEDURE `insert_string_storedprocedure`(bubble_variable text)
BEGIN
set #insert_string = "INSERT INTO `myTable`
(`phone`,`email`,`country`)
VALUES
(insert_statement)
AS new
ON DUPLICATE KEY UPDATE
email = new.email;";
set #fullcommand = REPLACE(#insert_string, '(insert_statement)', bubble_variable);
PREPARE stmt FROM #fullcommand;
EXECUTE stmt;
END

MySQL sorting data with trigger to other tables - resolved

I have main_table:
SELECT PORT_ID, DATA from main_table
i need run trigger AFTER INSERT main_table which sort DATA to the other tables:
INSERT INTO #PORT_ID (DATA) VALUE (#DATA)
return an error message:
dynamic sql is not allowed in stored function or trigger resolved.
Any idea?
Many thanks
Workarround
I do a simply workarround, 1, save a SQL query into new table as a whole text. 2, run an EVENT per second with EXECUTE saved query
This is to big for a comment.
A simple Procedure that only executes the insert as prepared statement
CREATE DEFINER=`root`#`localhost` PROCEDURE `procedure_stmt`(IN test TEXT)
BEGIN
SET #sql = test;
PREPARE stmt2 FROM #sql;
EXECUTE stmt2;
END
ANd as a trigger
BEGIN
SET #sql = CONCAT('INSERT INTO ','gps_', NEW.PORT,' (DATA) VALUES (',NEW.DATTA,')');
CALL procedure_stmt(#sql);
END$$
DELIMITER ;
If you had posted tables and data i would have tested it further.

How do I select every row from a table based on a string containing the name of the table?

In MySQL, I have a number of procedures which are more or less identical - they all perform the same (or very similar) operations, but they perform it on different tables.
I'd like to reduce these to one procedure, parameterized by table name, if possible. For example, suppose I wanted to execute a generic select:
SELECT * FROM TableFor("TableName")
Is this (or anything similar) possible in MySQL? Is it possible in any SQL dialect?
Per Tomva's Answer
A full example:
DROP PROCEDURE IF EXISTS example;
CREATE PROCEDURE example(IN tablename VARCHAR(1000)) BEGIN
SET #statement = CONCAT('SELECT * FROM ', #tablename);
PREPARE statement FROM #statement;
EXECUTE statement;
DEALLOCATE PREPARE statement;
END;
CALL example('tablename');
You can do this with a prepared statement.
It will be something along the lines of
SET #stat = CONCAT('SELECT * FROM ', #tab');
PREPARE stat1 FROM #stat;
EXECUTE stat1;
DEALLOCATE PREPARE stat1;
Dynamic SQL does not work in a function, so make a Stored Procedure from this, and you will be able to provide the table parameter.
I am going to assume you know what a stored procedure is (I hope you do otherwise my answer will be useless)
First create a table object in your procedure
declare #tablenames table(name varchar)
insert into #MonthsSale (name) values ('firsttable')
insert into #MonthsSale (name) values ('secondtable')
...
You can add this little line to suppress the rows affected messages:
SET NOCOUNT ON
Then create a cursor for this table and a variable to save your table name
DECLARE #TABLENAME VARCHAR
DECLARE tables_cursor CURSOR FOR SELECT name FROM #tablenames
Then loop through cursor and execute your code for each table name
OPEN Tables_cursor
FETCH NEXT FROM Tables_cursor INTO #Tablename
WHILE ##FETCH_STATUS = 0
BEGIN
YOUR CODE USING THE #Tablename
END
CLOSE Tables_cursor
DEALLOCATE Tables_cursor

Using Dynamic MySQL to create a new table

I want to change this query:
CREATE TABLE diablo.diablo_sales2 LIKE diablo.diablo_sales;
to a dynamic one like this:
set #old = 'diablo_sales';
set #new = 'diablo_sales2 ';
set #schema = 'diablo';
set #query = 'create table #schema.#new like #schema.#old';
Is this even possible in MySQL? How can I execute #query?
EDIT: below is the whole query I'm trying to make dynamic
-- create same table structure - blank table
CREATE TABLE diablo.diablo_sales2 LIKE diablo.diablo_sales;
-- add the new column(s)
ALTER TABLE diablo.diablo_sales2
add column new_column decimal(10);
-- insert the old data into new table - new column will remain blank
INSERT INTO diablo.diablo_sales2
(
column1,
column2
)
SELECT * FROM diablo.diablo_sales;
-- rename the new table to its original name. Drop the old table.
RENAME table diablo_sales TO `old`, diablo_sales2 TO diablo_sales;
DROP TABLE old;
You can use the SQL Syntax for Prepared Statements:
SET #sql := CONCAT('
CREATE TABLE `',REPLACE(#schema,'`','``'),'`.`',REPLACE(#new,'`','``'),'`
LIKE `',REPLACE(#schema,'`','``'),'`.`',REPLACE(#old,'`','``'),'`
');
PREPARE stmt FROM #sql;
EXECUTE stmt;
DEALLOCATE PREPARE stmt;
I have taken the liberty of quoting the identifiers and calling REPLACE() to escape any quotes contained therein, in order to protect against SQL injection.

Changing Multiple DB Fields to Lower Case

With phpMyAdmin, I can use the following SQL to change all values in the table.field mytable.Site to lower case...
UPDATE my_table SET Site=LOWER(Site)
I have a zillion tables that have this same field, and I'd like to change all of them to lower case. Is there a SQL command that will do that - change EVERY field named Site in every table to lower case (preferably without having to list every table that has that field)?
Not EXACTLY what you want,but pretty close.Tested on my machine.
First create a procedure
delimiter //
CREATE PROCEDURE test(IN tbl CHAR(64))
BEGIN
SET #s = CONCAT('UPDATE ',tbl,' SET Site=LOWER(Site)' );
PREPARE stmt FROM #s;
EXECUTE stmt;
DEALLOCATE PREPARE stmt;
END
//
delimiter ;
And for finding tables with a certain column name:
SELECT TABLE_NAME
FROM INFORMATION_SCHEMA.COLUMNS
WHERE COLUMN_NAME IN ('Site')
AND TABLE_SCHEMA='YourDB';
For calling the procedure
CALL test('tableName')