dd_
dd_

Reputation: 146

INSERT RANDOM values from one database to other

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.

Upvotes: 0

Views: 223

Answers (1)

Zohar Peled
Zohar Peled

Reputation: 82474

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.

Upvotes: 1

Related Questions