Click here to Skip to main content
15,885,757 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
hi frnds,

am working on asp.net project.using C# SqlServer 2005

I have 2 tables in SqlServer database

I want to move all the data from table A to Table B when submit button clicks on my asp.net webpage.

Please can u help me how to do this ?

Thanks in advance
Posted

SQL
query="INSERT INTO Table2(Tb2Column1, Tb2Column2)
  SELECT Tb1Column1, Tb1Column2 FROM Table1"
SqlCommand cmd = new SqlCommand(query, con);


NOTE:
1. Data Type of column should match with data type of column.
2. You can select desired number of columns.
3. Column with Primary key can not be copied.
 
Share this answer
 
v2
SQL
CREATE TABLE YOUTABLE_B(ID BIGINT,NAME VARCHAR(200),ADDRESS VARCHAR(500))
CREATE TABLE YOUTABLE_A(ID BIGINT,NAME VARCHAR(200),ADDRESS VARCHAR(500),
					DELETED_TIME SMALLDATETIME NOT NULL DEFAULT GETDATE())

INSERT INTO YOUTABLE_B VALUES(1,'YOUR_NAME','YOUR_ADDRESS')
INSERT INTO YOUTABLE_B VALUES(2,'YOUR_NAME','YOUR_ADDRESS')

Create TRIGGER dbo.TRG_YOU_TRG_NAME
ON dbo.YOUTABLE_B
FOR DELETE
AS
BEGIN
   INSERT INTO dbo.YOUTABLE_A
   (ID,NAME,ADDRESS,DELETED_TIME)
   SELECT ID,NAME,ADDRESS,getdate()
   FROM deleted;
END;
GO

DELETE FROM YOUTABLE_B WHERE ID = '1' -- or delete all. 
--on submit button you write ^ above Query 
-- data of table B will be inserted in TAble B automatically when trigger fired on it

select * from YOUTABLE_B
select * from YOUTABLE_A


Hope so it will be usefull for you
 
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