简体   繁体   中英

Insert/update data from access database to sql server database

I have a ms access database file and a sql server database. I need to write something in php in order for me to be able to update sql database with access database data. Update means to update existing entries and insert new ones.

Script to connect and fetch from access db file:

<?php
$connStr = 'odbc:Driver={Microsoft Access Driver (*.mdb, *.accdb)};' .'Dbq=C:\\myfile.accdb;';
$dbh = new PDO($connStr);
$dbh->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);

$sql = "SELECT ID, `LAST NAME` AS lm, `FIRST NAME` AS fm,  FROM OLD";
$sql .= " UNION ALL SELECT ID, `Last Name` AS lm, `First Name` AS fm,  FROM NEW) ";
$sql .= " ORDER BY lm";

$sth = $dbh->prepare($sql);
$params = array();
$sth->execute($params);

while ($row = $sth->fetch()) {
//do something with $row['lm'] and $row['fm']
}
?>

Script to connect and fetch from sql db file:

<?php
$sql = "SELECT * FROM MYTABLE";
$stmt = sqlsrv_query($conn, $sql);
if($stmt === false) {
    echo 'Error. Could not execute query!';  
}

while ($rowsec = sqlsrv_fetch_array($stmt, SQLSRV_FETCH_ASSOC)) {
     //do something with $rowsec['MYTABLE_COLUMN']
?>

So, how could i combine those 2 queries in order to get data from access file and insert/update records from sql table?

Assume that when i read data from access file before updating/inserting to sql table i need to check a column from access file and modify this like this:

<?php 
if($row['fm'].indexOf('am') > -1) {$id = 'am'.$row['ID']}
?> 

and then i should use $id along $row['lm'] and $row['fm'] values to insert/update to sql database.

Thank you in advance

As mentioned, consider SQL Server's OPENROWSET if user is allowed ad-hoc distributed query privileges and avoid any looping between resultsets using the same ODBC connection driver:

INSERT QUERY

INSERT INTO MYTABLE (ID, LM, FM)
SELECT t.*
FROM (SELECT ID, [LAST NAME] AS lm, [FIRST NAME] AS fm  
      FROM OPENROWSET('MSDASQL',
                      'Driver={Microsoft Access Driver (*.mdb, *.accdb)};
                       DBQ=C:\\myfile.accdb', 'SELECT * FROM [OLD]')

      UNION ALL 

      SELECT ID, [LAST NAME] AS lm, [FIRST NAME] AS fm  
      FROM OPENROWSET('MSDASQL',
                      'Driver={Microsoft Access Driver (*.mdb, *.accdb)};
                       DBQ=C:\\myfile.accdb', 'SELECT * FROM [New]')
     ) t
WHERE NOT EXISTS (SELECT 1 FROM MYTABLE s WHERE s.ID = t.ID)
ORDER BY t.LM

UPDATE QUERY

UPDATE t
SET t.Lm = n.lm, t.Fm = n.fm
FROM MYTABLE t
INNER JOIN 
    (SELECT ID, [LAST NAME] AS lm, [FIRST NAME] AS fm  
     FROM OPENROWSET('MSDASQL',
                     'Driver={Microsoft Access Driver (*.mdb, *.accdb)};
                      DBQ=C:\\myfile.accdb', 'SELECT * FROM [OLD]')

     UNION ALL 

     SELECT ID, [LAST NAME] AS lm, [FIRST NAME] AS fm  
     FROM OPENROWSET('MSDASQL',
                     'Driver={Microsoft Access Driver (*.mdb, *.accdb)};
                      DBQ=C:\\myfile.accdb', 'SELECT * FROM [New]')
    ) n
ON t.ID = n.ID

Alternatively, use the ACE OLEDB Provider:

FROM OPENROWSET('Microsoft.ACE.OLEDB.12.0',   
   'C:\\myfile.accdb';'admin';'', Old)

Be sure to double up single quotes (similar to doubling up \\ ) inside the PHP query string.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM