简体   繁体   中英

SQL statement against Access 2010 DB not working with ODBC

I'm attempting to run a simple statement against an Access DB to find records.

Data validation in the records was horrible, and I cannot sanitize it. Meaning, it must be preserved as is.

I need to be able to search against a string with white space and hyphen characters removed. The following statement will work in Access 2010 direct:

select * from dummy where Replace(Replace([data1],' ',''),'-','') = 'ABCD1234';

Running it from an ODBC connection via PHP will not. It produces the following error:

SQL error: [Microsoft][ODBC Microsoft Access Driver] Undefined function 'Replace' in expression., SQL state 37000 in SQLExecDirect

Creating a query in the database that runs the function and attempting to search its values indirectly causes the same error:

select * from dummy_indirect where Expr1 = 'ABCD1234';

I've attempted to use both ODBC drivers present. ODBCJR32.dll (03/22/2010) and ACEODBC.dll (02/18/2007). To my knowledge these should be current as it was installed with the full Access 2010 and Access 2010 Database Engine.

Any ideas on how to work around this error and achieve the same effect are welcome. Please note, that I cannot alter the database in way, shape, or form. That indirect query was created in another mdb file that has the original tables linked from the original DB.

* Update *

OleDB did not really affect anything.

$dsn= "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=c:\dummy.mdb;";

I'm not attempting to use it as a web backend either. I'm not a sadomasochist.

There is a legacy system that I must support that does use Access as a backend. Data gets populated there from other old systems that I must integrate into more modern systems. Hence, the creation of an API with Apache/PHP that is running on the server supporting the legacy system.

I need to be able to search a table that has an alphanumeric case identifier to get a numeric identifier that is unique and tied to a generator (Autonumber in access). Users have been using it a trash box for years (inconsistent data entry with sporadic notations) so the only solution I have is to strip everything except alphanumeric out of both the field value and the search value and attempt to perform a LIKE comparison against it.

If not replace() which is access supported, what ODBC compatible functions exist that I can use do the same kind of comparison?

Just to recap, the Access db engine will not recognize the Replace() function unless your query is run from within an Access application session. Any attempt from outside Access will trigger that "Undefined function" error message. You can't avoid the error by switching from ODBC to OleDb as the connection method. And you also can't trick the engine into using Replace() by hiding it in separate query (in the same or another Access db) and using that query as the data source for your main query.

This behavior is determined by Access' sandbox mode . That linked page includes a list of functions which are available in the default sandbox mode. That page also describes how you can alter the sandbox mode. If you absolutely must have Replace() available for your query, perhaps the lowest setting (0) would allow it. However, I'm not recommending you do that. I've never done it myself, so don't know anything about the consequences.

As for alternatives for Replace() , it would help to know about the variability in the values you're searching. If the space or dash characters appear in only one or a few consistent positions, you could do a pattern match with a Like expression. For example, if the search field values consist of 4 letters, an optional space or dash, followed by 4 digits, a WHERE clause like this should work for the variations of "ABCD1234":

SELECT * FROM dummy
WHERE
       data1 = 'ABCD1234'
    OR data1 Like 'ABCD[- ]1234';

Another possibility is to compare against a list of values:

SELECT * FROM dummy
WHERE
       data1 IN ('ABCD1234','ABCD 1234','ABCD-1234');

However if your search field values can include any number of spaces or dashes at any position within the string, that approach is no good. And I would look real hard for some way to make the query task easier:

  1. You can't clean the stored values because you're prohibited from altering the original Access db in any way. Perhaps you could create a new Access db, import the data, and clean that instead.
  2. Set up the original Access db as a linked server in SQL Server and build your query to take advantage of SQL Server features.
  3. Surrender. :-( Pull in a larger data set to your PHP client code, and evaluate which rows to use vs. which to ignore.

I'm not sure you can do this with ODBC and your constraints. The MS Access driver is limited (by design; MS wants you to use SQL Server for back ends).

Can you use OLEDB? that might be an option.

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