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,
Related
I am trying to query a linked server, when I expand object explorer in SSMS, I can see all db's on the server and all tables within the respective db's but cannot expand to see the columns. I also cannot run any queries with the following errors:
1 - If I right click on the table name and click script table as - select to - new query window, I get the following error
[LinkedServerName].[singhm]..[testtable] contains no columns that can be selected or the current user does not have permissions on that object.
2 - If I run the an openquery statement as follows:
select *
from openquery(LinkedServerName ,'select * from [singhm]..[testtable]')
I get the following error:
Cannot initialize the data source object of OLE DB provider "MSDASQL"
for linked server "LinkedServerName".
For context purposes, My linked server is an ODBC connection to a MySQL db datasource.
I would be grateful for any advice and/or direction regarding this matter.
Many thanks,
Manpaal Singh
I needed to download and install a different odbc provider. I can now query the linked server using the following syntax.
select top 10 * from openquery(MYSQL,'select * from singhm.testtable')
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.
I would like to transfer data from a MS SQL Server database to a MySQL database. So, I added a linked server to MS SQL so that I can use Openquery to insert the data in the MySQL database. I want to optimize the performance of the data transfer and I found the guidelines for improving performance of data loading in MySQL.
One optimization consists of disabling AUTOCOMMIT mode, however I was not able to do it using Openquery.
I tried both ways:
SELECT * from openquery(MYSQL,'SET autocommit=0')
exec openquery(MYSQL,'SET autocommit=0')
and I got:
Cannot process the object "SET autocommit=0". The OLE DB provider
"MSDASQL" for linked server "MYSQL" indicates that either the object
has no columns or the current user does not have permissions on that
object.
Is it possible to execute such statements through openquery?
Thanks,
Mickael
OPENDATASOURCE() and OPENROWSET() allow for add-hoc server connections. You do not need to define a linked server ahead of time.
The OPENQUERY() depends upon a static linked server being defined ahead of time.
Here is the MSDN reference.
http://technet.microsoft.com/en-us/library/ms188427.aspx
Most of the examples show a DML (SELECT, UPDATE, DELETE, INSERT) using the OPENQUERY() as the source or destination of the command. What you are trying to do is execute a session command. Therefore it will fail. Also, you might not even know if the session stays open for the next call.
Why not package up the logic on the MYSQL server as a stored procedure. The stored procedure can be executed on a linked server by using a four-part name?
For example:
INSERT INTO #results
EXEC server.database..stored-proc
This assumes MYSQL has the same object structure as ORACLE. Since I am not a MYSQL person, I can not comment. I allow you to research this little item.
But this should work. It will allow you to package any type of logic in the MYSQL database.
If you want to use SSIS to transfer data from SQL Server to MYSQL.
For the ADO.NET Destination to work properly, the MySQL database needs to have the ANSI_QUOTES SQL_MODE option enabled. This option can be enabled globally, or for a particular session. To enable it for a single session:
1 - Create an ADO.NET Connection Manager which uses the ODBC driver
2 - Set the connection manager’s RetainSameConnection property to True
3 - Add an Execute SQL Task before your data flow to set the SQL_MODE – Ex. set sql_mode='STRICT_TRANS_TABLES,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION,ANSI_QUOTES'
4 - Make sure that your Execute SQL Task and your ADO.NET Destination are using the same connection manager.
Matt Mason did this on a reply.. The key is item #2, use the same connection.
http://blogs.msdn.com/b/mattm/archive/2009/01/07/writing-to-a-mysql-database-from-ssis.aspx#comments
Also, CozyRoc has a custom ODBC driver that might be faster / more reliable than the free one from MYSQL.
http://cozyroc.com/ssis/odbc-destination
I'm trying to joinning SQLServer 2008 R2 tables with msaccess table (*.mdb).
I already tried "OPENDATASOURCE" and "Linked Server", but no one of them is work correctly.
in example, I've got the following message:
OLE DB provider "Microsoft.Jet.OLEDB.4.0" for linked server
"TestLinkServer" returned message "Cannot open database ''. It may
not be a database that your application recognizes, or the file may be
corrupt.".
the other error message:
OLE DB provider "Microsoft.Jet.OLEDB.4.0" for linked server "MDBTest"
returned message "The Microsoft Jet database engine cannot open the
file '\10.55.56.34\Shared Folder\LBUS.mdb'. It is already opened
exclusively by another user, or you need permission to view its
data.".
and many more :D
can anyone give the working tutorial?
thanks in advance.. :)
The easiest way is to do the join inside ms-access.
Set up a table link in your access database that references the sql-server table you want to join.
Then build a query in access that joins that table with one or more tables in the access database.
If you want to join more than one sql-server table, first create a view in sql-server that combines all the relevant tables. Then set up your table link to reference the view.
If, for some reason, you must do the join inside SQL server, you will have to use a different technique, or use the table link feature to "push" data from the access table to a (previously defined) sql server table. Then, it's just an ordinary join.
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