Question

I'm trying to do an UPDATE action to a remote table from a local query. Obviously i know it should be with an OPENROWSET but i don't know how to do it.

I know you can Insert to an OPENROWSET doing something like this:

 set @cadsql = convert(varchar(max),
  'insert into openrowset(''SQLOLEDB'','''+@cadcon+''','+@bd+'.ctoxtractor.lm_vehiculos) ' + char(13) + 
  '           (cuenta,corporativo,economico,ctatipotractor,sctatipotractor,ultcambio,ciausu,usuario,estatusoper) ' + char(13) + 
  'select 423,fv.vehic,fv.numeco,9802,0,getdate(),0,''ADMIN'',fv.estatusoper ' + char(13) + 
  'from ficvehic fv ' + char(13) + 
  'order by fv.vehic ')

 exec (@cadsql) 

Or that i can do a select like this:

SET @cadsql = 'SELECT * FROM OPENROWSET(''sqloledb'',''driver=sql server;server='+@server+';database='+ @database +';uid='+@user+';pwd='+@password+''',' +
'''SELECT Column1, column2,...  
   FROM table'')'

INSERT INTO another_table
exec (@cadsql)

But i havn't come up with a solution for updating. Is that possible?

Thanks in advance!

Was it helpful?

Solution

If I understand your intention correctly, this should get you going:

UPDATE target
SET
  target.column = query.value,
  ...
FROM OPENROWSET(
  'provider',
  'connection string',
  'SELECT columns FROM yourtable'
) AS target
INNER JOIN (
  your local query
) AS query
ON
  target.column = query.column
  AND ...
;

Basically, this is same as you would go about updating a local table from a query, just using OPENROWSET instead of a table name in the FROM clause and specifying its alias in the UPDATE clause.

Of course, it is also important that the remote query should be updatable (e.g. it should return results from a single table), same as with its being insertable when you are using OPENROWSET to insert.

OTHER TIPS

This is the syntax:

OPENROWSET 
( { 'provider_name' , { 'datasource' ; 'user_id' ; 'password' 
   | 'provider_string' } 
   , {   [ catalog. ] [ schema. ] object 
       | 'query' 
     } 
   | BULK 'data_file' , 
       { FORMATFILE = 'format_file_path' [ <bulk_options> ]
       | SINGLE_BLOB | SINGLE_CLOB | SINGLE_NCLOB }
} ) 

<bulk_options> ::=
   [ , CODEPAGE = { 'ACP' | 'OEM' | 'RAW' | 'code_page' } ] 
   [ , ERRORFILE = 'file_name' ]
   [ , FIRSTROW = first_row ] 
   [ , LASTROW = last_row ] 
   [ , MAXERRORS = maximum_errors ] 
   [ , ROWS_PER_BATCH = rows_per_batch ]
   [ , ORDER ( { column [ ASC | DESC ] } [ ,...n ] ) [ UNIQUE ]

And here you could get some more info

http://technet.microsoft.com/es-es/library/ms190312.aspx

A.F.A.I.K. The OPENROWSET function can also be referenced as the target table of an INSERT, UPDATE, or DELETE statement, subject to the capabilities of the OLE DB provider. Although the query may return multiple result sets, OPENROWSET returns only the first one.

OPENROWSET can be used to access remote data from OLE DB data sources only if the DisallowAdhocAccess registry option is explicitly set to 0. When this option is not set, the default behavior does not allow ad hoc access.

OPENROWSET does not accept variables for its arguments.

Syntax of OPENROWSET Function :

OPENROWSET ( 'provider_name' 
    , { 'datasource' ; 'user_id' ; 'password'
        | 'provider_string' } 
    , { [ catalog. ] [ schema. ] object
        | 'query' }   )

provider_name is a character string that represents the friendly name of the OLE DB provider as specified in the registry. It has no default value.

datasource is a string constant that corresponds to a particular OLE DB data source.

userid is a string constant that is the username passed to the specified OLE DB provider.

password is a provider-specific connection string that is passed in as the DBPROP_INIT_PROVIDERSTRING property to initialize the OLE DB provider.

catalog is a name of the catalog or database in which the specified object resides.

schema is a name of the schema or object owner for the specified object.

object is a object name that uniquely identifies the object to manipulate.

query is a string constant sent to and executed by the provider.

Updated.. Have a look here (final post one page).. I don't know if this works or not, but seems plausible..

http://www.sql-server-performance.com/forum/threads/how-to-update-using-openrowset.10275/

I will see if I can get this working today while I am work.. I have a couple test dbs that I can play with..

update t set t.col_Name='1' FROM OPENROWSET('SQLOLEDB','ServerName';'UserName';'Pwd','select * from TableName') t

HTH,

Kent

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top