• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 843
  • Last Modified:

Long query in VBscript issue

I've hit a road block with my vb script in regards to a long sql query.  When i execute my script, I'm receiving an error message from oracle, complaining about the syntax of the sql statement.  So, my guess is that the problem lies within the formatting of the query string in the script, but I can't find where.  Hopefully someone else can spot it.


Script:

HOST = "dbhost"
PORT = "1522"
SID = "TEST"
USER = "TESTUSER"
PASSWORD = "PASSWORD"

Srvname="Driver={Microsoft ODBC for Oracle}; " & _
"CONNECTSTRING=(DESCRIPTION=" & _
"(ADDRESS=(PROTOCOL=TCP)" & _
"(HOST="& HOST &")(PORT="& PORT &"))" & _
"(CONNECT_DATA=(SID="& SID &"))); uid="& USER &";pwd="& PASSWORD &";"

Set Rec = CreateObject("ADODB.Recordset")
Set conn=Createobject("ADODB.Connection") 
conn.open Srvname   


Set objFSO = CreateObject("Scripting.FileSystemObject")

strSQL = "select a.DataID from" &_
" (select LLAttrData.ID as DataID, DTree.ParentID, DTree.SubType, DTree.Name," &_
" LLAttrData.DefID, LLAttrData.ValStr as 'ASSET_ID'" &_
" from LLAttrData, DTree" &_
" where LLAttrData.DefID = 9477354" &_
" and LLAttrData.AttrID = 2" &_
" and LLAttrData.ID = DTree.DataID and LLAttrData.VerNum = DTree.VersionNum) a" &_
" inner join" &_
" (select LLAttrData.ID as DataID, DTree.ParentID, DTree.SubType, DTree.Name," &_
" LLAttrData.DefID, LLAttrData.ValStr as 'CONTRACTOR_DOCUMENT_NUMBER'" &_
" from LLAttrData, DTree" &_
" where LLAttrData.DefID = 9477354" &_
" and LLAttrData.AttrID = 6" &_
" and LLAttrData.ID = DTree.DataID and LLAttrData.VerNum = DTree.VersionNum) d" &_
" on a.DataID = d.DataID" &_
" where ('PLANT (ROF)' IS NULL OR UPPER(a.ASSET_ID)= UPPER('PLANT (ROF)'))" &_
" ('DH027602-000-SOW-0000-008' IS NULL OR UPPER(d.CONTRACTOR_DOCUMENT_NUMBER)= UPPER('DH027602-000-SOW-0000-008'))" &_
" order by a.DataID"

WScript.Echo(strSQL)

Rec.Open strSQL, conn
dataid=Rec.fields("DataID")
WScript.Echo(dataid)

Rec.Close
conn.Close

Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objTextFile = objFSO.OpenTextFile("D:\opt\Scripts\sourcefile.txt", 1)

do while not (objTextFile.AtEndOfStream)
  arrStr = Split(objTextFile.ReadLine, ",")
  WScript.Echo(arrStr(3))
Loop

objTextFile.close

Open in new window


Output: (sorry for the word wrap from cmd prompt)

C:\Documents and Settings\svc_dev>cscript "D:\opt\Scripts\getDataIDs.vbs"
Microsoft (R) Windows Script Host Version 5.6
Copyright (C) Microsoft Corporation 1996-2001. All rights reserved.

