Click here to Skip to main content
15,040,212 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Suppose there is two table in a database named like emp1 and emp2 and the fields are in emp1(emp_id,emp_name) and in emp2(emp_address,emp_sal). Now how can I insert all the value in emp1 and emp2 in a same sql insert query?
Posted

Is there a good reason to keep these values in separate tables?

Or haven't you heard about Database Normalization[^]?
   
Comments
thatraja 9-Jun-12 13:52pm
   
Right, 5!
You cannot insert into two tables from a single SQL query.

If needed, you can try using Stored Procedure where calling one procedure will do the job (one SP can have multiple queries).
Refer:
MSDN: HOW TO: Call SQL Server Stored Procedures in ASP.NET by Using Visual C# .NET[^]
ADO.NET : Using Stored Procedures[^]
MSDN: CREATE PROCEDURE (Transact-SQL)[^]
   
Comments
Tim Corey 9-Jun-12 22:31pm
   
+5 Good answer. You could also use one transaction with two insert statements (so if one insert fails the entire transaction gets rolled back).
Sandeep Mewara 10-Jun-12 2:17am
   
Thanks. Yep - transactions can be used.
I think it not possible. You want to use two insert query or keep all records in one table
   

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