1

I'm trying to get rid of a boring task of clicking on the create button of my GUI for creating new rows so I've decided to define a function to do that as many times as i want it to... here's the code, but it returns an error in NAVICAT:

CREATE FUNCTION AddRow (number INT)
    BEGIN
        SET @x = 0;
    REPEAT
        INSERT INTO 'result' () VALUES ()
        SET @x += 1;
    UNTIL x > @number END REPEAT;

    END
call AddRow (10)

Error:

[SQL]CREATE FUNCTION AddRow (number INT)
    BEGIN
        SET @x = 0;
    REPEAT
        INSERT INTO 'result' () VALUES ()
        SET @x += 1;
    UNTIL x > @number END REPEAT;

    END
call AddRow (10)

[Err] 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 'BEGIN
        SET @x = 0;
    REPEAT
        INSERT INTO 'result' () VALUES ()
        SET @x += ' at line 2
marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
  • 3
    You should try to avoid procedural (loop/goto) code in a relational database. – StingyJack Sep 03 '13 at 16:34
  • Also, what you want is a "numbers table". There are many examples on the web - http://stackoverflow.com/questions/9751318/creating-a-numbers-table-in-mysql – StingyJack Sep 03 '13 at 16:36
  • @StingyJack: Well i'm not trying to write anything for production use. only as a query to be executed when i need it, it only applies new rows to db when i call the function. what u mentioned in the second comment is rather complex for a simple task. but if u have any other solution i would be happy to hear it. – T0M XeOn LuCiFeR Sep 04 '13 at 10:20
  • I dont think MySQL allows for anything simpler. Other RDBMS have simpler techniques, and you can always search for "numbers table MYSQL" to try and find better examples. – StingyJack Sep 04 '13 at 14:06
  • Why dont you just add a text box to the gui for "#of rows", and have the gui just call the add row that many times? – StingyJack Sep 04 '13 at 14:07

0 Answers0