How to execute a SQL statement between two separate MySQL connections? - sql

All, is there any means by which it is possible to connect to two separate MySQL databases and execute SQL statements to interact with both?
The aim would be to query DB 1 on connection 1 and insert the results into DB 2 on connection 2.
Is it possible to do this using the MySQL query browser?

I suppose there are two ways how to do this.
the first is manual : DB dump : " SELECT * INTO OUTFILE 'authors090308.sql' FROM authors; " and then "LOAD DATA INFILE 'authors090308.sql' INTO TABLE authors;" (you also need to specify some other parameters as to delimiting columns etc.) or by some other "database dump", where data goes into file the way you want. In between you must switch database manually to the second one.
the second is programmatic : with programming language : Then you need to just open two connections to the databases and realize the statements you need. I don't know if it is what you mean, you'd need to be more specific.
Anyway, you need a connection to a DB server, either via mysql client, oracle sql developer, mysql workbench or via mysql adapter/driver of a programming language. What you need, is the "glue" between 2 connections, which is the programming language. It's either a programming language or a file data is dumped into and back again.
I'm sorry but I don't thing there are any other ways of doing this...as to the first way, you can only get the output from a database and either use mysql pager and pipe the output somewhere (a file perhaps), or SELECT * INTO OUTFILE, mysqldump, mysqlhotcopy etc.

Related

Alternate codes for ExecuteReader

It is my first time to post here. My question is what code can we use instead of ExecuteReader? We're currently doing a total production program and we want to input the productions, Projects 1-4 (I label it P1-P4) into the text box, but i have read that ExecuteReader is only for SQL, we're only using Access DB. Our code for displaying the projects to textboxes
You apparently don't know what "SQL" is. SQL (structured query language) is not a database. It is a language that databases use to define and manipulate data. Just about all databases use SQL for this purpose, including Access.
There are a number of databases with "SQL" in the name, e.g. SQL Server, MySQL and SQLite, but there's no database named "SQL". Some people lazily refer to Microsoft's SQL Server as just SQL or maybe MS SQL but its name is SQL Server.
As for using ExecuteReader, it's for any database. If you were connecting to SQL Server then you'd use a System.Data.SqlClient.SqlCommand object and call ExecuteReader on that. As you're using Access, you need to use a System.Data.OleDb.OleDbCommand and call ExecuteReader on that. They both work the same way but the different providers are for different data sources.

Updating multiple tables data from different databases having same column name

I have 11 databases in which I'm having tables contains User Details i.e. all employee details. There I have a column "Status"(which is 1 for Active and 0 for Inactive). I have a regular tasks for updating "Status" column value 0 or 1 for mentioned employees and for that, I have to go through all the databases then User table then I have to update. The same task i have to do for all the database and it consumes a lot of time.
If I will get a short Query or Procedure that I have to run once and will do all updation at once then, it would be a great help.
I see a couple of possible options.
You could build an SSIS package to connect to each database and do the necessary updates provided the criteria of which employees to update and what to update them to could be found within the database or some external source such as a text file.
Alternatively, you could use SQLCMD mode in SQL Server Management Studio and then within your SQL script use CONNECT command to switch to each server and database something like this...
:CONNECT Server1
USE Database1
--put your update SQL script
:CONNECT Server2
USE Database2
--put your update SQL script
...
These links provide some further information on using SQLCMD mode...
Connecting to multiple servers in a Query Window using SQLCMD
SQL Server SQLCMD Basics
Noel
As you mentioned, you have 11 databases.
Problem : First, you are using very bad approach for database design,
What really Happens : When you are using multiple databases and you need to check in every database, then the server needs to connect to different database again and again, which takes very more time compared to switching into the tables, because of connection handling.
Solution : In your case, you have only one option to connect different databases in loops and then run the query in the loop for every DB.
Suggestion : you should keep all the data in the same database, you can use an extra column in tables to keep track your data to different entities.

"Select data source" popping-up when executing a SQL writed query

I try to create a pretty complex database on ms Access 2013, so I wanted to type it directly in SQL. It has no errors, as other DBMS can fully build the database from the script I wrote (for example, phpmyadmin imports it with no difficulty).
On this tutorial, it is showed how to write a SQL query in order to build tables. I thought this way matched well with my goal as I could copy-paste my script in the query and run it to create the whole thing.
But when I tried to open/double-click on the query a pop-up appears saying "Select data source", waiting for me to select an ODBC, either from a file or a host, before continuing and executing the query.
I tried other types of queries (creating only one table at time, trying on a blank file, or even SELECT * FROM *), bt this message keeps showing up and I really don't know how to deal with it as I don't want to connect to anything but the infile database.
Does anyone got a hint about what to do in this case?
Or, even better, how could Access import my SQL script in order to create the database?
You should configure the database connection in the ODBC and check whether the connection is established or not. Once the connection is established, you can run the query to fetch the data or create tables as per your requirement.

Move data from one database to another over ODBC

Let say, I am connected to two different databases (one SQLite and one Oracle) over ODBC in one program. Is it possible to execute a query on one database and insert the resultset as a new table in the second database directly by just passing something like a data cursor, i.e. without the hurdle to create insert statements with explicit values from the result set and executing those on the destination database?
As far as I am aware you cannot do that. You can use something like a join engine to do it (e.g., http://www.easysoft.com/products/data_access/odbc_odbc_join_engine/index.html) but it might be overkill if you are doing it just the once.
If you use Oracle then you can use Oracle Heterogeneous Services which can work with ODBC sources. Have a look at: http://www.dba-oracle.com/t_heterogeneous_database_connections_sql_server.htm

How to do a search and replace of a part of a string in all columns in all tables in an sql database

Is it possible to search and replace all occurrences of a string in all columns in all tables of a database? I use Microsoft SQL Server.
Not easily, though I can thing of two ways to do it:
Write a series of stored procedures that identify all varchar and text columns of all tables, and generate individual update statements for each column of each table of the form "UPDATE foo SET BAR = REPLACE(BAR,'foobar','quux')". This will probably involve a lot of queries against the system tables, with a lot of experimentation -- Microsoft doesn't go out of its way to document this stuff.
Export the entire database to a single text file, do a search/replace on that, and then re-import the entire database. Given that you're using MS SQL Server, this is actually the easier approach. Microsoft created the Microsoft SQL Server Database Publishing Wizard for other reasons, but it makes a fine tool for exporting all of the tables of a SQL Server database as a text file containing pure SQL DDL and DML. Run the tool to export all of the tables for a database, edit the resulting file as you need, and then feed the file back to sqlcmd to recreate the database.
Given a choice, I'd use the second method, as long as the DPW works with your version of SQL Server. The last time I used the tool, it met my needs (MS SQL Server 2000 / 2005) but it had some quirks when working with database Roles.
In MySQL, you can do it very easily like this:
update [table_name] set [field_name] = replace([field_name],'[string_to_find]','[string_to_replace]');
I have personally tested this successfully on a production server.
Example:
update users set vct_filesneeded = replace(vct_filesneeded,'.avi','.ai');
Ref: http://www.mediacollege.com/computer/database/mysql/find-replace.html
A good starting point for writing such a query is the "Search all columns in all the tables in a database for a specific value" stored procedure. The full code is at the link (not trivial, but copy/paste it and use it, it just works).
From there on it's relatively trivial to amend the code to do a replace of the found values.