简体   繁体   中英

Trying to Import data into SQL Server from Access

I tried this small script below.

USE [Test]
GO
/****** Object:  StoredProcedure [dbo].[Import_From_Access]    Script Date: 4/16/2017 5:13:19 PM ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO

ALTER PROCEDURE [dbo].[Import_From_Access]
AS
BEGIN


SET NOCOUNT ON;

EXEC sp_addlinkedserver
@server = 'EXCEL-PC\SQLEXPRESS',
@provider = 'Microsoft.Jet.OLEDB.4.0',
@srvproduct = 'OLE DB Provider for Jet',
@datasrc = 'C:\Users\Excel\Desktop\Coding\Microsoft Access\Northwind.mdb'

EXEC sp_addlinkedsrvlogin 'EXCEL-PC\SQLEXPRESS', FALSE, Null, Admin, Null

END

Then, to run it, i tried this:

SELECT *
FROM OPENQUERY('EXCEL-PC\SQLEXPRESS', 'SELECT * FROM Table1')

Now, I thought that would work, but I'm getting an error message that reads: Msg 102, Level 15, State 1, Line 6 Incorrect syntax near 'EXCEL-PC\\SQLEXPRESS'.

I am thinking that the issue may be the '-' character or the '\\' character. unfortunately, that is the name of the server machine, and it's probably not going to change. If there a work-around for this kind of thing, or am I just out of luck with this kind of setup?

Thanks to all!

EXCEL-PC\\SQLEXPRESS isn't a string literal:

Try this:

SELECT *
FROM OPENQUERY([EXCEL-PC\SQLEXPRESS], 'SELECT * FROM Table1')

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