Update column from one DB table to another - sql-server-2008

I have DatabaseA which has table Episodes
I have restored a backup of that database locally (lets call that DatabaseB) a while back to my localhost instance in SQL Server.
I f'd up Episodes.Description in DatabaseA and want to restore that by selecting Description from the one on my local instance and to match by Episodes.ID
Not sure hot to go about that and also how to reference the different server instances (DatabaseB (local vs. the server housing DatabaseA
here's the updated statement that messed up all files, I forgot to highlight the where clause, classic stupid mistake and so it updated everything with the same description:
update episode set description =
'some text here'
so I wanna do something like this assuming I have context to the live server while in management studio which is why I'm not fully qualifying the server for the first part, only for local I am (non-working pseudo cod here):
update episode set [description] = [description] from [CHICAGOLAPTOP\MSSQLSERVER].[DimeCastsDotNet].[dbo].[Episode] episodeLocal
where ID = episodeLocal.ID

You just need to create a linked server on either side and then you access it by the 4 part name.
Linked Servers
Little more efficient if you create the link at the server that is being updated.

Related

Use 1 table in 5 mysql server and connect servers togther

hello this is my mysql table
table_clients
client_id
client_username (UNIQUE)
client_password
now i want to get 5 servers and install mysql on each server and use this table on each 5 servers.
my question is: is there any way that all 5 servers connected to each other (by 1 request) and converted to 1 big server? i mean if there is one user with username (hiworld) in server 2, can not create it this user again in other servers !. (connect all 5 server with same table together and make them 1 big server) .. is it possible?
my queries are too big (2-3 bilion) and i want to share them between 3-4 servers but (make servers united) like when use 1 server )
How to create linked server MySQL
"Cross Linked Servers" a functionality that exists on Ms Sql is the type of thing your looking for.
You would need to do some sort of insert trigger that checked to see if the name existed on the other server.
But this will be slow plus there is a risk of two servers getting the same name at the same time and allowing the insert because when it checked the other name was not already there.
No real good way of doing this. One idea may be to have only one master table for the table_clients. Make it a master to slave relationship. Any time you have to do an insert you insert to the master table/database, then copy that data to the slave instances. But you would still have to cross link the servers for this to work. What you are describing would require waiting on 5 servers to tell if the name has already been used. This way you only have to check on one server.

ms-access client server configuration

How can I easily set the path for the front end to connect to the backend database. Do I have to use the linked table manager every time ?
I found a tool here but it isn't very reliable. There is no simple config to set the back end path ?
When importing a table from another MS-Access database you can choose between a 'table' and a 'linked source'. If you now choose the 'linked source' and then you select the file of the other MS-Access file (where you select the table you want to link to ) you will get the table in your front-end Access as a normal table with one new property 'linked table'.
Now you can do everything on the selected data in both Access environments. The data will be synchronised as soon as you enter one record (make a change in one line and go to the next line). You do not need to visit the linked table manager any more at all.

Query 2 databases on 2 different SQL Servers

In reviewing many of the answers, don't see a solution something I feel should be simple.
I'm attempting to update a couple of fields in my Production database on one server from a restored database on another server because of a loss of data by our ERP vendor's update.
Anyway, have both servers connected in SSMS and just want to run the query below:
USE coll18_production;
GO
USE coll18_test2;
GO
UPDATE coll18_Production.dbo.PERSON
SET W71_ID_CRD_NO = T2.PERSON_USER1, W71_ID_CRD_DATE = T2.PERSON_USER9
FROM coll18_test2.dbo.PERSON as T2
WHERE coll18_Production.dbo.PERSON.ID = T2.ID;
I would think this would be a simple update, but can't make a query for 2 different server databases via the same tables.
Thanks if anyone can make this simple,
Donald
Okay, thanks for the input. In the essence of time I'm going to do something similar to what cpaccho recommended. Create a temp table containing the 2 fields that I want to update from in my Production database. Then I'm going to connect to my Test2 database that I restored from backup. Export these two fields as a csv file with the primary key and simply restore this table data into the temp. table in my production database. Then simply run my update from this temp table into the 2 fields in my production PERSON table where the ID's equal each other.
Have a great weekend,
Donald
The problem is that since the databases are on 2 different servers in order to join between them you will need a way for the servers to talk to each other.
The way to do that is through linked servers. Then you can set up your query to join the 2 tables together using 4 part naming (server.DB.Schema.Table) and accomplish your goal. The query will look sort of like this:
UPDATE Server.DB.Schema.Table1
SET column = b.column
FROM Server1.DB.Schema.Table1 a
INNER JOIN Server2.DB.Schema.Table2 b
ON a.column = b.column
Where a.column = something
You will only need to set up the linked server on one side and the Server name in the query will be the name you give the linked server. The only caveat is that this can be slow because in order to join the tables SQL Server may have to copy the entire table from one server to the other. I would also set up the linked server on the server you are updating (so that you run the update on the same server as the DB you are updating)
How to set up Linked Server Microsoft KB
A simple, rather hacky way would be to hard copy the table from database to database...
First create a table that containts the changes you want:
USE coll18_test2;
GO
SELECT PERSON_USER1, PERSON_USER9, ID
INTO dbo.MyMrigationOrWhateverNameYouLike
FROM coll18_test2.dbo.PERSON
Then go to SSMS, right click on coll18_test2 database --> Task --> Generate scripts and go with the assistant to generate a script for the newly created table. Don't forget to setup, in the advanced options, "Type of data to script" to "Schema and "Data".
Now that you have your script, just run it in your production database, and make your query based on that table.
UPDATE dbo.PERSON
SET W71_ID_CRD_NO = T2.PERSON_USER1, W71_ID_CRD_DATE = T2.PERSON_USER9
FROM dbo.MyMrigationOrWhateverNameYouLike as T2
WHERE coll18_Production.dbo.PERSON.ID = T2.ID;
Finally drop the MyMrigationOrWhateverNameYouLike table and you're done...

Settting up a SSMS Linked Server to DB2

I have a local SQL Server instance on which I created a Linked Server connection to a DB2 database named "DB2OurDatabase." In creating the Linked Server connection, I specified a UID and PWD that I use in various query tools or applications to query "[SchemaX].[TableX]."
I seemed to have success in creating the Linked Server: A Linked Server node Object by the name of "DB2OurDatabase" was created under the Linked Server node in SSMS and when I expand it, I am able to see the of tables in the database.
When I right mouse click on the [SchemaX].[TableX] table and select
"Script Table as => Select To ==> New Window", a new query window was opened with the text
--[DB2OurDatabase].[DataCenterCityName2_DB2OurDatabase].[SchemaX].[TableX]
contains no columns that can be selected or the current user does not have permissions on that object.
GO
I don't understand how I was able to create a Linked Server that can see the table names in the database but yet apparently seem to encounter what appears to be a lack of rights to query the table even tghough I am using same credentials that I have used in Squirell SQL query tool, for example, to query the table.
In SSMS, I tried to execute this
SELECT *
FROM [DB2OurDatabase].[DataCenterCityName2_DB2OurDatabase].[SchemaX].[TableX]
Error:
Msg 7314, Level 16, State 1, Line 1
The OLE DB provider "IBMDADB2" for linked server "DB2OurDatabase]" does not contain the table ""DataCenterCityName2_DB2OurDatabase"."SchemaX"."TableX"". The table either does not exist or the current user does not have permissions on that table.
I was a little surprised that the fully qualified table name included [DataCenterCityName2_DB2OurDatabase] since I did not specify this when I set up the Linked Server connection, but the name of the DataCenter city was correct so I took this as a further sign that the Linked Server connection was successful.
Nevertheless, I also tried to execute remove this level of the fully qualified table name:
SELECT *
FROM [DB2OurDatabase].[SchemaX].[TableX]
which resulted in this error.
Msg 208, Level 16, State 1, Line 1
Invalid object name 'DB2OurDatabase.[SchemaX].[TableX]'.
What do I need to do to create a DB2 Linked Server that lets me query the tables in the DB2 database? Here's my linked server properties:
I haven't investigate what are probably multiple ways to conenct and query DB2 from Sql Server, but this worked:
SELECT * FROM OPENQUERY(DB2OurDatabase, 'SELECT * FROM SchemaX.TableX')
Obviously, you modified the actual commands by replacing object names, so it's impossible to be sure, but the problem may be caused by your use of quoted identifiers (those square brackets), which essentially makes the object names case-sensitive. DB2 will by default create object (table, schema) names in uppercase, unless they are quoted. create table MySchema.MyTable... (unquoted) on the DB2 side will create the table MYSCHEMA.MYTABLE, and referencing it later from SSMS as [MySchema].[MyTable] (using quoted identifiers) will obviously fail.
These are the 3 steps that bring me to the solution:
Download and install Microsoft OLE DB Provider for DB2 Version 6.0 (this is the latest version but by the time you read this post there might be a new version now)
From the start menu open the Data Access Tool > File > New Data Source and complete all the steps: provide the notorious credentials like Server, Port, Database, User, Password. If unsure contact your DBA. Once completed test the connection and copy the Connection String
Now on SSMS go to Server Object > Linked Servers > New Linked Server and fill up like in picture setting up in the Provider string the string you copied before from the Data Access Tool:
Done, you are good to go now,

Copying Database From One SQL Server to Another

Greetings.
We are running Microsoft SQL Server 2008 on one machine with a single license. We need to create an identical development instance of a database held on this server, including tables, triggers, default values, data, views, keys, constraints and indexes.
As a temporary solution, I downloaded and installed SQL Server 2008 Express R2 along with the SQL Server 2008 Toolkit on a separate machine. I then used DTSWizard.exe and pointed it at the remote host as the data source and the local machine as the target.
Transfer of data at first appeared to be fine as the tables, indexes, etc. were created but after a little more digging, I realized it was NOT transferring/setting the default values of any fields! Many of the fields have "NOT NULL" constraints and we're interfacing with a COM API (Response RCK) which does not allow us to manually edit the queries so we're stuck with how they have interface with the database/insert entries (including the use of default values circumventing the NOT NULL constraints.)
As a second option we used the "Generate Script" option and exported all tables, constraints, indexes, default values, data, etc as a .SQL file but now I'm not sure how to load this SQL file into SQL Server because it is 4.9GB - All of which is required, no circumventing the size of this monster.
So my questions are:
- Is there a way I can make a complete copy of SQL database to another server including default values?
- Or is there a way to import a .SQL file without copying and pasting it as a New Query?
P.S: Apologize if my "Microsoft" lingo is not perfect, I'm a Linux guy familiar with PostgreSQL and mySQL.
Why not just take a complete backup of the database and restore it to the new server? That will include everything including default values?
Here is some SQL that should make it happen (edit paths and logical file names to fit your needs):
-- On the source server run:
BACKUP DATABASE [TestDb]
TO DISK = N'C:\TEMP\TestDb.bak'
WITH
NOFORMAT,
NOINIT,
NAME = N'SourceDb-Full Database Backup',
SKIP,
NOREWIND,
NOUNLOAD,
STATS = 10
GO
-- On the other server run
RESTORE DATABASE [DestDb]
FROM DISK = N'C:\Temp\TestDb.bak'
WITH
FILE = 1,
MOVE N'TestDb' TO N'C:\TEMP\DestDb_data.mdf',
MOVE N'TestDb_log' TO N'C:\TEMP\DestDb_log.ldf',
NOUNLOAD, STATS = 10
GO
and you need to move the backup file between the servers if it is not accessible over the network...
Finally came across a solution that works.
In SQL Server 2008, there appears to be a bug when either exporting a database in which DEFAULT values are not carried with the table structures.
Here is my solution for circumventing this:
Right-click on the database you wish to backup.
If "Welcome to the Generate SQL Server Scripts wizard" dialog appears, click next. Otherwise continue to next step.
Select the database you wish to transfer.
There key things to ensure you select properly are as follows:
Set Script Defaults to True
Script USE DATABASE to False
Script Data to True
Script Indexes to True
Script Primary Keys to True
Script Triggers to True
Script Unique Keys to True
Once you've finished setting other optional parameters, click Next >.
Check Stored Procedures, Tables and View (do not check Users unless you want to/need to.) and click Next >.
Click Select All to select all Stored Procedures and click Next >.
Click Select All to select all Tables and click Next >.
Click Select All to select all Views and click Next >.
Under Script mode, select Script to file.
Click the Browse... Button and select the folder and filename you wish to save the SQL script under. In this example we'll use my_script.sql.
Click Finish.
Now that we have the entire database backed up including tables, views, stored procedures, indexes, data, etc. it's time to import this data into a new database.
On the machine you wish to restore this information to, perform the following steps:
Open your command prompt by clicking Start -> Run... or Pressing Windows (Super) + R on your keyboard.
Type "cmd" without the quotes in the Run dialog and click OK.
Browse to the directory your SQL file is located at. In my case, cd "C:\Documents and Settings\Administrator\Desktop"
Type "sqlcmd -s [server][instance] -i my_script.sql" ... [server] is whatever the name of your Windows machine and [instance] is whatever the name of your SQL instance is. For SQLExpress it is "SQLEXPRESS" without the quotes.
Press Enter and you're on your way!
Hope this helps someone else who has encountered the maraud of issues!
Is possible to run a both query from a single server
-- On the source server run:
BACKUP DATABASE [TestDb]
TO DISK = N'C:\TEMP\TestDb.bak'
WITH
NOFORMAT,
NOINIT,
NAME = N'SourceDb-Full Database Backup',
SKIP,
NOREWIND,
NOUNLOAD,
STATS = 10 GO
-- On the other server run
RESTORE DATABASE [DestDb]
FROM DISK = N'C:\Temp\TestDb.bak'
WITH
FILE = 1,
MOVE N'TestDb' TO N'C:\TEMP\DestDb_data.mdf',
MOVE N'TestDb_log' TO N'C:\TEMP\DestDb_log.ldf',
NOUNLOAD, STATS = 10
GO