• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 434
  • Last Modified:

MS SQL Copy record into another table

Hi,

Is it possible to copy a record into another table without specifying all the fields/values, the fields are the same in both tables, one of the tables is an archive.

Thanks
0
sanjshah12
Asked:
sanjshah12
1 Solution
 
zadeveloperCommented:
yea:
insert into tableA 
select feild, field2 from TableB

or 

insert into tableA 
select * from TableB

Open in new window

0
 
zadeveloperCommented:
you can add where to that :)
0
 
chapmandewCommented:
do not forget to add a field list!!!!!

insert into tableA  (field1, field2)
select feild, field2 from TableB
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
waltersnowslinarnoldCommented:
Use the following sample by specifying the suitable id at WHERE clause..

INSERT INTO ArchiveTable (SELECT * FROM normalTable WHERE ID=the recordID)
0
 
zadeveloperCommented:
using MS SQL server you will have to remove the () encasing your select statment
0
 
zadeveloperCommented:
The field list is preferable, but not required (preferable because if you add more fields to TableA then this query will no longer work), however if TableA is only ever going to contain the exact amount of records as Table B  or your field B list.
ie:
insert into tableA  
select feild, field2 from TableB

this assumes that table A only has (and will only ever) contain 2 fields wich will match up to  feild, field2 from TableB
insert into tableA  
select * from TableB
 
this assumes that table A and TableB have the same number of columns
0
 
sanjshah12Author Commented:
Thanks Walters, that's great, thanks zadeveloper for heads up about the ().
0
 
chapmandewCommented:
>>The field list is preferable, but not required (preferable because if you add more fields to TableA then this query will no longer work),

Its is required when you're not inserted every field.
0

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now