Populating a table using an sql script

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • -

    #1

    Populating a table using an sql script

    Hi all,

    I want to create an sql script and one of the table that will be created
    is a 'country' table.

    The question is how do I populate the country tables?
    Should there be individual INSERT statements for each country or is it
    possible to use a loop somehow?

    Thank you in advance.
  • Aggro

    #2
    Re: Populating a table using an sql script

    - wrote:
    [color=blue]
    > The question is how do I populate the country tables?
    > Should there be individual INSERT statements for each country or is it
    > possible to use a loop somehow?[/color]

    insert into country(id,name )
    values(1,'Finla nd'),(2,'Canada '),(3,'Sweden') ;

    Note that there might be some max query length setting. This is normally
    quite large, but if you have a lot of data, you might need to break
    single insert into smaller ones with all having 500 rows to insert or
    something like that.

    Comment

    • -

      #3
      Re: Populating a table using an sql script

      Aggro wrote:[color=blue]
      > - wrote:
      >[color=green]
      >> The question is how do I populate the country tables?
      >> Should there be individual INSERT statements for each country or is it
      >> possible to use a loop somehow?[/color]
      >
      >
      > insert into country(id,name )
      > values(1,'Finla nd'),(2,'Canada '),(3,'Sweden') ;
      >
      > Note that there might be some max query length setting. This is normally
      > quite large, but if you have a lot of data, you might need to break
      > single insert into smaller ones with all having 500 rows to insert or
      > something like that.[/color]

      Hi Aggro,

      I'm aware of the syntax which is:

      INSERT INTO country(code, name) VALUES (...);
      INSERT INTO country(code, name) VALUES (...);
      INSERT INTO country(code, name) VALUES (...);
      so on and so forth..

      However, I would like to know whether it is possible to use a loop
      inside an .sql file so that there is only one INSERT statement e.g.

      INSERT INTO country(code, name) VALUES (_variable_, _variable1_);

      I am creating an .sql script file to create the country table and
      populating the values.

      Is it possible for an .sql file to do that?



      Comment

      • Aggro

        #4
        Re: Populating a table using an sql script

        - wrote:
        [color=blue]
        > INSERT INTO country(code, name) VALUES (_variable_, _variable1_);
        >
        > I am creating an .sql script file to create the country table and
        > populating the values.
        >
        > Is it possible for an .sql file to do that?[/color]

        I don't know if such exist, but I think not. For that I usually use PHP
        or some other programming language to create content for sql file for me.

        Here is an example done with Javascript (it's pretty bad code propably,
        since I'm not very familiar with Javascript, but it worked on my
        computer). Just save it in .html file and run the file in a browser
        where javascript is enabled and it should print some inserts on the screen.

        <script type="text/javascript">
        for( var i = 0; i < 10; i++ )
        {
        document.write( "insert into country values("+i+");< br>\n" );
        }
        </script>

        Comment

        • Bill Karwin

          #5
          Re: Populating a table using an sql script

          - wrote:[color=blue]
          > However, I would like to know whether it is possible to use a loop
          > inside an .sql file so that there is only one INSERT statement e.g.[/color]

          I usually use one INSERT statement with multiple sets of row constructors:

          INSERT INTO country (code, name) VALUES
          ('US', 'United States'),
          ('SE', 'Sweden'),
          ('NO', 'Norway'),
          ...


          Regards,
          Bill K.

          Comment

          • Rich R

            #6
            Re: Populating a table using an sql script


            "-" <nobody@hoem.om > wrote in message news:41e8e162$1 @news.starhub.n et.sg...[color=blue]
            > Hi all,
            >
            > I want to create an sql script and one of the table that will be created
            > is a 'country' table.
            >
            > The question is how do I populate the country tables?
            > Should there be individual INSERT statements for each country or is it
            > possible to use a loop somehow?
            >
            > Thank you in advance.[/color]

            For "bulk" inserts I use LOAD DATA INFILE... Very efficient, and very
            simple. No loop.

            Regards,
            Rich R.


            Comment

            Working...