Solved

How to query a table with a hyphen in the name?

Posted on 2010-09-17
12
1,813 Views
Last Modified: 2013-11-05
I have a database that has a table with a hyphen in the name (don't look at me, I didn't do it!).   Let's call the table E-Mail_Addresses.   I need to query that table.  The database is Intersystems Cache, but it accepts standard SQL queries.

I need something that looks like

SELECT * FROM [E-Mail_Addresses] WHERE Name='JohnDoe'

Except that I need it to work.  If it matters to anyone, I'm coding in VBS using ADODB and the InterSystems 5.1 ODBC driver.


Thanks!!
0
Comment
Question by:exx1976
12 Comments
 
LVL 92

Expert Comment

by:Patrick Matthews
ID: 33704158
Are you saying that that does not work?  Generally, you would encase "non-standard" database object names in square brackets...
0
 
LVL 15

Expert Comment

by:AmmarR
ID: 33704173
hi

if you have a hyphen in the data then you need to double it in your script

check code below

SELECT * FROM [E-Mail_Addresses] WHERE Name='''JohnDoe'''

Open in new window

0
 
LVL 8

Expert Comment

by:mustaccio
ID: 33704277
Try enclosing the table name in double quotes (this is standard SQL):

SELECT * FROM "E-Mail_Addresses" WHERE Name='JohnDoe'

Note that this makes the table name case-sensitive; if its name is in fact "e-mail_addresses" or "E-MAIL_ADDRESSES" it won't match.
0
NAS Cloud Backup Strategies

This article explains backup scenarios when using network storage. We review the so-called “3-2-1 strategy” and summarize the methods you can use to send NAS data to the cloud

 
LVL 18

Author Comment

by:exx1976
ID: 33704285
Below is the code (VBS) that I'm using.

This is the error that I get:

[SQLCODE: <-1>:<Invalid SQL statement>]
[Cache Error: <<SYNTAX>errdone+1^%qaqqt>]
[Details: <Prepare>]
[%msg: < SQL ERROR #1: `ID' expected, `[' found^SELECT * FROM [>]
Set oConn = CreateObject("ADODB.Connection")
Set oRS = CreateObject("ADODB.RecordSet")
oConn.Open "DRIVER={InterSystems ODBC};SERVER=FQDN.domain.local;DATABASE=database_name;UID=user;PWD=pwd"
If oConn.State <> 1 Then MsgBox "connect failed"
SQLCmd = "SELECT * FROM [E-Mail_Addresses] WHERE Name='JohnDoe'"
oRS.Open SQLCmd, oConn

Open in new window

0
 
LVL 18

Author Comment

by:exx1976
ID: 33704312
Mustaccio - unfortunately, no, the double quotes doesn't work.  The error is:

[SQLCODE: <-1>:<Invalid SQL statement>]
[Cache Error: <<SYNTAX>errdone+1^%qaqqt>]
[Details: <Prepare>]
[%msg: < SQL ERROR #1: `ID' expected, `:' found^SELECT * FROM :%qpar>]
0
 
LVL 8

Expert Comment

by:mustaccio
ID: 33704346
Did you escape double quotes in your double-quoted SQLCmd string?
0
 
LVL 18

Author Comment

by:exx1976
ID: 33704405
Yes.  The actual code used was

SQLCmd = "SELECT * FROM ""Co-Signer_Route_Bal"" WHERE ACCOUNT_NUM='36'"

I also tried

SQLCmd = "SELECT * FROM """"Co-Signer_Route_Bal"""" WHERE ACCOUNT_NUM='36'"

with the same result.
0
 
LVL 18

Author Comment

by:exx1976
ID: 33704413
Ok..  So now we know it's not email addresses..   LOL   Oops!  I feel like such a n00b
0
 
LVL 17

Accepted Solution

by:
StrifeJester earned 500 total points
ID: 33704676
Perhaps wherever you are getting the table name from is wrong.  Perhaps it is just and underscore and someone forgot to hold shift.
0
 
LVL 18

Author Comment

by:exx1976
ID: 33704718
That's the most ridiculous thing I've ever heard...  Let me go try it!
0
 
LVL 18

Author Comment

by:exx1976
ID: 33704750
Oops  no!!  I wanted to accept 33704676
0
 
LVL 18

Author Closing Comment

by:exx1976
ID: 33704755
Go figure, typo in the FM..
0

Featured Post

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Grid querry results 41 80
Help with stripping out character in SQL LEFT/RIGHT/REPLACE 2 48
Restrict result set 1 39
My Query is not giving correct result. Please help 5 51
Introduction This article will provide a solution for an error that might occur installing a new SQL 2005 64-bit cluster. This article will assume that you are fully prepared to complete the installation and describes the error as it occurred durin…
This article explains how to reset the password of the sa account on a Microsoft SQL Server.  The steps in this article work in SQL 2005, 2008, 2008 R2, 2012, 2014 and 2016.
This video shows how to quickly and easily add an email signature for all users on Exchange 2016. The resulting signature is applied on a server level by Exchange Online. The email signature template has been downloaded from: www.mail-signatures…
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…

820 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