Reputation: 121
I have a table Persons (ID, Name, Address)
and another table Persons_twin (ID, Name, Address)
in which I want to copy whole rows from Persons
.
Which is the shortest and best way to copy the data? Below is the code I tried but no execution is happening. Your suggestions are most valuable. Thank you.
BEGIN
-- SET NOCOUNT ON added to prevent extra result sets from
SET NOCOUNT ON;
-- Insert statements for procedure here
INSERT INTO [dbo].[Persons_twin](ID, Name, Address)
VALUES(SELECT ID, Name, Address FROM [dbo].[Persons])
END
Upvotes: 0
Views: 8991
Reputation: 67311
You might even create the second table on-the-fly. In this case you would not have to bother about the table's structure:
SELECT * INTO persons_twin FROM Persons
Upvotes: 0
Reputation: 93734
Remove the values
clause and use select
query directly
INSERT INTO [dbo].[Persons_twin](ID, Name, Address)
SELECT ID, Name, Address FROM [dbo].[Persons]
Upvotes: 2
Reputation: 156
If both tables are the same, you could use this query:
INSERT INTO Persons_twin
SELECT * FROM Persons
Upvotes: 0