Copying all tables from one database to another database
how to copy all the tables from one databse to another in sql server 2008 r2
Know the answer? Post it. Somebody with the same question will find it here.
Answer a question in the forums and earn +20 SHARP for every answer.
See unanswered questions
DevSocialUpcoming · 3rd editionAI Agents ConferenceDec 1–3, 2026Live onlineFree to attend5days leftRegister
Hemant SrivastavaRank #581 · 4.2K reputationPosted Sep 27, 2013, 11:24 AM EDT
For example, I am copying here ADDRESS Table from HKS databse to HKS_Testing database in following way:
-- Creation script for ADDRESS table
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
SET ANSI_PADDING ON
GO
CREATE TABLE [HKS_Testing].[dbo].[ADDRESS](
[P_Id] [int] NOT NULL,
[LastName] [varchar](50) NULL,
[FirstName] [varchar](50) NULL,
[Address] [varchar](50) NULL,
[City] [varchar](50) NULL,
CONSTRAINT [PK_ADDRESS_1] PRIMARY KEY CLUSTERED
(
[P_Id] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY]
GO
SET ANSI_PADDING OFF
GO
---- Creation table ends here
-- Now start copying the data
----INSERT INTO ADDRESS using SELECT
INSERT INTO [HKS_Testing].[dbo].[ADDRESS] (P_Id,LastName,FirstName,Address,City)
SELECT P_Id,LastName,FirstName,Address,City
FROM [HKS].[dbo].[ADDRESS]
----Verify that Data in ADDRESS table
SELECT *
FROM [HKS_Testing].[dbo].[ADDRESS]
GO
Hemant SrivastavaRank #581 · 4.2K reputationPosted Sep 27, 2013, 11:47 AM EDT
For example, suppose we have PID column as an identity column in HKS database, then query will be as:
-- Creation script for ADDRESS table
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
SET ANSI_PADDING ON
GO
CREATE TABLE [HKS_Testing].[dbo].[ADDRESS](
[P_Id] [int] IDENTITY(1,1) NOT NULL,
[LastName] [varchar](50) NULL,
[FirstName] [varchar](50) NULL,
[Address] [varchar](50) NULL,
[City] [varchar](50) NULL,
CONSTRAINT [PK_ADDRESS_1] PRIMARY KEY CLUSTERED
(
[P_Id] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY]
GO
SET ANSI_PADDING OFF
GO
----INSERT INTO ADDRESS using SELECT
INSERT INTO [HKS_Testing].[dbo].[ADDRESS] (LastName,FirstName,Address,City)
SELECT LastName,FirstName,Address,City
FROM [HKS].[dbo].[ADDRESS]
----Verify that Data in ADDRESS table
SELECT *
FROM [HKS_Testing].[dbo].[ADDRESS]
GO
Jaganathan BantheswaranRank #170 · 14.1K reputationPosted Sep 27, 2013, 7:52 PM EDT
SELECT * INTO NewDB.dbo.NewTable FROM OldDB.dbo.OldTable
Pankaj Kumar198 reputationPosted Sep 27, 2013, 7:59 PM EDT
http://stackoverflow.com/questions/187770/copy-tables-from-one-database-to-another-in-sql-server
http://blog.sqlauthority.com/2009/07/29/sql-server-2008-copy-database-with-data-generate-t-sql-for-inserting-data-from-one-table-to-another-table/
Hemant SrivastavaRank #581 · 4.2K reputationPosted Sep 30, 2013, 9:33 AM EDT