i have tableA in sql database ,
and tableB in mysql database ,
How to write the join and which function should i use for that(myssql_query or mssql_query )
Thanks
You can't do that unfortunately. Even if you connected to both via ODBC, you'd still have two separate connections. Besides MySQL knows nothing about MSSQL, and MSSQL knows nothing about MySQL.
An additional layer of abstraction would be required, but it would possibly be very inefficient.
So far as I know it is not possible with default PHP (mysql and mssql) functions, but I'm pretty sure that it is possible with ODBC on your machine.
With ODBC you can make cross DB connections between MySQL and MSSQL. So I think you can create a query like this:
SELECT
MYSQL.db.tbl_x.*
LEFT JOIN
MSSQL.db.tbl_y
ON
MYSQL.db.tbl_x.id=MSSQL.db.tbl_y=id
If you only would like to copy some data, I recommend Navicat.
Related
I am trying to generate a report by querying 2 databases (Sybase) in classic ASP.
I have created 2 connection strings:
connA for databaseA
connB for databaseB
Both databases are present on the same server (don't know if this matters)
Queries:
q1 = SELECT column1 INTO #temp FROM databaseA..table1 WHERE xyz="A"
q2 = SELECT columnA,columnB,...,columnZ FROM table2 a #temp b WHERE b.column1=a.columnB
followed by:
response.Write(rstsql) <br>
set rstSQL = CreateObject("ADODB.Recordset")<br>
rstSQL.Open q1, connA<br>
rstSQL.Open q2, connB
When I try to open up this page in a browser, I get error message:
Microsoft OLE DB Provider for ODBC Drivers error '80040e37'
[DataDirect][ODBC Sybase Wire Protocol driver][SQL Server]#temp not found. Specify owner.objectname or use sp_help to check whether the object exists (sp_help may produce lots of output).
Could anyone please help me understand what the problem is and help me fix it?
Thanks.
With both queries, it looks like you are trying to insert into #temp. #temp is located on one of the databases (for arguments sake, databaseA). So when you try to insert into #temp from databaseB, it reports that it does not exist.
Try changing it from Into #temp From to Into databaseA.dbo.#temp From in both statements.
Also, make sure that the connection strings have permissions on the other DB, otherwise this will not work.
Update: relating to the temp table going out of scope - if you have one connection string that has permissions on both databases, then you could use this for both queries (while keeping the connection alive). While querying the table in the other DB, be sure to use [DBName].[Owner].[TableName] format when referring to the table.
your temp table is out of scope, it is only 'alive' during the first connection and will not be available in the 2nd connection
Just move all of it in one block of code and execute it inside one conection
temp is out of scope in q2.
All your work can be done in one query:
SELECT a.columnA, a.columnB,..., a.columnZ
FROM table2 a
INNER JOIN (SELECT databaseA..table1.column1
FROM databaseA..table1
WHERE databaseA..table1.xyz = 'A') b
ON a.columnB = b.column1
Hello im developing an application where data comes from MSSQL database. Here using php odbc connection i get all the needed data to work with app. But now i have a problem:
Im looking for:
All data from mssql (assume) mssql_Table_A will be listed in this app, each row will be given with checkbox so user can check the data required, after checking When he hit save checked data will be stored in local MySQL database (assume) mysql_table_A.
So next time when he again wants few more data, this app will list all the data from mssql_table_A which dont exists in mysql_table_A.
Problem is:
If both the dataflwo were from MYSQL i would have done this where easily by using mysql select query
mysql_query("SELECT * FROM mysql_table_a WHERE NOT EXISTS (SELECT * FROM mysql_table_B WHERE thisID != thatID)")
But here how do i check data exists in mysql table and list the data from odbc mssql database.
Please help me to resolve this problem.
Thank You..
You need to use mssql_, sqlsrv_ or PDO functions to connect to MSSQL. mysql_ functions are strictly for MySQL.
Microsoft SQL Server Driver for PHP
Microsoft SQL Server
Microsoft SQL Server Functions (PDO_SQLSRV)
You will need two separate connections if you are fetching data from both.
Alternatively, you can connect to MySQL from MSSQL using an ODBC connection and perform your query on the MSSQL side.
The other answers have told you how to connect the two databases but based on your comments you still want to know how to do the comparison.
I have to assume you've got at least one column which you can uniquely identify rows in table_a which are not in table_b. If you link mysql in MS SQL Server like one answer said you can
select * from mssql.table_a where unique_column not in (select unique_column from mysql.table_b)
but this works because mssql is linked to mysql in mssql i.e., you can do one query and mssql will do the join for you.
If for some reason you cannot or don't want to link mysql to ms sql via a linked table the easiest way would be if you had some sort of timestamp in table_a. Let's say every record written into table_a has a timestamp of when it was inserted:
mysql connection does select max(timestamp) into X from mysql.table_b
mssql connection does select * from mssql.table_a where timestamp > X
There may be other ways if we knew a bit more about your data.
I only have MySQL installed right now but will these work fine when run in PgSQL, MS SQL, etc.?
SELECT MAX(field) as max_field FROM table
SELECT MIN(field) as max_field FROM table
SELECT SUM(field) as max_field FROM table
You could check for yourself, but aggregate functions are common across most, if not all, RDBMS:
H2 aggregate functions
Oracle aggregate functions
PostgreSQL aggregate functions
SQL Server aggregate functions
You get the idea.
Yes, they work well in every database.
SELECT AVG(field) as max_field FROM table -- Work fine too
All *SQL RDBMSes are designed to be used with the SQL language. ALL of them. That's why they're called *SQL RDBMSes. Aggregate functions are part of standard SQL, and therefore usable wherever SQL is usable.
how can i run a query that joins two tables from TWO different Databases in mssql_query or mysql_query in php
for example
$conn=mssql_connect($ip,$username,$password);
mssql_select_db("DB1",$conn);
$q="select A.name,B.ID from DB1.dbo.T1 A, DB2.dbo.T2 B where A.ID=B.ID";
$res=mssql_query($q);
how to run such query??
Just prefix the tablenames with the database name, as you are already doing.
The user login that you are using to connect to mySQL needs to have access to both databases. Without this, it is impossible.
I think something like this:
SELECT X.field1, Y.field2
FROM database1.table_a AS X
INNER JOIN database2.table_b as Y
ON X.id=Y.id
[EDITED]
Sorry I didn't finish the post, you should use mysqli http://www.php.net/manual/en/mysqli.query.php (don't worry for the constructor, put just 1 database) and run the query as a regular query. Also, like the guy in the top said, the user that makes the query must have the permissions for both tables.
How do I get the last insert id from a database using a ODBC connection?
I'm looking for a solution similar to the mysql_insert_id() function.
SELECT ##IDENTITY AS ID
If you're using databases with PHP I strongly recommend using PDO (simple database wrapper for a lot of common database engines, more and more supported all the time, part of PHP canon), and hence use PDO::lastInsertId if your database supports the equivalent of mysql_insert_id.
Don't use "SELECT max(id) FROM table;" as it can result in seriously freaky and hard-to-find bugs later on.
* **UPDATE : Ok, you're using ODBC, and I suspect you're after odbc_cursor. I still stand by the strong recomendation to use PDO, as it has an ODBC driver. (ODBC in my eyes is an grumpy bitter old man who mumbles under his breath driving his truck that's falling apart, as the hip and effective PDO guys race past in their sexy VOLVO S90's)
It depends on the database type, but look into the SEQUENCE syntax for your rdbm.
I used "SELECT ##IDENTITY AS LastID", while working with PHP/MSSQL, through ODBC. That brought some issues under SQL 2005 server (or was it 2000?).
Either way if you do like this:
SET NOCOUNT ON
[NOW INSERT YOUR INSERT SQL QUERY]
SELECT ##IDENTITY AS LastID
you should be just fine in any MSSQL server version.
"SET NOCOUNT ON" will prevent sql server from sending messages like '1 rows inserted', which will keep your script working properly.
If you have MySQL under ODBC - you can use the next query:
"SELECT LAST_INSERT_ID( );"
It have to be executed mmediately after executing INSERT-query.
For other database - use other specific queries...
Use in MySQL
SELECT *
FROM table_name
ORDER BY Id Desc
LIMIT 1
Use in SQL Server
SELECT top 1 *
FROM table_name
ORDER BY Id Desc
Are you inserting rows into your database via PHP? If so, perhaps you can generate a unique primary key using uniqid() - then you will know the ID without having to query the database.
If it is not possible to update the key type, perhaps you can still insert a unique id when you do the inserts, so you could do a query like this:
SELECT id FROM mytable WHERE rowid = '$myuniqueid'
That way you're ensuring that you're pulling back the correct ID from the database - a much better solution than MAX(id).
Using
SELECT max(id) FROM table;
should be fine from inside a transaction, or does ODBC not support transactions?