简体   繁体   中英

forging a sql statement that crosses db servers for use in a perl script using DBI

perl 5.10 Access 2010 SqlServer 2008 R2

So I need to update a column in table A with data in table B where A and B have a column I can JOIN on.

This would work great

$sqlCmd = "UPDATE aa SET aa.foo = bb.fancyfoo " .
          "FROM [dbo.serverOne] AS aa " .
          "RIGHT JOIN [noteTable] AS bb " .
          "ON aa.[recid] = bb.[recid] " ;
$sth = $dbh->prepare( $sqlCmd);

IF both tables were in the same database since there's only one database handle in play. But my tables reside on different databases , and in fact in different servers -- dbo.ServerOne lives in an instance of SqlServer while noteTable resides in an Access database ( sorry ).

And for extra added spice, bb.fancyfoo is defined as a MEMO and aa.foo is defined as a nvarchar(max)

Frankly I can't see how this can be achieved in one pass - can a sql command make use of more than one db handle? If not , and I have to use two separate commands ie an UPDATE on dbo.ServerOne and SELECT on noteTable, how do I set this up to work for MEMO/nvarchar(max) fields? I mean, how should I store the data while its in beteween tables? CLOB?

TIA,

Still-learning Steve

Your luck is in - both tables can be in the same database.

What you need to google for is "linked tables". There is a brief overview here but basically it lets you add an external table via ODBC (or other supported connection method).

You can either link the Access table in to the SQL Server DB or the other way around. Which will depend on whether the Access file is in a fixed location and where the majority of your data is. It's probably more efficient to link to the smaller from the larger.

Having said that, I'd expect SQL Server to be smarter about planning/executing the query so I'd try that first.

Since we're already schlepping data out the the Access tables into SqlServer tables I decided to do the JOIN via adding fields from both tables to a hashref keyed on the shared key field. Not as elegant as I'd like, but it works.

Thanks to all who replied!

CASE CLOSED

Still-learning Steve

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