Click here to Skip to main content
15,898,134 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
I want to choose data from table1 + data from table2 & insert into table3.


Can somebody help me plssssss......
Posted

Try something like this,

SQL
INSERT INTO Table3 (Column1, Column2, Column3)

SELECT  a.Column1_1, a.Column2_1, b.Column2_2
FROM    Table1 a
JOIN    Table2 b
ON      a.Column1_1 = b.Column1_2
 
Share this answer
 
Comments
Member 9693583 3-Jan-13 15:47pm    
It is not working....

I m trying below code

protected void btnAllo_Click(object sender, EventArgs e)
{
SqlConnection con = new SqlConnection("SERVER=KSHITIJA-PC; Initial Catalog=CRM;Integrated Security=True");
//SqlCommand cmd = new SqlCommand("Update Call_reg set status='Assigned' Where Complaint_no= '" + ddlComplaint.SelectedItem.ToString() + "'", con);
SqlCommand cmd = new SqlCommand(" insert into Call_Allocation values('select a.Fname from New_Tech a join Call_reg b on a.Fname=b.Complaint_no '", con);
try
{
con.Open();

int i = cmd.ExecuteNonQuery();
//int a = cmd1.ExecuteNonQuery();
if (i == 1)
{
ddlComplaint.SelectedItem.Text = "";
EO.Web.MsgBoxButton mb = new EO.Web.MsgBoxButton("OK");
MsgBox1.Show("Message: ", "Call Assigned to Technician", null, mb);
}

}
catch (Exception ex)
{
EO.Web.MsgBoxButton mb = new EO.Web.MsgBoxButton("OK");
MsgBox1.Show("Error: ", ex.Message, null, mb);

}
con.Close();
}
[no name] 3-Jan-13 19:48pm    
You have to mention column names for your Call_Allocation table. Try this

SqlCommand cmd = new SqlCommand(" insert into Call_Allocation (Name)
select a.Fname from New_Tech a join Call_reg b on a.Complaint_no = b.Complaint_no ", con);
[no name] 3-Jan-13 19:49pm    
Check your query in SQL Server Management Studio first.
SQL
insert into table1(column1, column2)
select t1.col1, t2.col2 from table1 t1 inner join table2 t2 on t1.id = t2.id
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900