Solved

MySQL Trigger INSERT number of new records based on column value

Posted on 2014-03-10
2
1,140 Views
Last Modified: 2014-05-14
This question is a revised version of another currently open question: OPEN QUESTION.

I've distilled the essence of my question into a much simpler form:

I need to write a MySQL trigger event that will INSERT a number of records into `table2` based on the value of a field (quantity) of `table1`.

Thus, if during the initial INSERT (into table1) the column `table1.quantity` = 7; My trigger should INSERT 7 new records into table2.


\\
\\
\\
\\

This is ridiculously simple to execute in PHP... I'm simply running into a wall setting up the counter syntax for MySQL. Many thanks!

Roman A/K/A Rapturer
0
Comment
Question by:Rapturer
2 Comments
 
LVL 26

Expert Comment

by:Zberteoc
ID: 39920475
Here is a tutotial for MySQL trigger with PHP code examples:

http://code.tutsplus.com/articles/introduction-to-mysql-triggers--net-12226
0
 
LVL 34

Accepted Solution

by:
gr8gonzo earned 500 total points
ID: 39920496
Not tested but it's probably something like this:

DELIMITER $$
CREATE TRIGGER foo BEFORE INSTERT ON table1
   FOR EACH ROW BEGIN
     SET @x = 0;
     insertLoop: LOOP
       SET @x = @x + 1;
       
       INSERT INTO table2...
       
       IF @x < NEW.quantity THEN
         ITERATE insertLoop;
       END IF;
       LEAVE insertLoop;
     END LOOP insertLoop;
   END;
$$

Open in new window

0

Featured Post

Complete VMware vSphere® ESX(i) & Hyper-V Backup

Capture your entire system, including the host, with patented disk imaging integrated with VMware VADP / Microsoft VSS and RCT. RTOs is as low as 15 seconds with Acronis Active Restore™. You can enjoy unlimited P2V/V2V migrations from any source (even from a different hypervisor)

Join & Write a Comment

More Fun with XML and MySQL – Parsing Delimited String with a Single SQL Statement Are you ready for another of my SQL tidbits?  Hopefully so, as in this adventure, I will be covering a topic that comes up a lot which is parsing a comma (or other…
Does the idea of dealing with bits scare or confuse you? Does it seem like a waste of time in an age where we all have terabytes of storage? If so, you're missing out on one of the core tools in every professional programmer's toolbox. Learn how to …
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…
You have products, that come in variants and want to set different prices for them? Watch this micro tutorial that describes how to configure prices for Magento super attributes. Assigning simple products to configurable: We assigned simple products…

757 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

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now