How can I insert 100 rows into a table that has only one identity column

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • jasmine23
    New Member
    • Mar 2013
    • 4

    How can I insert 100 rows into a table that has only one identity column

    I am trying to insert 100 rows in a single queries into the table.
    my table is CREATE User (ID INT IDENTITY(1,1))

    Thanks in advance.
  • r035198x
    MVP
    • Sep 2006
    • 13225

    #2
    Where are the rows coming from?

    Comment

    • jasmine23
      New Member
      • Mar 2013
      • 4

      #3
      I just wanna insert the rows. for example, I want the 100 ID rows using identity column..
      how can i inserts multiple rows if i don't use repeating INSERT INTO User DEFAULT VALUES
      i am testing Identity column..
      Thanks.

      Comment

      • r035198x
        MVP
        • Sep 2006
        • 13225

        #4
        It really depends on where you want the data to come from but you can use an insert into ... select statement like
        Code:
        INSERT INTO YourTable  (col1, col12, ..., coln)
               SELECT val1, val2  FROM SomeSource;

        Comment

        • jasmine23
          New Member
          • Mar 2013
          • 4

          #5
          Thanks. r035198x
          I see. But my data is not from other source. The identity column can generate key values. what I want to know is how can i insert to get 100 rows generating.

          insert into User Default Values
          insert into User Default Values

          when I repeat the query two times and retrieve again, the result shows
          ID
          1
          2
          Like that i want to get 100 rows without inserting again and again.
          Thanks.

          Comment

          • r035198x
            MVP
            • Sep 2006
            • 13225

            #6
            Does your table only contain one column? you should let the database generate the ids so that you don't need to insert them and just insert the other values. That way you don't need to manually increment.

            If you insist on manually setting it then you need something like
            Code:
            DECLARE @CN INT
            WHILE (@CN<=100)
            BEGIN
            INSERT INTO yourTable (columnName) SELECT @CN
            SET @CN = @CN + 1
            END

            Comment

            • jasmine23
              New Member
              • Mar 2013
              • 4

              #7
              Thanks r035198x :)
              I got it with your help. I used like
              Code:
              DECLARE @CN INT=1
              WHILE (@CN<=100)
              BEGIN
              INSERT INTO User DEFAULT VALUES // I need to use IDENTITY column that's why 
              set @CN +=1
              END
              Last edited by Rabbit; Mar 13 '13, 03:51 PM. Reason: Please use code tags when posting code.

              Comment

              • r035198x
                MVP
                • Sep 2006
                • 13225

                #8
                Ok, cool. Well done!

                Comment

                Working...