Creating table backup by insert into SQL command | Copying into an empty table.


--Copying data from one table 
--and inserts it into another table

--Need to create a backup table first

Create table tabEmployeeBackup(
EmpID int identity(1000,1),
EmpName char(20) not null,
Department char(30),
Designation char(20),
Salary int default 9000
);

--Execute the following code it will copy all records
--Retrieved by select query
Insert into tabEmployeeBackup 
Select EmpName,Department,Designation,Salary 
FROM tabEmployee

--New backup table created contains the identity field
--Identity field will not allow getting inserted manually
--So we should OMIT the identity field in the selection query
--also select * from tabEmployee throw the ERROR.

--Output
Select * from tabEmployeeBackup


1000   Neha Yadav          Development    Analyst Trainee     25000
1001   Shalini Patel       Development    Analyst Trainee     25000
1002   Abinesh Thangaraj   Development    Analyst Trainee     25000
1003   Syed Nawas          Development    Technical Trainer    9000
1004   Sameeha Syed        Development    Technical Trainer    9000
1005   Saleema Parvin      NULL           Technical Trainer    9000
1006   Asrafi Basha        Training       NULL                 9000
1007   Adham AS            Training       Behavioral Trainer   9000
1008   Ibrahim AS          Training       Behavioral Trainer   9000
1009      Esha AS             Training       Behavioral Trainer   9000
1010      Muhammad SAW        Training       Life Science                   9000

No comments:

Post a Comment