Mysql 5.7 Stored Procedure error - mysql

I an trying to create stored procedure in following format :
DELIMITER $$
CREATE PROCEDURE `CreateInsertLocation` (tableName VARCHAR(255),ts BIGINT(20),systs INT(20),lat FLOAT,lon FLOAT)
BEGIN
DECLARE FoundCount INT;
SELECT COUNT(1) INTO FoundCount
FROM information_schema.tables
WHERE table_schema = 'DB'
AND table_name = tableName;
IF FoundCount = 1 THEN SET #sql = CONCAT('INSERT INTO ',tableName,'
(timestamp, lattitude, longitude,systime)
VALUES
(','ts','lat','lon','systs')');
PREPARE stmt FROM #sql;
EXECUTE stmt;
ELSE
SET #sql = CONCAT('CREATE TABLE `',tableName,'`(
`id` bigint(20) NOT NULL DEFAULT '0',
`timestamp` bigint(20) NOT NULL DEFAULT '0',
`lattitude` float NOT NULL DEFAULT '0',
`longitude` float NOT NULL DEFAULT '0',
`systime` bigint(20) DEFAULT NULL,
KEY `LocIdx` (`vtuId`,`timestamp`),
KEY `SysIdx` (`vtuId`,`systime`),
PRIMARY KEY (id)');
PREPARE stmt FROM #sql;
EXECUTE stmt;
SET #sql = CONCAT('INSERT INTO ',tableName,'
(timestamp, lattitude, longitude,systime)
VALUES
(','ts','lat','lon','systs')');
PREPARE stmt FROM #sql2;
EXECUTE stmt;
END
$$
DELIMITER ;
-- When I am trying to execute this query in Mysql 5.7 I am getting following error
ERROR 1064 (42000): You have an error in your SQL syntax; check the
manual that corresponds to your MySQL server version for the right
syntax to use near '');
PREPARE stmt FROM #sql;
EXECUTE stmt;
ELSE SET #sql = CONCAT' at line 10
Can anyone help to improve this sotred procedure ?

This should work. Pay attention to order of single quotes as it can get tricky.
DELIMITER $$
CREATE PROCEDURE `CreateInsertLocation` (tableName VARCHAR(255),ts BIGINT(20),systs INT(20),lat FLOAT,lon FLOAT)
BEGIN
DECLARE FoundCount INT;
SELECT COUNT(1) INTO FoundCount
FROM information_schema.tables
WHERE table_schema = 'DB'
AND table_name = tableName;
IF FoundCount = 1 THEN
SET #sql = CONCAT('INSERT INTO ',tableName,' (timestamp, lattitude, longitude,systime) VALUES (',ts, ',', lat, ',', lon, ',', systs, ')' );
PREPARE stmt FROM #sql;
EXECUTE stmt;
ELSE
SET #sql = CONCAT('CREATE TABLE `',tableName,'` (
`id` bigint(20) NOT NULL DEFAULT 0,
`timestamp` bigint(20) NOT NULL DEFAULT 0,
`lattitude` float NOT NULL DEFAULT 0,
`longitude` float NOT NULL DEFAULT 0,
`systime` bigint(20) DEFAULT NULL,
KEY `LocIdx` (`vtuId`,`timestamp`),
KEY `SysIdx` (`vtuId`,`systime`),
PRIMARY KEY (id) )' );
PREPARE stmt FROM #sql;
EXECUTE stmt;
END IF;
SET #sql = CONCAT('INSERT INTO ',tableName,' (timestamp, lattitude, longitude,systime) VALUES (',ts, ',', lat , ',', lon, ',', systs, ')');
PREPARE stmt FROM #sql;
EXECUTE stmt;
END
$$
DELIMITER ;

