Virat
Virat

Reputation: 121

Stored procedure for copying entire data from one table to another table using SQL

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

Answers (3)

Gottfried Lesigang
Gottfried Lesigang

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

Pரதீப்
Pரதீப்

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

Dranes
Dranes

Reputation: 156

If both tables are the same, you could use this query:

INSERT INTO Persons_twin
SELECT * FROM Persons

Upvotes: 0

Related Questions