Ehsan
Ehsan

Reputation: 3491

How Bulk insert in Sql

I have two similar tables in two databases. Person_Table1 in DB1 and Person_Table2 in DB2. I want have a bulk insert from Person_Table1 in DB1 to Person_Table2 on DB2.

Column Names in both tabels is similar. Count of Column in both tables is equal also count of column is many. How can i have a bulk insert like fallowing query:

insert into DB2.dbo.Person_Table2 (*) (SELECT * FROM DB1.dbo.Person_Table1 )

Upvotes: 2

Views: 1595

Answers (1)

marc_s
marc_s

Reputation: 754220

I would always use explicit column names - just to be very clear and safe when doing such an INSERT:

INSERT INTO DB2.dbo.Person_Table2(Col1, Col2, ...., ColN) 
   SELECT Col1, Col2, ...., ColN
   FROM DB1.dbo.Person_Table1

That way, you could also e.g. replace one column in the target table with e.g. a constant value or the value of a SQL function (like GETDATE() or something)

INSERT INTO DB2.dbo.Person_Table2(Col1, Col2, ...., ColN, ModifyDate) 
   SELECT Col1, Col2, ...., ColN, GETDATE()
   FROM DB1.dbo.Person_Table1

Upvotes: 1

Related Questions