Click here to Skip to main content
15,035,600 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Hi friends, how can i insert into two tables from single sql query ...
i want to insert into tbl1 values a,b,c and into tbl2 the values d,e,f but i want to use single query how can i achieve this please help with the code...
with regards bishnu karki
Posted
Updated 20-Dec-11 11:20am
v2

Start here[^].
   
Comments
Monjurul Habib 20-Dec-11 12:28pm
   
nice link 5!
You can not do in a one query but you can do it one transaction :)

As far as I know you are allowed to INSERT into or UPDATE one table per SQL statement. On the other hand, you can have multiple statements in a single query and separate the statement with a semi-colon ( ; ).

Or a snipplet that can help you:

SQL
BEGIN TRANSACTION; 
DECLARE @DataID int; 
INSERT INTO DataTable (Column1 ...) VALUES (....); 
SELECT @DataID = scope_identity(); 
INSERT INTO LinkTable VALUES (@ObjectID, @DataID); 
COMMIT; 
   
You need two separate INSERT statements, but it sounds like you want to get the IDENTITY from the first insert and use it in the second, in which case, you might want to look into OUTPUT or OUTPUT INTO:
REF: OUTPUT Clause (Transact-SQL)

Bur you can easily do that using SQL Parameterized Stored Procedures by Using the ODBC .NET Provider and Visual C# .NET

Another solution for Inserting data in multiple tables C#
   
Can you please give some example for this. What actually you need.? Please explain.
   

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