Need to insert a data row multiple times in the database. RRS feed

  • Question

  • User-267849662 posted

    I have two data tables in sql. I need to insert data feom one table to another. My first data table Table1 contain data column Code,Model,Num,Qty and second table Table2 also contain the same. But I need to insert the data from table1 to table2 multiple times. If Qty in Table1 is 4 then, insert the data of
    Table1 for 4 times according to the quantity.
    EX: Table1
    Code            Model           Num              Qty

    a1                   m12             12                  4

    Then the result may be in Table2 is

    Code                   Model             Num               Qty

    a1                         m12                12                   1
    a1                         m12                12                   1
    a1                         m12               12                    1
    a1                         m12               12                    1
    How can I achieve this? This is my code for insertion.
    protected void Button15_Click3(object sender, EventArgs e)
    String str1 = "insert into Table2(Code,Model,Num,Qty) select Code,Model,Num,Qty from Table1;";
    SqlCommand xp1 = new SqlCommand(str1, con);
    SqlDataAdapter da1 = new SqlDataAdapter();
    da1.SelectCommand = xp1;
    DataSet ds1 = new DataSet();
    da1.Fill(ds1, "Code");
    GridView1.DataSource = ds1;

    Thursday, December 31, 2015 6:39 AM

All replies