You are using single quote (') for the concat string of your create table statement and the default values within it.
Either put the default values in double quotes ("), escape them (\') or remove them as they are all numberic types.
Edit: Your parameters for the insert statements are also put into quotes which looks wrong, too.

create schema mine;
use mine;
delimiter $$
CREATE PROCEDURE `CreateInsertLocation` (tableName VARCHAR(255),ts BIGINT(20),systs INT(20),lat FLOAT,lon FLOAT)
BEGIN
DECLARE FoundCount INT;
SELECT COUNT(1) INTO FoundCount
FROM information_schema.tables
WHERE table_schema = 'DB'
AND table_name = tableName;
IF FoundCount = 1 THEN SET #sql = CONCAT('INSERT INTO ',tableName,'
(timestamp, lattitude, longitude,systime)
VALUES
(','ts','lat','lon','systs\')');
PREPARE stmt FROM #sql;
EXECUTE stmt;
ELSE
SET #sql = CONCAT('CREATE TABLE ',tableName,'(
`id` bigint(20) NOT NULL DEFAULT \'0\',
`timestamp` bigint(20) NOT NULL DEFAULT \'0\',
`lattitude` float NOT NULL DEFAULT \'0\',
`longitude` float NOT NULL DEFAULT \'0\',
`systime` bigint(20) DEFAULT NULL,
KEY `LocIdx` (`vtuId`,`timestamp`),
KEY `SysIdx` (`vtuId`,`systime`),
PRIMARY KEY (id)');
PREPARE stmt FROM #sql;
EXECUTE stmt;
SET #sql = CONCAT('INSERT INTO ',tableName,'
(timestamp, lattitude, longitude,systime)
VALUES
(','ts','lat','lon','systs\')');
PREPARE stmt FROM #sql2;
EXECUTE stmt;
END if;
end
$$
DELIMITER ;
You can try above code;
Firstly you need to escape all Single quote '
And you forgot to close ENDIF.

In the marked comment, this code was missing a quote to complete concat correctly
$$
CREATE PROCEDURE `CreateInsertLocation` (tableName VARCHAR(255),ts BIGINT(20),systs INT(20),lat FLOAT,lon FLOAT)
BEGIN
DECLARE FoundCount INT;
SELECT COUNT(1) INTO FoundCount
FROM information_schema.tables
WHERE table_schema = 'DB'
AND table_name = tableName;
IF FoundCount = 1 THEN SET #sql = CONCAT('INSERT INTO ',tableName,'
(timestamp, lattitude, longitude,systime)
VALUES
(','ts','lat','lon','systs',')'); /* Error in this line */
PREPARE stmt FROM #sql;
EXECUTE stmt;
ELSE
SET #sql = CONCAT('CREATE TABLE `',tableName,'`(
`id` bigint(20) NOT NULL DEFAULT '0',
`timestamp` bigint(20) NOT NULL DEFAULT '0',
`lattitude` float NOT NULL DEFAULT '0',
`longitude` float NOT NULL DEFAULT '0',
`systime` bigint(20) DEFAULT NULL,
KEY `LocIdx` (`vtuId`,`timestamp`),
KEY `SysIdx` (`vtuId`,`systime`),
PRIMARY KEY (id)');
PREPARE stmt FROM #sql;
EXECUTE stmt;
SET #sql = CONCAT('INSERT INTO ',tableName,'
(timestamp, lattitude, longitude,systime)
VALUES
(','ts','lat','lon','systs')');
PREPARE stmt FROM #sql2;
EXECUTE stmt;
END
$$
DELIMITER ;

Related

How to create an Archive Table with a Mysql Procedure and Date()

I try to create a procedure which runs once a day and stores a subset of data from a bigger Table. The Name of this table should be dynamically created with CURDATE().
DROP PROCEDURE daily_backup;
DELIMITER |
CREATE PROCEDURE daily_backup()
BEGIN
SET #tbl = CONCAT('items_data_', DATE_FORMAT(CURDATE(), '%Y%m%d'));
SET #s = CONCAT('DROP TABLE IF EXISTS ', #tbl);
PREPARE stmt FROM #s;
EXECUTE stmt;
#s = CONCAT('CREATE TABLE `', #tb1, '` (`id` int(11) NOT NULL,`up` mediumint(9) NOT NULL,`down` mediumint(9) NOT NULL) ENGINE=Archive');
PREPARE stmt FROM #s;
EXECUTE stmt;
SET #s = CONCAT('INSERT INTO ', #tbl, ' SELECT id,up,down FROM items;');
PREPARE stmt FROM #s;
EXECUTE stmt;
END
These are the results
#1064 - You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '#s = CONCAT('CREATE TABLE `', #tb1, '` (`id` int(11) NOT NULL,`up` mediumint(9) ' at line 8
EDIT // Working now
DROP PROCEDURE daily_backup;
DELIMITER |
CREATE PROCEDURE daily_backup()
BEGIN
SET #tbl = CONCAT('items_data_', DATE_FORMAT(CURDATE(), '%Y%m%d'));
SET #s = CONCAT('DROP TABLE IF EXISTS ', #tbl);
PREPARE stmt FROM #s;
EXECUTE stmt;
SET #s = CONCAT('CREATE TABLE `', #tbl, '` (`id` int(11) NOT NULL,`up` mediumint(9) NOT NULL,`down` mediumint(9) NOT NULL) ENGINE=Archive');
PREPARE stmt FROM #s;
EXECUTE stmt;
SET #s = CONCAT('INSERT INTO ', #tbl, ' SELECT id,up,down FROM items;');
PREPARE stmt FROM #s;
EXECUTE stmt;
DEALLOCATE PREPARE stmt;
END
Variable assignments always need a SET command:
SET #s = CONCAT('CREATE TABLE `', #tbl, '` (`id` int(11) NOT NULL,`up` mediumint(9) NOT NULL,`down` mediumint(9) NOT NULL) ENGINE=Archive');
Note that you have #tb1 in this statement, it should be #tbl.
It's also good practice to DEALLOCATE a prepared statement after you use it i.e.
DEALLOCATE PREPARE stmt;
after the EXECUTE stmt

the count of row( user) for each table begining with cm in the schema

I want all the tables' list with corresponding count of number of NULL entries in column 'user'. I have printed all table's name beginning with cm using
SELECT
TABLE_NAME
FROM
INFORMATION_SCHEMA.TABLES WHERE (TABLE_NAME LIKE 'cm%') ;
but for each table i want to run
SELECT COUNT(1) FROM <TABLENAME> WHERE `create_user` IS NULL
OR `create_time` IS NULL
and print
Create the table below, replacing it with the correct database name:
CREATE TABLE `db`.`tbl_count_null` (
`tableschema` varchar(64) NOT NULL DEFAULT '',
`tablename` varchar(64) NOT NULL DEFAULT '',
`qtd` char(0) NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
See if the procedure below meets you:
CREATE DEFINER=`user`#`%` PROCEDURE `nameprocedure`()
BEGIN
DECLARE x, y LONGTEXT;
DECLARE done INT DEFAULT 0;
DECLARE databasesCursor CURSOR FOR
SELECT TABLE_SCHEMA, TABLE_NAME FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_NAME LIKE 'cm%';
DECLARE CONTINUE HANDLER
FOR SQLSTATE '02000' SET done = 1;
OPEN databasesCursor;
myLoop: LOOP
FETCH databasesCursor INTO x, y;
IF NOT done THEN
SET #query = CONCAT("INSERT INTO`db`.`tbl_count_null` (tableschema, tablename, qtd) SELECT '",x,"' AS `schema`,'",y,"' AS `table`,COUNT(1) FROM `",x,"`.`",y,"` WHERE `create_user` IS NULL OR `create_time` IS NULL;");
PREPARE stmt FROM #query;
EXECUTE stmt;
DEALLOCATE PREPARE stmt;
END IF;
END LOOP myLoop;
CLOSE databasesCursor;
END;
After creating the above procedure, call with the command:
call db.nameprocedure;
Verify that the records have been inserted into the table db.tbl_count_null

Stored procedure for copying data

I am all new to creating procedures and triggers in MySQL, but have struggled with this over the last couple of days, and it simply will not work. The error messages, that I get from Mysql does not help me any longer.
So I am trying to create a procedure, which I need to run after an update. I will take any updates and store new data in a dynamic created table in another database.
Here it is:
CREATE PROCEDURE get_price_storage_table (IN market_id INT(11), OUT tablename VARCHAR(50))
BEGIN
SET #NUMBER = CEILING(market_id / 100000) * 100000;
SET tablename = CONCAT('price_',#NUMBER);
SET #SQL = CONCAT('CREATE TABLE IF NOT EXISTS data.',tablename,'(
`pk_id` INT(11) UNSIGNED NOT NULL AUTO_INCREMENT,
`fk_market_id` INT(11) UNSIGNED NOT NULL,
`fk_outcome_type_id` MEDIUMINT(8) UNSIGNED NOT NULL,
`price` DOUBLE NOT NULL,
`status` ENUM(\'enabled\',\'disabled\') NOT NULL,
`created` DATETIME NOT NULL,
PRIMARY KEY (`pk_id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8')');
PREPARE stmt FROM #SQL;
EXECUTE stmt;
DEALLOCATE PREPARE stmt;
END;
This need to be triggered after update, where the SQL goes like:
CREATE TRIGGER copy_price_data AFTER UPDATE ON price
FOR EACH ROW
BEGIN
IF NEW.updated <> OLD.updated THEN
SET #market_id = NEW.fk_market_id;
SET #tablename = NULL;
CALL create_price_storage_table(#market_id, #tablename);
SELECT #tablename;
SET #SQL = CONCAT(
'INSERT INTO ',
#tablename,
' (`fk_market_id`, `fk_outcome_type_id`, `price`, `status`, `created`) VALUES (NEW.fk_market_id, NEW.fk_outcome_type_id, NEW.price, NEW.status, NOW())');
PREPARE stmt FROM #SQL;
EXECUTE stmt;
DEALLOCATE PREPARE stmt;
END IF;
END;
When trying to create the procedure, then I get the following error message from MySQL:
CREATE PROCEDURE get_price_storage_table (IN market_id INT(11), OUT tablename VARCHAR(50))
BEGIN
SET #NUMBER = CEILING(market_id / 100000) * 100000;
MySQL returnerede: Dokumentation 1064 - You have an error in your SQL
syntax; check the manual that corresponds to your MySQL server version
for the right syntax to use near '' at line 3
I hope someone with greater understanding than me, can point me in the right direction. Thanks.
Change:
CREATE PROCEDURE get_price_storage_table (IN market_id INT(11), OUT tablename VARCHAR(50))
BEGIN
SET #NUMBER = CEILING(market_id / 100000) * 100000;
SET tablename = CONCAT('price_',#NUMBER);
SET #SQL = CONCAT('CREATE TABLE IF NOT EXISTS data.',tablename,'(
`pk_id` INT(11) UNSIGNED NOT NULL AUTO_INCREMENT,
`fk_market_id` INT(11) UNSIGNED NOT NULL,
`fk_outcome_type_id` MEDIUMINT(8) UNSIGNED NOT NULL,
`price` DOUBLE NOT NULL,
`status` ENUM(\'enabled\',\'disabled\') NOT NULL,
`created` DATETIME NOT NULL,
PRIMARY KEY (`pk_id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8')');
PREPARE stmt FROM #SQL;
EXECUTE stmt;
DEALLOCATE PREPARE stmt;
END;
By:
CREATE PROCEDURE `get_price_storage_table`(`market_id` INT UNSIGNED, OUT `tablename` VARCHAR(50))
BEGIN
DECLARE `NUMBER` INT UNSIGNED;
SET `NUMBER` := CEILING(`market_id` / 100000) * 100000;
SET `tablename` := CONCAT('`price_', NUMBER, '`');
SET #`SQL` := CONCAT('CREATE TABLE IF NOT EXISTS ', tablename, '(
`pk_id` INT(11) UNSIGNED NOT NULL AUTO_INCREMENT,
`fk_market_id` INT(11) UNSIGNED NOT NULL,
`fk_outcome_type_id` MEDIUMINT(8) UNSIGNED NOT NULL,
`price` DOUBLE NOT NULL,
`status` ENUM(\'enabled\',\'disabled\') NOT NULL,
`created` DATETIME NOT NULL,
PRIMARY KEY (`pk_id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8');
-- ) ENGINE=InnoDB DEFAULT CHARSET=utf8')');
PREPARE `stmt` FROM #`SQL`;
EXECUTE `stmt`;
DEALLOCATE PREPARE `stmt`;
END//
Example:
mysql> DROP PROCEDURE IF EXISTS `get_price_storage_table`;
Query OK, 0 rows affected, 1 warning (0.00 sec)
mysql> DELIMITER //
mysql> CREATE PROCEDURE `get_price_storage_table`(`market_id` INT UNSIGNED, OUT `tablename` VARCHAR(50))
-> BEGIN
-> DECLARE `NUMBER` INT UNSIGNED;
-> SET `NUMBER` := CEILING(`market_id` / 100000) * 100000;
-> SET `tablename` := CONCAT('`price_', NUMBER, '`');
-> SET #`SQL` := CONCAT('CREATE TABLE IF NOT EXISTS ', tablename, '(
> `pk_id` INT(11) UNSIGNED NOT NULL AUTO_INCREMENT,
> `fk_market_id` INT(11) UNSIGNED NOT NULL,
> `fk_outcome_type_id` MEDIUMINT(8) UNSIGNED NOT NULL,
> `price` DOUBLE NOT NULL,
> `status` ENUM(\'enabled\',\'disabled\') NOT NULL,
> `created` DATETIME NOT NULL,
> PRIMARY KEY (`pk_id`)
> ) ENGINE=InnoDB DEFAULT CHARSET=utf8');
-> PREPARE `stmt` FROM #`SQL`;
-> EXECUTE `stmt`;
-> DEALLOCATE PREPARE `stmt`;
-> END//
Query OK, 0 rows affected (0.00 sec)
mysql> DELIMITER ;
mysql> CALL `get_price_storage_table`(1, #`tablename`);
Query OK, 0 rows affected (0.00 sec)
mysql> SELECT #`tablename`;
+----------------+
| #`tablename` |
+----------------+
| `price_100000` |
+----------------+
1 row in set (0.00 sec)
mysql> SHOW TABLES;
+----------------+
| Tables_in_test |
+----------------+
| price_100000 |
+----------------+
1 row in set (0.00 sec)
Syntax errors are to be located to the left (or above) of the place reported in the error message, since the reported place is the first thing that confuses MySQL.
In your case this is the ;
It finishes your CREATE PROCEDURE statement, but MySQL expects more to come, like an END.
Procedures / Triggers / Functions have to be declared with a different delimiter when they consist of more than one statement.
Try like this:
DELIMITER $$
CREATE PROCEDURE whatever()
BEGIN
SELECT 'whatever';
SELECT 'another';
END $$
DELIMITER ;

syntax error in mysql procedure error code 1064

I am trying to write one procedure, i am getting syntax error. I was trying to fix the same with the help of net, but failed.
Here is my stored procedure. Any help please?
The scenario is i am trying to take workspaceid column values from table hotelings and trying to make that value as my column for another table. Then i am trying to update the same column value with ; for a given start and end time of hoteling table in newly created table2.
My two tables are
CREATE TABLE `hotelings` (
`HotelingId` int(11) NOT NULL AUTO_INCREMENT,
`Description` longtext,
`StartDate` datetime NOT NULL,
`EndDate` datetime NOT NULL,
`BookedBy` longtext,
`BookingType` int(11) NOT NULL,
`RepeatType` longtext,
`RepeatDay` longtext,
`ProjectId` int(11) NOT NULL,
`WorkSpaceId` int(11) NOT NULL,
`starttime` varchar(45) DEFAULT NULL,
`endtime` varchar(45) DEFAULT NULL,
PRIMARY KEY (`HotelingId`)
) ENGINE=InnoDB AUTO_INCREMENT=3 DEFAULT CHARSET=utf8;
CREATE TABLE `hotelingtime` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`Time` time DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=4 DEFAULT CHARSET=utf8;
and my procedure is:
DELIMITER $$
CREATE DEFINER=`root`#`localhost` PROCEDURE `new_procedure`()
BEGIN
SET #sql = NULL;
SELECT
GROUP_CONCAT(DISTINCT
CONCAT(
'MAX(CASE WHEN workspaceid = ''',
workspaceid,
''' then "" ELSE NULL end) AS ',
CONCAT('`',workspaceid,'`')
)
) INTO #sql
FROM sms.hotelings;
SET #sql = CONCAT('CREATE TABLE IF NOT EXISTS table2 AS SELECT t.Time as Time, ', #sql, ' FROM sms.hotelings h, sms.hotelingtime t
GROUP BY t.Time');
PREPARE stmt FROM #sql;
EXECUTE stmt;
DEALLOCATE PREPARE stmt;
begin
declare num_rows int;
declare i int;
declare col_name varchar(50);
declare v varchar(10);
DECLARE v_finished INTEGER DEFAULT 0;
-- cursor to fetch column names
DECLARE col_names CURSOR FOR
SELECT column_name
FROM INFORMATION_SCHEMA.COLUMNS
WHERE table_name = 'table2'
ORDER BY ordinal_position;
-- declare NOT FOUND handler
DECLARE CONTINUE HANDLER
FOR NOT FOUND SET v_finished = 1;
OPEN col_names;
temp_hotelingloop: LOOP
FETCH col_names INTO col_name;
IF v_finished = 1 THEN
LEAVE temp_hotelingloop;
END IF;
begin
declare starttime time;
declare endtime time;
-- cursor to fetch start and end for a given workspaceid
DECLARE startendTime CURSOR FOR
SELECT starttime, endtime from hotelings
where workspaceid = col_name;
OPEN startendTime;
FETCH startendTime INTO starttime, endtime;
-- i am getting error here and not giving me the result.
SET #sql = CONCAT('update table2 set ''',#col_name ,''' = '';'' where time between ''',#starttime,''' and ''',#endtime,'''');
PREPARE stmt FROM #sql;
EXECUTE stmt;
DEALLOCATE PREPARE stmt;
CLOSE startendTime;
end;
SET i = i + 1;
END LOOP temp_hotelingloop;
CLOSE col_names;
end;
select * from table2;
DROP TABLE table2;
END
Any help please?
Thanks in advance
SET #sql = CONCAT('update table2 set ''',#col_name ,''' = '';'' where time between ''',#starttime,''' and ''',#endtime,'''');
You are using #col_name here, but on your declaration missing the #
declare col_name varchar(50);

How to insert data from an EXECUTE statement in mySql?

I have data in a wp_users table, and I want to duplicate the data from that table (except for the ID column) into another table, called wp_users2.
If I didn't care about the id column, which I want to auto-increment, I could just do this:
insert into wp_users2 (select *, NULL as ID from wp_users)
So I know I could do this by typing out all of the column headers except for ID and manually selecting that one as NULL,
SELECT NULL as id, col2, col3...
but I'd like to do it dynamically. I read this great S.O. post about how to do that, and it works, however I can't figure out how to take the data it gives me and put it into an insert statement.
INSERT INTO wp_users2 (
SET #sql = CONCAT('SELECT NULL as ID,',
(SELECT REPLACE(GROUP_CONCAT(COLUMN_NAME), 'ID,', '')
FROM INFORMATION_SCHEMA.COLUMNS
WHERE TABLE_NAME = 'wp_users'
AND TABLE_SCHEMA = 'wp1'),
' FROM wp_users');
PREPARE stmt1 FROM #sql;
EXECUTE stmt1;
)
What's the right syntax for that?
As I understand - id is AUTO_INCREMENT field.
So, try to use this script as an example for your task -
CREATE TABLE table1(
id INT(11) NOT NULL AUTO_INCREMENT,
column1 VARCHAR(255) DEFAULT NULL,
column2 VARCHAR(255) DEFAULT NULL,
PRIMARY KEY (id)
);
CREATE TABLE table2(
id INT(11) NOT NULL AUTO_INCREMENT,
column1 VARCHAR(255) DEFAULT NULL,
column2 VARCHAR(255) DEFAULT NULL,
PRIMARY KEY (id)
);
INSERT INTO table1 VALUES
(1, 'c1', 'c2'),
(2, 'c3', 'c4');
SET #source_table = 'table1';
SET #target_table = 'table2';
SET #id = 'id';
SET #columns = NULL;
SELECT group_concat(column_name) INTO #columns FROM information_schema.columns
WHERE
table_schema = 'database_name' -- Set your database name here
AND table_name = #source_table
AND column_name != #id;
SET #insert = concat('INSERT INTO ', #target_table, '(', #id, ',', #columns, ') SELECT NULL, ', #columns, ' FROM ', #source_table);
PREPARE stmt1 FROM #insert;
EXECUTE stmt1;
DEALLOCATE PREPARE stmt1;
set #sql = (select concat('insert into wp_users2 SELECT NULL,',
group_concat(column_name),' from ',table_name) from information_schema.columns
where table_name = 'wp_users' and table_schema = 'wp1' and column_name != 'id'
order by ordinal_position);
prepare stmt1 from #sql;
execute stmt1;
deallocate prepare stmt1;