[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

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

Posted on 2016-09-13
1
Medium Priority
?
69 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 2000 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

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

A Stored Procedure in Microsoft SQL Server is a powerful feature that it can be used to execute the Data Manipulation Language (DML) or Data Definition Language (DDL). Depending on business requirements, a single Stored Procedure can return differe…
Sometimes MS breaks things just for fun... In Access 2003, only the maximum allowable SQL string length could cause problems as you built a recordset. Now, when using string data in a WHERE clause, the 'identifier' maximum is 128 characters. So, …
This video shows how to set up a shell script to accept a positional parameter when called, pass that to a SQL script, accept the output from the statement back and then manipulate it in the Shell.
Via a live example, show how to backup a database, simulate a failure backup the tail of the database transaction log and perform the restore.
Suggested Courses

834 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