Adición de una base de datos de Visual FoxPro a SQL Server como servidor vinculado
En este artículo se presenta cómo agregar y consultar mediante programación un origen de datos de Visual FoxPro como servidor vinculado de Visual FoxPro.
Versión original del producto: Visual FoxPro
Número de KB original: 199131
Resumen
SQL Server 7.0 y SQL Server 2000 permite agregar orígenes de datos externos como servidores vinculados. Esta característica proporciona acceso para distribuir consultas heterogéneas en orígenes de datos OLE DB. En este artículo se muestra cómo agregar y consultar mediante programación un origen de datos de Visual FoxPro como servidor vinculado desde Visual FoxPro.
Más información
sp_addlinkedserver
es un nuevo procedimiento almacenado introducido en SQL Server 7.0 y SQL Server 2000. sp_addlinkedserver
crea un servidor vinculado, que permite el acceso para distribuir consultas heterogéneas en orígenes de datos OLE DB.
La sintaxis para agregar un servidor vinculado desde Transact - SQL es:
sp_addlinkedserver [@server =] 'server',
[@srvproduct =] 'product_name',
[@provider =] 'provider_name',
[@datasrc =] 'data_source',
[@location =] 'location',
[@provstr =] 'provider_string',
[@catalog =] 'catalog'[@server =] 'server' Is the name of the linked server to
create with sp_addlinkedserver.
[@srvproduct =] 'product_name' Is the product name of the OLE DB data
source to add as a linked server.
[@provider =] 'provider_name' Is the unique provider identifier of the
OLE DB provider corresponding to the
data source.
[@datasrc =] 'data_source' Is the name of the data source, as
interpreted by the OLE DB provider.
[@location =] 'location' Is the location of or path to the
database as interpreted by the OLE DB
provider.
[@provstr =] 'provider_string' Is the OLE DB provider-specific.
[@catalog =] 'catalog' Is the catalog to be used when making a
connection to the OLE DB provider.
El siguiente fragmento de código agrega la base de datos Testdata.DBC
de Visual FoxPro desde el directorio SAMPLES\DATA a SQL Server como servidor vinculado. En este fragmento de código se supone que Visual FoxPro y SQL Server se ejecutan en la misma máquina.
Nota:
El usuario, Username, debe tener permisos para realizar estas operaciones en la base de datos.
Source_Path=IIF(VAL(SUBSTR(VERSION(),15,2))=6,HOME(2),HOME()+"SAMPLES\")
Connect_String='DRIVER={SQL Server};' + ;
'SERVER=MY_SERVER;DATABASE=PUBS;UID=UserName;PWD=StrongPassword'
gnConnHandle=SQLSTRINGCONN(Connect_String)
IF gnConnHandle > 0
* Create a command string to pass to SQL Server via SQLExec
SQLCommand="sp_addlinkedserver 'VFP','','MSDASQL',NULL,NULL,"+ ;
"'DRIVER={Microsoft Visual FoxPro Driver};" + ;
"SourceDB="+Source_Path+"DATA\TESTDATA.DBC;SourceType=DBC;NULL'"* CREATE the LINKED Server"
Create_Linked_Server=SQLExec(gnConnHandle,SQLCommand)
IF Create_Linked_Server > 0
* The linked server was successfully created
* Run the query
=RunQuery()
ELSE
* The Linked Server either already exists or the command failed.
* Test for existence of linked server with aerror()
=AERROR(s_failed)
IF "VFP' ALREADY EXISTS."$UPPER(s_failed[1,2])
* The linked server exists, so run the query
=RunQuery()
ELSE
* The linked server doesn't exist, so display a message
=MESSAGEBOX(s_failed[1,2],32,'Failed')
ENDIF
ENDIF
=SQLDISCONN(gnConnHandle)
ENDIF
PROCEDURE RunQuery
SQLCommand="SELECT * FROM OPENQUERY(VFP,'SELECT * FROM CUSTOMER')"
QRYVal=SQLExec(gnConnHandle,SQLCommand,'SQLRESULTS')
IF QRYVal > 0
SELECT SQLResults
BROW
ELSE
=AERROR(L_Server)
=MESSAGEBOX(L_Server[1,2],32,'Query Failed')
ENDIF
RETURN
Después de ejecutar el fragmento de código, abra SQL Server Enterprise Manager y expanda el nodo Servidores vinculados. Se ha agregado un servidor vinculado denominado VFP.
Referencias
Para obtener más información sobre sp_addlinkedserver
, busque sp_addlinkedserver
en el archivo de Ayuda de referencia de Transact - SQL.