OpenQuery error with Oracle

I've got a 2008 SQL Server linked to Oracle 10 server and when writing straight queries with OpenQuery everything works fine.  However I need to create proc with parameters as I will have to perform several additional steps after I get this data.

I'm getting an error that makes no sense to me, so I need an extra set of eyes to help.  I've had the code print off the dynamic SQL and it runs just fine in Oracle, but when I wrap it in the OpenQuery It fails with an incorrect syntax error at a spot that just can't be the issue.  The error is in the WHERE clause.  Message is Incorrect syntax near 'INCIDENT'.

Anybody got an idea about what's going on?




SELECT * FROM OPENQUERY(MAXPROD,'
   SELECT  DISTINCT
     INC.TICKETID,
     INC.SEVERITY,
     INC.VENDOR,
     INC.VENDORTKTNO
   FROM 
     INCIDENT INC
     LEFT OUTER JOIN (
       SELECT DISTINCT
	TICKETID,	
	MIN(CHANGEDATE) AS CHANGEDATE 		 
       FROM TKSTATUS 
       WHERE 
	TKSTATUS.CLASS = 'INCIDENT'
       AND TKSTATUS.STATUS IN ('RESOLVED','CAN','CLOSED')
       GROUP BY TICKETID) STATUSCHANGEDT 
       ON INC.TICKETID = STATUSCHANGEDT.TICKETID         
     LEFT OUTER JOIN WOCHANGE WC
       ON INC.TICKETID = WC.ORGID 	   
   WHERE 
       INC.VENDOR IS NOT NULL	
   AND INC.CREATIONDATE 
       BETWEEN to_date'6/24/2010','mm/dd/yyyy') 
       AND to_date('7/15/2010','mm/dd/yyyy'))

Msg 102, Level 15, State 1, Line 13
Incorrect syntax near 'INCIDENT'.

Open in new window

LVL 5
BPebAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

mcv22Commented:
Quotes used for strings need to be escaped:

Use ''INCIDENT'' ... ''RESOLVED'' etc
0
mcv22Commented:
Something like :


SELECT * FROM OPENQUERY(MAXPROD,'
   SELECT  DISTINCT
     INC.TICKETID,
     INC.SEVERITY,
     INC.VENDOR,
     INC.VENDORTKTNO
   FROM 
     INCIDENT INC
     LEFT OUTER JOIN (
       SELECT DISTINCT
	TICKETID,	
	MIN(CHANGEDATE) AS CHANGEDATE 		 
       FROM TKSTATUS 
       WHERE 
	TKSTATUS.CLASS = ''INCIDENT''
       AND TKSTATUS.STATUS IN (''RESOLVED'',''CAN'',''CLOSED'')
       GROUP BY TICKETID) STATUSCHANGEDT 
       ON INC.TICKETID = STATUSCHANGEDT.TICKETID         
     LEFT OUTER JOIN WOCHANGE WC
       ON INC.TICKETID = WC.ORGID 	   
   WHERE 
       INC.VENDOR IS NOT NULL	
   AND INC.CREATIONDATE 
       BETWEEN to_date(''6/24/2010'',''mm/dd/yyyy'') 
       AND to_date(''7/15/2010'',''mm/dd/yyyy'')')

Open in new window

0
cyberkiwiCommented:
double up the single-quotes
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
The Five Tenets of the Most Secure Backup

Data loss can hit a business in any number of ways. In reality, companies should expect to lose data at some point. The challenge is having a plan to recover from such an event.

vdr1620Commented:
your missing an open braces near BETWEEN

BETWEEN to_date('6/24/2010','mm/dd/yyyy')
0
BPebAuthor Commented:
vdr1620 - I think you may have it. I'll take a look in the morning.  The code I included was the output of the dynamic SQL, so the others that commented about the double quotes missed that small point, but I thank you all for helping.
0
cyberkiwiCommented:
I admit didn't get that far - the error was complaining about "near INCIDENT", so I noticed all the single single-qoutes.  mcv22 didn't miss it though, he fixed the entire query.
0
BPebAuthor Commented:
The problem was that the OpenQuery treats the SQL as if it were dynamic and thus requires the double quotes for processing.  Thanks to all for your help.  BTW - The missing bracket turns out to be from editing the code to look right on EE.  
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft SQL Server 2008

From novice to tech pro — start learning today.