0

I have a project, and i need to insert values to NORTHWIND database from other database, AdventureWorks2012.

I know how to insert manualy values to one table in one database, but i can find out how to insert from another database.

I FORGOT TO SAY THAT I NEED RANDOM VALUES TO TAKE FROM AdventureWorks2012 DB AND INSERT IT TO Northwind DB.

I'm using T-SQL Server Management studio 2014.

Can someone post me syntax of inserting from one DB to other DB? I would be graceful.

dd_
  • 146
  • 1
  • 10
  • possible duplicate of [How to insert table values from one database to another database?](http://stackoverflow.com/questions/3502269/how-to-insert-table-values-from-one-database-to-another-database) – jmoerdyk Jun 01 '15 at 17:22

1 Answers1

1

assuming both databases are on the same server, or on linked servers, you just need to specify fully qualified names for the tables (database.schema.table):

INSERT INTO NORTHWIND.dbo.tableName (column 1, column 2 ... column n)
SELECT TOP 20 -- or whatever number of rows you want
       column 1, column 2 ... column n
FROM AdventureWorks2012.dbo.tableName
WHERE condition
ORDER BY NEWID()

Using the NEWID() function will generate a new guid. this is the simplest way to select random orderd data in sql server I'm aware of.

Zohar Peled
  • 79,642
  • 10
  • 69
  • 121