ASH
ASH

Reputation: 20302

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!

Upvotes: 0

Views: 41

Answers (1)

Mikhail Lobanov
Mikhail Lobanov

Reputation: 3026

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

Try this:

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

Upvotes: 1

Related Questions