SQL stored procedure instance--dynamically copy data from one table to another table based on table data

Source: Internet
Author: User
Tags one table

Dynamically copy data from one table to another table based on table data

Copy the track table records to the corresponding track_ according to the mac_id two digits. In the table

such as: mac_id=12345678910, the latter two-bit 10 corresponds to the table is track_10, the record is copied to track_10

To create a sub_track stored procedure implementation:

 --Create a stored procedure named Sub_track
CREATE PROCEDURESub_track ()
begin DeclareIint;--defining the loop variable i Set @imei =0;--define IMEI last two bits Set @t_count=0;--define how many record variables the table has SETI=0; SELECT COUNT(*) into @t_count fromTrack--get the number of bars in a table whileI<= @t_countDo--Loops SELECT Right(MAC_ID,2) into @imei fromTrackORDER byGpstimeDESCLIMIT I,1;--get IMEI last two bit Set @imei =CONVERT(@imei, signed);--Convert to Digital --SQL Statements Set @sql1="(Mac_id,mac_type,channel,type,x,y,gpstime,bvalid,speed,dir,s1,s2,s3,s4,ins_date,imagefi Le,battery)SELECTMac_id,mac_type,channel,type,x,y,gpstime,bvalid,speed,dir,s1,s2,s3,s4,ins_date,imagefil E,battery from TrackORDER byGpstimeDESCLIMIT "; Set @inset_sql=CONCAT ("INSERT intoTrack_ ",@imei,@sql1, I, ",",1);--stitching a complete INSERT SQL statement PREPAREInsert_track from @inset_sql;--Pre-compilation EXECUTEInsert_track;--Execute SQL statement SetI=I+1;--End LoopEnd while; Commit;End



--Execute Stored procedure

call Sub_track ();

@flm

SQL stored procedure instance--dynamically copy data from one table to another table based on table data

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.