简体   繁体   中英

MySQL Stored Procedure [Copy table 1 -> table 2]

My knowledge of MySQL/ SQL apart from the simple CRUD operations is basic.

If I had to use a stored procedure to move certain attributes (not in a specific order) to another table how could it be done?

These are the following tables. I want to move from the 1st to the 2nd table.

As you can see the datatype sizes are different for certain columns.

CREATE TABLE IF NOT EXISTS `source_cdr` (
  `callstart` datetime NOT NULL DEFAULT '0000-00-00 00:00:00',
  `src` varchar(80) NOT NULL DEFAULT '',
  `dst` varchar(80) NOT NULL DEFAULT '',
  `accountcode` varchar(50) NOT NULL,
  `uniqueid` varchar(100) NOT NULL,
  `ID` int(11) NOT NULL AUTO_INCREMENT,
  `callanswer` datetime NOT NULL DEFAULT '0000-00-00 00:00:00',
  `callend` datetime NOT NULL DEFAULT '0000-00-00 00:00:00',
  `disposition` varchar(50) NOT NULL,
  `cdr_id` int(11) unsigned NOT NULL DEFAULT '0',
  `pin_code` varchar(4) NOT NULL,
  `provider` int(11) NOT NULL,
  PRIMARY KEY (`ID`),
  KEY `calldate_idx` (`callstart`) USING BTREE,
  KEY `idx_acc_code_calldate` (`accountcode`,`callstart`) USING BTREE,
  KEY `uniqueid` (`uniqueid`),
  KEY `cdr_id` (`cdr_id`),
  KEY `idx_uniqueid_cdr_id` (`uniqueid`,`cdr_id`)
) ENGINE=MyISAM;

--

CREATE TABLE IF NOT EXISTS `destination_cdr` (
  `id` bigint(20) NOT NULL AUTO_INCREMENT,
  `calldate` datetime NOT NULL,
  `source` varchar(80) NOT NULL,
  `destination` varchar(80) NOT NULL,
  `account_code` varchar(30) DEFAULT NULL,
  `pincode` varchar(45) NOT NULL,
  `duration_call` bigint(20) NOT NULL DEFAULT '0',
  `duration_talk` bigint(20) NOT NULL,
  `disposition` varchar(255) NOT NULL,
  `clid` varchar(80) DEFAULT NULL,
  `cdr_id` bigint(20) DEFAULT NULL,
  `vxcdr_id` bigint(20) DEFAULT NULL,
  `provider` int(11) NOT NULL DEFAULT '0'
  PRIMARY KEY (`id`)
) ENGINE=MyISAM;

EDIT 1

An example of a row

    ('2012-03-18 20:54:49', '5796', '0761100866', '103f0124ad510516f33cab132c0a695b', 'call-F1884808-6753-2F10-181C-3A@10.217.164.33', 308006367, '2012-03-18 20:55:05', '2012-03-18 20:55:51', '200 OK', 2, '', 0),

Thanks

You can use MySQL: INSERT ... SELECT Syntax to copy data from one table to the other.
Decide common fields in both and copy the same.

Example :

INSERT INTO TABLE2( COL1, COLx, ... ) SELECT colM, colY FROM TABLE1;

If the column sizes mismatch, data truncation takes place, and you can't overcome that but redefine the destination table columns.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM