Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. Database & SysAdmin
  3. Database
  4. Create multiple tables with a single SQL query

Create multiple tables with a single SQL query

Scheduled Pinned Locked Moved Database
databasejavaquestion
7 Posts 5 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • R Offline
    R Offline
    Richard Fernandes Dubai UAE
    wrote on last edited by
    #1

    Is there any way of creating say 100 different tables using a single SQL query/statement..Or is there another way around?:java:

    C L Richard DeemingR K 4 Replies Last reply
    0
    • R Richard Fernandes Dubai UAE

      Is there any way of creating say 100 different tables using a single SQL query/statement..Or is there another way around?:java:

      C Offline
      C Offline
      Chris Quinn
      wrote on last edited by
      #2

      You could use a cursor or loop, generating inline create table statements which you then execute

      ========================================================= I'm an optoholic - my glass is always half full of vodka. =========================================================

      1 Reply Last reply
      0
      • R Richard Fernandes Dubai UAE

        Is there any way of creating say 100 different tables using a single SQL query/statement..Or is there another way around?:java:

        L Offline
        L Offline
        Lost User
        wrote on last edited by
        #3

        No. A single create-statement creates a single table. You can build your own loop, but then it wouldn't be a single statement anymore. If you want it to be a single query, simply concatenate the statements. Logically, each table would have it's own layout/schema - unless all tables look the same (which would be a code-smell), different column-specifications need to be used per table.

        Bastard Programmer from Hell :suss: If you can't read my code, try converting it here[^]

        L 1 Reply Last reply
        0
        • R Richard Fernandes Dubai UAE

          Is there any way of creating say 100 different tables using a single SQL query/statement..Or is there another way around?:java:

          Richard DeemingR Offline
          Richard DeemingR Offline
          Richard Deeming
          wrote on last edited by
          #4

          From your question, I'm assuming you're trying to partition a table. Creating 100 physical tables is probably not the best approach. For MS SQL, have a look at Partitioned Tables and Indexes[^] instead.


          "These people looked deep within my soul and assigned me a number based on the order in which I joined." - Homer

          "These people looked deep within my soul and assigned me a number based on the order in which I joined" - Homer

          1 Reply Last reply
          0
          • L Lost User

            No. A single create-statement creates a single table. You can build your own loop, but then it wouldn't be a single statement anymore. If you want it to be a single query, simply concatenate the statements. Logically, each table would have it's own layout/schema - unless all tables look the same (which would be a code-smell), different column-specifications need to be used per table.

            Bastard Programmer from Hell :suss: If you can't read my code, try converting it here[^]

            L Offline
            L Offline
            Lost User
            wrote on last edited by
            #5

            Eddy Vluggen wrote:

            Logically, each table would have it's own layout/schema - unless all tables look the same (which would be a code-smell)

            It's common to see more than one table with the same schema in horizontal partitioning. Eg., CustomerEast and CustomerWest. I know this violate normalization rules, but again, not all production databases are normalized completely.

            L 1 Reply Last reply
            0
            • L Lost User

              Eddy Vluggen wrote:

              Logically, each table would have it's own layout/schema - unless all tables look the same (which would be a code-smell)

              It's common to see more than one table with the same schema in horizontal partitioning. Eg., CustomerEast and CustomerWest. I know this violate normalization rules, but again, not all production databases are normalized completely.

              L Offline
              L Offline
              Lost User
              wrote on last edited by
              #6

              Shameel wrote:

              not all production databases are normalized completely.

              That's rather an understatement. You'll also often have multiple small tables consisting of an BIGINT|UUID and a [N]VARCHAR, but even though they share their structure - you don't have a single statement.

              Bastard Programmer from Hell :suss: If you can't read my code, try converting it here[^]

              1 Reply Last reply
              0
              • R Richard Fernandes Dubai UAE

                Is there any way of creating say 100 different tables using a single SQL query/statement..Or is there another way around?:java:

                K Offline
                K Offline
                King Fisher
                wrote on last edited by
                #7

                you can do by using loop statement

                1 Reply Last reply
                0
                Reply
                • Reply as topic
                Log in to reply
                • Oldest to Newest
                • Newest to Oldest
                • Most Votes


                • Login

                • Don't have an account? Register

                • Login or register to search.
                • First post
                  Last post
                0
                • Categories
                • Recent
                • Tags
                • Popular
                • World
                • Users
                • Groups