I have two SQL Server instances on two virtual machines, Test server and Production server.
I need to do an UPDATE to the production server taking data values from test server.
For example :
UPDATE [server_production].dbname.mytable
SET column1 = [server_test].dbname.mytable.column1
How do I do this ?
On your production server, define a Linked Server that points to your test server.
Then, on your production server you can run a query similar to the following to update your column:
UPDATE p
SET column1 = t.column1
from <dbname>.<schema>.mytable p
join <TestLinkedServerName>.<dbname>.<schema>.mytable t
on p.<id> = t.<id>
In the above query, you'll need to provide values for the placeholders:
<dbname> - the name of the prod/test database
<schema> - the schema in which the table is defined (typically dbo)
<TestLinkedServerName> - the name you've given to the linked server
<id> - your PK column, or a column that uniquely identifies rows and provides a way to join the two tables
Have a look at redgate sql data compare
http://www.red-gate.com/products/sql-development/sql-data-compare/
Related
I have a server called GreatPlains and I would like to create a new table (not already defined) using the INSERT INTO clause onto my local server's reporting database. We have a linked server set up for the GreatPlains server and our main production server.
Simplified version of current query:
SELECT *
INTO [local].[Reporting].[dbo].[NewTable]
FROM [linked].[Main].[dbo].[Orders]
I'm also getting the error:
The object name 'local.Reporting.dbo.NewTable' contains more than the
maximum number of prefixes. The maximum is 2.
There are two mistakes in your query
1.INTO clause support maximum of 2 prefixes. You cannot include SERVER NAME
DATABASE_NAME.SCHEMA_NAME.TABLE_NAME
2.Unwanted INSERT ketword
So your query should be
SELECT *
INTO [Reporting].[dbo].[NewTable]
FROM [linked].[Main].[dbo].[Orders];
I think you have an extra insert:
SELECT *
INTO [local].[Reporting].[dbo].[NewTable]
FROM [linked].[Main].[dbo].[Orders];
If the table is already defined and has the same columns in the same order, then you can do:
INSERT INTO [local].[Reporting].[dbo].[NewTable]
SELECT *
FROM [linked].[Main].[dbo].[Orders];
If you are running the script in your local server and table already exists in the database,Use the below script.
USE [Reporting]
GO
INSERT INTO [dbo].[NewTable]
SELECT *
FROM [linked].[Main].[dbo].[Orders]
If you don't have the table in your database,use the below script.
USE [Reporting]
GO
SELECT *
INTO dbo.[NewTable]
FROM [linked].[Main].[dbo].[Orders]
I have two SQL Server database servers in two different server computers.
Server A - 192.168.1.100
Server B - 192.168.2.102
I need to execute a query from server A to retrieve data from a table in the Server B.
How to write the SQL select statement to perform this?
Are there any server configurations to allow these type of querying?
Add a linked server here are commands for 1 way of doing this. replace the user and password values with an appropriate SQL credential.
EXECUTE master.dbo.sp_addlinkedserver #server = N'192.168.2.102', #srvproduct=N'SQL Server'
EXECUTE master.dbo.sp_addlinkedsrvlogin #rmtsrvname=N'192.168.2.102',#useself=N'False',#rmtuser='ASQLLogin',#rmtpassword='Password'
then simply query like you would a normal table but append the linked server in front as commented above linked_server.db_name.schema_name.table_name like so:
SELECT *
FROM
[192.168.2.102].[DatabaseName].[SchemaName].[TableName]
You can even join it to your local server A if you want.
SELECT *
FROM
[192.168.2.102].[DatabaseName].[SchemaName].[TableName] b
INNER JOIN SomeTableOnServerA a
ON b.ID = a.ID
I have few tables I need to load from linked firebird server into SQL Server from time to time. I use statement like this:
SELECT * into dekr FROM OPENQUERY ( [PLINK] ,'select * from dekr' )
It takes a while since it's going over network etc, is there a way to update once created
table dekr only with changes since last time?
thanks
Hai,
I have two database and I want to select one value from one of the databases....
for that I want to pass one value and if that value is stored in the database I want to pick the id representing the value in the database.
that means the operation is that.....
first I select a row of data from one database by using a user control...
in that row there is a value (example "apple") and I want pass this value("apple") to the second database... in the second database the value("apple") having a id (example "australian") I want that the query search for that id("australian") and show that in the text box.
Please help me....
thanks to all advance....
example
first database
id name details
1 apple sweet
2 orange sweet
second database
id name details
Australian apple sold
Indian banana sold
Imagine that there are the two databases....
using a user control I select first row from first database and I want to pass that value apple to second database and find out the id australian from the second database and show that in a text box....
thank you.........
You can do a join between the two databases as long as you use the fully qualified prefix for each one.
I think you should go for the join , your query should look something like this
SELECT SecondDataBase.TableName.Id
FROM FirstDatabase.TableName INNER JOIN
SecondDataBase.TableName ON FirstDatabase.TableName.["Column contains Apple"] = SecondDataBase.TableName.["Column contains Apple"]
Fully qualified table name, which includes server name, db name, schema and table (e.g. MySqlServerInstance1.mydb1.dbo.table1) name will definitely work as long as one database server has a registered reference within a calling DB server. See this for things you have to do if you are using MS SQL Server: http://msdn.microsoft.com/en-us/library/ms188231.aspx
In MSSQL you normally reference a table using SchemaName.TableName
dbo.Fruit
The database is automatically determined by your connection string. FirstDatabase
So when you use dbo.fruit, the server automatically appends the database name to the table like
FirstDatabase.dbo.Fruit
If the user account has permission, you can select from a completely different database by specifying the database
SecondDatabase.dbo.FruitSales
To take it even further you can select from an entirely different SQL server if you have set up a linked server by specifying the linked server name like
SecondServer.ThirdDatabase.dbo.FruitShipping
So you can join between a table in your database and a table in your second database like
SELECT *
FROM FirstDatabase.dbo.Fruit AS F INNER JOIN
SecondDatabase.dbo.FruitSales AS FS ON F.Something = FS.Something
But you can even join between a table in your database and a table on a different server like
SELECT *
FROM FirstDatabase.dbo.Fruit AS F INNER JOIN
SecondServer.ThirdDatabase.dbo.FruitShipping AS FS ON F.Something = FS.Something
I am using sql server 2005
I have a table [say tblHistory] and this table contains 100 rows.
I have created the same table at the server, but the table doesn't have the data, I want data from tblHistory to convert into
INSERT INTO tblHistory ------
so that I could run the script on the server to fill the database.
To generate all the INSERT INTO statements you need based on table data, take a look at this project: http://www.codeproject.com/KB/database/sqlinsertupdategenerator.aspx
you need to create a linked server between the two servers and then you do something like this
INSERT INTO tblHistory
select * from LinkedServerNAme.DatabaseName.SchemaName.tblHistory
To add a linked server read this http://msdn.microsoft.com/en-us/library/ms190479.aspx
BTW you can also use OPENROWSET, OPENDATASOURCE, SSIS or bcp out and bcp in
Not sure I understand the question... you just want to copy the contents of one table into another?
INSERT INTO newTable SELECT * FROM tblHistory
If the new table doesn't already exist, you can use SELECT INTO:
SELECT *
INTO new_table
FROM tblHistory
But that's the caveat - it has to be a new table, no data already in there.
Otherwise, use:
INSERT INTO new_table
SELECT x.* --preferrable to actually define the column list than use *
FROM OLD_TABLE x
You should check out the SSMS Tools Pack - one of its feature is the ability to generate those INSERT scripts you're looking for!
Get the SSMS Tool Pack from this site - it's an add-in for SQL Server Management Studio - highly recommended!