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

x
?
Solved

Issue with SQL Query and the - character

Posted on 2009-04-22
3
Medium Priority
?
282 Views
Last Modified: 2012-05-06
Hello,

I have a sql query in SQL 2005:
select MY-SQL.MYDATABASE.MYTABLE.idnum FROM etc.
On one computer (MY-SQL itself), i recieve this error when running the query:
Incorrect syntax near '-'.

I have an application that runs on this server, that uses this query, which also gives me the same error.

I have the same application on another computer, that DOES NOT give me this error...

My question is ...WHY!!!

0
Comment
Question by:melegant99
3 Comments
 
LVL 143

Accepted Solution

by:
Guy Hengel [angelIII / a3] earned 2000 total points
ID: 24209149
-  is invalid inside names... unless you put [] around it:
select [MY-SQL].MYDATABASE.MYTABLE.idnum FROM etc.

Open in new window

0
 

Author Comment

by:melegant99
ID: 24209208
Yes, that worked great. But - It does work for me through my application that resides on any other computer other than the SQL server itself...and the code is NOT putting brackets around the name (although, that is what I'll do to fix it)
0
 
LVL 57

Expert Comment

by:Raja Jegan R
ID: 24209228
Your servers differ in the versions. Might be because of ANSI_DEFAULTS option

To avoid obtaining errors in both your machines.

select [MY-SQL].MYDATABASE.MYTABLE.idnum FROM etc

- is a Subtraction string and since it is present within characters, you are obtaining this error. If you encapsulate it within "" or [] then it considers that as a single object and you wont be obtaining errors out of it. This will be checked based upon the Collation and ANSI_DEFAULTS option of your SQL Server.
0

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying 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

Your data is at risk. Probably more today that at any other time in history. There are simply more people with more access to the Web with bad intentions.
This article shows how to get a list of available printers for display in a drop-down list, and then to use the selected printer to print an Access report or a Word document filled with Access data, using different syntax as needed for working with …
Video by: Steve
Using examples as well as descriptions, step through each of the common simple join types, explaining differences in syntax, differences in expected outputs and showing how the queries run along with the actual outputs based upon a simple set of dem…
In this video, Percona Director of Solution Engineering Jon Tobin discusses the function and features of Percona Server for MongoDB. How Percona can help Percona can help you determine if Percona Server for MongoDB is the right solution for …
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