?
Solved

MySQL procedure or function

Posted on 2009-02-10
6
Medium Priority
?
260 Views
Last Modified: 2012-05-06
Hi,

I would like to copy the records from table 'A' to table 'B'.
Table A:
timestamp | value | varname | file_h | file_o
Table B:
timestamp | blockid | value | varname | exported | file_h

Notice there are field to compute: blockid and exported

to compute field exported:
if the file_h is NULL, set exported to -1, else set it to 1

to compute the blockid here is some code:

DECLARE mytimestamp DATETIME;
SET mytimestamp = ADDTIME(    <<the timestamp from the table>>  , '0 00:15:00');
IF m = 0 THEN
blockid = CONCAT(SUBSTRING(mytimestamp,1,4),
SUBSTRING(mytimestamp,6,2), SUBSTRING(mytimestamp,9,2), SUBSTRING(mytimestamp,12,2), '00')
ELSE
blockid = CONCAT(SUBSTRING(mytimestamp,1,4),
SUBSTRING(mytimestamp,6,2), SUBSTRING(mytimestamp,9,2), SUBSTRING(mytimestamp,12,2), m)
END IF;

1. What do I need ? procedure, function, ... ?
2. Can you compelte some code.

Thank you for any help.
0
Comment
Question by:jamesbond007_
  • 4
5 Comments
 
LVL 26

Expert Comment

by:Tomas Helgi Johannsson
ID: 23599955
   Hi!

Easiest would be to create an function with an timestamp as an input variable
and varchar as an output variable and

http://dev.mysql.com/doc/refman/5.1/en/create-procedure.html



Regards,
   Tomas Helgi
CREATE FUNCTION THEBLOCKID (ts TIMESTAMP) RETURNS VARCHAR(10) 
BEGIN
DECLARE mytimestamp DATETIME;
DECLARE blockid = VARCHAR(10);
SET mytimestamp = ADDTIME(    <<the timestamp from the table>>  , '0 00:15:00');
IF m = 0 THEN
blockid = CONCAT(SUBSTRING(mytimestamp,1,4),
SUBSTRING(mytimestamp,6,2), SUBSTRING(mytimestamp,9,2), SUBSTRING(mytimestamp,12,2), '00')
ELSE
blockid = CONCAT(SUBSTRING(mytimestamp,1,4),
SUBSTRING(mytimestamp,6,2), SUBSTRING(mytimestamp,9,2), SUBSTRING(mytimestamp,12,2), m)
END IF;
return BLOCKID;
END;
    

Open in new window

0
 
LVL 26

Assisted Solution

by:Tomas Helgi Johannsson
Tomas Helgi Johannsson earned 1600 total points
ID: 23599971
Sorry small error in code.

Regards,
   Tomas Helgi
CREATE FUNCTION THEBLOCKID (ts TIMESTAMP) RETURNS VARCHAR(10) 
BEGIN
DECLARE mytimestamp DATETIME;
DECLARE blockid = VARCHAR(10);
SET mytimestamp = ADDTIME(    ts  , '0 00:15:00');
IF m = 0 THEN
blockid = CONCAT(SUBSTRING(mytimestamp,1,4),
SUBSTRING(mytimestamp,6,2), SUBSTRING(mytimestamp,9,2), SUBSTRING(mytimestamp,12,2), '00')
ELSE
blockid = CONCAT(SUBSTRING(mytimestamp,1,4),
SUBSTRING(mytimestamp,6,2), SUBSTRING(mytimestamp,9,2), SUBSTRING(mytimestamp,12,2), m)
END IF;
return BLOCKID;
END;

Open in new window

0
 

Author Comment

by:jamesbond007_
ID: 23600071
Hi,

I do not need the blockid, I need a to copy records from a table to another.
0
 
LVL 26

Accepted Solution

by:
Tomas Helgi Johannsson earned 1600 total points
ID: 23600250
Then simply use

insert into table a(column,...) select (columns,...) from table b

and you could call the function in the select statement.

Regards,
    Tomas Helgi
0
 
LVL 26

Expert Comment

by:Tomas Helgi Johannsson
ID: 23600259
sorry
insert into table b(column,...) select (columns,...) from table a  :)

Regards,
   Tomas Helgi
0

Featured Post

Hire Technology Freelancers with Gigs

Work with freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely, and get projects done right.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

It is possible to export the data of a SQL Table in SSMS and generate INSERT statements. It's neatly tucked away in the generate scripts option of a database.
MSSQL DB-maintenance also needs implementation of multiple activities. However, unprecedented errors can hamper the database management. In that case, deploying Stellar SQL Database Toolkit ensures fast and accurate database and backup repair as wel…
In this video, Percona Solutions Engineer Barrett Chambers discusses some of the basic syntax differences between MySQL and MongoDB. To learn more check out our webinar on MongoDB administration for MySQL DBA: https://www.percona.com/resources/we…
With just a little bit of  SQL and VBA, many doors open to cool things like synchronize a list box to display data relevant to other information on a form.  If you have never written code or looked at an SQL statement before, no problem! ...  give i…
Suggested Courses

839 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question