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);
con.Open();
SqlDataAdapter da1 = new SqlDataAdapter();
da1.SelectCommand = xp1;
DataSet ds1 = new DataSet();
da1.Fill(ds1, "Code");
GridView1.DataSource = ds1;
con.Close();
}
MSDN Community Support
Please remember to click "Mark as Answer" the responses that resolved your issue.
If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.
Member
2 Points
25 Posts
Need to insert a data row multiple times in the database.
Dec 31, 2015 06:39 AM|vs19911991|LINK
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);
con.Open();
SqlDataAdapter da1 = new SqlDataAdapter();
da1.SelectCommand = xp1;
DataSet ds1 = new DataSet();
da1.Fill(ds1, "Code");
GridView1.DataSource = ds1;
con.Close();
}
All-Star
18785 Points
3830 Posts
Microsoft
Re: Need to insert a data row multiple times in the database.
Dec 31, 2015 08:24 AM|Nan Yu|LINK
Hi vs19911991,
You may create the datatable depends on Qty , then try to use the "table type" parameters that allows multiple rows as parameters :
Refer to below articles for details :
https://msdn.microsoft.com/en-us/library/bb675163.aspx
http://stackoverflow.com/questions/1030848/how-to-pass-user-defined-table-type-as-stored-procedured-parameter-in-c-sharp
Best Regards,
Nan Yu
Please remember to click "Mark as Answer" the responses that resolved your issue.
If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.
Member
2 Points
25 Posts
Re: Need to insert a data row multiple times in the database.
Dec 31, 2015 08:47 AM|vs19911991|LINK
@Nan Yu I didnt get, could u pls help