How to copy the data from one table to another table using sql stored procedure.

Posted by Karthik2010_Mca under Sql Server on 7/18/2013 | Points: 10 | Views : 1214 | Status : [Member] | Replies : 5
Hi All,
Please let me know how to copy one table to another table using sql stored procedure.

Let consider following example you have 2 batch A, B.A is your main batch and you want to copy module from A to B(Child batch).

Before copy both the batches have the following content uploaded in these batch
Table A
Session Content
1 Video1
2 Video2
3 Video3
4 Video4


Table B
Session Content
1 Abc
2 xyz
3
4

After Copy module your child batch content will be
Table B
Session Content
1 Video1
2 Video2
3 Video3
4 Video4

Karthik


Responses

Posted by: Allemahesh on: 7/18/2013 [Member] [MVP] Silver | Points: 25

Up
0
Down
Can I say batch is your table?

Karthik2010_Mca, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Karthik2010_Mca on: 7/18/2013 [Member] Starter | Points: 25

Up
0
Down

yes.

Karthik

Karthik2010_Mca, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Allemahesh on: 7/19/2013 [Member] [MVP] Silver | Points: 25

Up
0
Down
You can use the below query:-

TRUNCATE TABLE B;

INSERT INTO B ([Session], Content)
SELECT [Session], Content FROM A

Karthik2010_Mca, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Bandi on: 7/29/2013 [Member] [MVP] Platinum | Points: 25

Up
0
Down
-- Simply you can use UPDATE statement for the above output....
UPDATE B
SET B.Content = A.Content
FROM TableA A
JOIN TableB B on A.session = B.session

Mark This Response as Answer
--
Chandu
http://www.dotnetfunda.com/images/dnfmvp.gif

Karthik2010_Mca, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Ssj_Kumar on: 7/29/2013 [Member] Starter | Points: 25

Up
0
Down
First update the available record in the Table B, Then insert new records from Table A

UPDATE B
SET B.Content = A.Content
FROM TableA A
JOIN TableB B on A.session = B.session

INSERT INTO TableB ([Session], [Content])
SELECT A.[Session], A.[Content] FROM TableA A
where not exists(
select * from TableB B
where A.[Session]=B.[Session]
)

Regards,
Jayakumar Selvakani

Karthik2010_Mca, if this helps please login to Mark As Answer. | Alert Moderator

Login to post response