Solved

Need help with the proper SQL syntax when querying a linked Server

Posted on 2016-09-13
1
43 Views
Last Modified: 2016-09-13
I am currently using this statement to query a linked Server:
INSERT INTO WOMAST01
SELECT * FROM OPENQUERY(ATQAV, 'SELECT * FROM WOMAST01')

Open in new window

and it works fine.

I want to add a WHERE clause to filter by date using the RECDATE field. I try
INSERT INTO WOMAST01
SELECT * FROM OPENQUERY(ATQAV, 'SELECT * FROM WOMAST01 WHERE RECDATE > '20140101'')

Open in new window


but it is not working, error received:  Msg 102, Level 15, State 1, Line 2
Incorrect syntax near '20140101'.

What is the correct syntax?
0
Comment
Question by:maximus1974
1 Comment
 
LVL 20

Accepted Solution

by:
Russ Suter earned 500 total points
ID: 41796242
You're not properly escaping the single quotes. It should look like this:
SELECT * FROM OPENQUERY(ATQAV, 'SELECT * FROM WOMAST01 WHERE RECDATE > ''20140101''')

Open in new window

Quick explanation...
Single quotes inside a quoted string are represented by a double single quote (''). NOTE: Not a double quote (") which looks almost identical in most fonts.

So... if you need quoted text inside quoted text it would be like this:
'The quick brown ''fox'' jumps over a lazy dog'
 -- OR --
'The quick brown fox jumps over a lazy ''dog'''

In your case the quoted text is at the end of the quoted text (as in my 2nd example) so you end up with 3 single quotes in a row. It looks a little odd at first but once you parse it it begins to make sense.
0

Featured Post

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

In this article I will describe the Detach & Attach method as one possible migration process and I will add the extra tasks needed for an upgrade when and where is applied so it will cover all.
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
Familiarize people with the process of utilizing SQL Server functions from within Microsoft Access. Microsoft Access is a very powerful client/server development tool. One of the SQL Server objects that you can interact with from within Microsoft Ac…
Viewers will learn how to use the SELECT statement in SQL to return specific rows and columns, with various degrees of sorting and limits in place.

707 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now