select a.DataID from (select LLAttrData.ID as DataID, DTree.ParentID, DTree.SubType, DTree.Name, LLAttrData.DefID, LLAttrData.Va
lStr as 'ASSET_ID' from LLAttrData, DTree where LLAttrData.DefID = 9477354 and LLAttrData.AttrID = 2 and LLAttrData.ID = DTree.D
ataID and LLAttrData.VerNum = DTree.VersionNum) a inner join (select LLAttrData.ID as DataID, DTree.ParentID, DTree.SubType, DTr
ee.Name, LLAttrData.DefID, LLAttrData.ValStr as 'CONTRACTOR_DOCUMENT_NUMBER' from LLAttrData, DTree where LLAttrData.DefID = 947
7354 and LLAttrData.AttrID = 6 and LLAttrData.ID = DTree.DataID and LLAttrData.VerNum = DTree.VersionNum) d on a.DataID = d.Data
ID where ('PLANT (ROF)' IS NULL OR UPPER(a.ASSET_ID)= UPPER('PLANT (ROF)')) ('DH027602-000-SOW-0000-008' IS NULL OR UPPER(d.CONTRACTOR_DOCUMENT_NUMBER)= UPPER('DH027602-000-SOW-0000-00
8')) order by a.DataID
D:\opt\Scripts\getDataIDs.vbs(41, 1) Microsoft OLE DB Provider for ODBC Drivers: [Microsoft][ODBC driver for Oracle][Oracle]ORA-
00923: FROM keyword not found where expected

Open in new window

0
bmsande
Asked:
bmsande
  • 5
  • 2
2 Solutions
 
chaauCommented:
In your last WHERE clause there are two conditions in parenthesis. There is no OR/AND operator between them:
where ('PLANT (ROF)' IS NULL OR UPPER(a.ASSET_ID)= UPPER('PLANT (ROF)')) -- there is nothing here: insert OR/AND depending on your requirement
('DH027602-000-SOW-0000-008' IS NULL OR UPPER(d.CONTRACTOR_DOCUMENT_NUMBER)= UPPER('DH027602-000-SOW-0000-008')) 

Open in new window


BTW, if you copy your statement to a editor with a syntax highlighting (such as an open source editor called Notepad++), and select "SQL" as a language, you will see everything highlighted nicely. With Notepad++ you will also see the matching closing bracket:
Notepad   query
0
 
bmsandeAuthor Commented:
I had my hopes up that the missing expression would fix it, wrong.... same error.
0
 
bmsandeAuthor Commented:
found it.  lines 22 & 29 had single quotes.  I removed them and the query worked.
0
Cloud Class® Course: Certified Penetration Testing

This CPTE Certified Penetration Testing Engineer course covers everything you need to know about becoming a Certified Penetration Testing Engineer. Career Path: Professional roles include Ethical Hackers, Security Consultants, System Administrators, and Chief Security Officers.

 
Mark GeerlingsDatabase AdministratorCommented:
I'm glad that you found the error.  I'm concerned by a few other things I see in your query though:

1. where [some thing] is null
2. and UPPER(a.ASSET_ID) = [a value]
3. UPPER(d.CONTRACTOR_DOCUMENT_NUMBER) = [a value]

All of those conditions can impose huge performance penalties on Oracle queries! That is especially true if the "is null" condition applies to a database column or if either the ASSET_ID or the CONTRACTOR_DOCUMENT_NUMBER columns are indexed.

Why?  Applying any SQL operators (like: nvl, upper, lower, substr, to_char, to_date, to_number, etc.) to any column value prevents Oracle from using an index on that column (unless your database is set up to support "function-based" indexes *AND* you have index(es) that exactly match this query syntax).  Depending on the numbers of records in the table, and on the speed and capacity of your server and storage system, the performance impact of queries like this can be significant.

Using these operators on your query bind variables, like this:
UPPER('DH027602-000-SOW-0000-008'
is *NOT* a problem.
0
 
bmsandeAuthor Commented:
this query is used on demand to fetch a unique ID based on a USER PROVIDED list of attributes.  So, in some cases a field is left blank, and CASE does not match up.  The query is meant to be flexible in these cases.  I'm sure performance could be improved, but i'm not an oracle guy, and this works for my need.
0
 
Mark GeerlingsDatabase AdministratorCommented:
"The query is meant to be flexible...".  Yes, I understand that.  Unfortunately, in SQL queries (and in other things too at times) "flexible" and "fast" do not go together.  Usually in SQL, the most-flexible queries perform the slowest.  So, if the users or application demand flexibility, SQL statements can be written to be flexible.  The users should then not expect good performance.

If good performance is an issue, often some flexibility needs to be sacrificed.
0
 
bmsandeAuthor Commented:
Users won't be interacting with this query at all.  This is strictly used for bulk attribute updates, performed by me, on demand--maybe once a quarter.  I appreciate your concern ;)
0
 
bmsandeAuthor Commented:
My comment provided the final solution to my problem.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Cloud Class® Course: Amazon Web Services - Basic

Are you thinking about creating an Amazon Web Services account for your business? Not sure where to start? In this course you’ll get an overview of the history of AWS and take a tour of their user interface.

  • 5
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now