locked
INSERT statement many times RRS feed

  • Question

  • I have create a database on Azure with a table. I want to insert the same data many times so I get multiple rows that are the same. I did some research and found this should work.

    INSERT INTO [person] (name) VALUES ('Tom') GO 5

    So the GO 5 at the end would make that statement run 5 times so I would  have 5 rows with the name column as 5 but it does not seem to work. It tells me :

    Msg 102, Level 15, State 1, Line 1
    Incorrect syntax near 'GO'.

    Any ideas on how to make it work?

    Thanks!

    Saturday, September 21, 2013 4:58 PM

Answers

  • Hi Safinnator,

    According to your description, if you want to insert the same data many times in one table, I recommend you can use an temple table, you can refer to the following T-SQL statement.

    create table #Person(name varchar(20) not null)insert into #Person(name)values('Tom')go 5

    Or If you do not want to use a stored procedure /CTE, you can try using the following method.

    CREATE TABLE InsertedTable( 

    ID int IDENTITY (1,1)PRIMARY Key,col1 varchar(20),col2 varchar(20),col3 varchar(20))

    insert into InsertedTable (col1,col2,col3)

    select 'a','b','c'

    from (select row_number() over (order by object_id, column_id)seq

    from sys.columns) awhere a.seq <= 5

    SELECT * FROM InsertedTable

    Thanks,
    Sofiya Li


    Sofiya Li
    TechNet Community Support

    • Marked as answer by Sofiya Li Thursday, September 26, 2013 9:18 AM
    Monday, September 23, 2013 5:43 AM

All replies

  • If you are ok to use other methods why not use while loop in sql and just run this command in loop?

    Thanks,

    Dilkush


    Thanks, Dilkush Patel Microsoft SQL Developer Support Microsoft Corporation

    • Proposed as answer by Dilkush Patel Saturday, September 21, 2013 7:58 PM
    Saturday, September 21, 2013 7:58 PM
  • Hi Safinnator,

    According to your description, if you want to insert the same data many times in one table, I recommend you can use an temple table, you can refer to the following T-SQL statement.

    create table #Person(name varchar(20) not null)insert into #Person(name)values('Tom')go 5

    Or If you do not want to use a stored procedure /CTE, you can try using the following method.

    CREATE TABLE InsertedTable( 

    ID int IDENTITY (1,1)PRIMARY Key,col1 varchar(20),col2 varchar(20),col3 varchar(20))

    insert into InsertedTable (col1,col2,col3)

    select 'a','b','c'

    from (select row_number() over (order by object_id, column_id)seq

    from sys.columns) awhere a.seq <= 5

    SELECT * FROM InsertedTable

    Thanks,
    Sofiya Li


    Sofiya Li
    TechNet Community Support

    • Marked as answer by Sofiya Li Thursday, September 26, 2013 9:18 AM
    Monday, September 23, 2013 5:43 AM