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.
Sign in to answer this question
It is the same account you read, post and publish with — and you will come straight back to this page.
Hemant SrivastavaPosted Sep 30, 2013, 1:33 PM
Pankaj KumarPosted Sep 27, 2013, 11:59 PM
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/
Jaganathan BantheswaranPosted Sep 27, 2013, 11:52 PM
SELECT * INTO NewDB.dbo.NewTable FROM OldDB.dbo.OldTable
Hemant SrivastavaPosted Sep 27, 2013, 3:47 PM
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
Hemant SrivastavaPosted Sep 27, 2013, 3:24 PM
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