I have a table A with some columns in Sybase IQ. One of the columns is named "Comment".
Whenever I select that column:
select Comment from A
I got the error:
[Error Code: 102, SQL State: 42W04] SQL Anywhere Error -131: Syntax error near 'Comment'
I am able to select other columns without issues. Could you advise the reason and solution? Thank you
Try
select "Comment" from A
COMMENT is a reserved word in Sybase IQ.
Here is the link explaining your problem.
Some keywords in SQL are also reserved words. To use a reserved word
in a SQL statement as an identifier, you must enclose the word in
double quotes. Many, but not all, of the keywords that appear in SQL
statements are reserved words. For example, you must use the following
syntax to retrieve the contents of a table named SELECT.
SELECT * FROM "SELECT"
Each table has a column RECNUM. They are (decimal(28,0), not null). That is where I am doing my join. I want to select the column DESC in CAUNIT. It is (varchar(28,0), not null). When I run my query I get:
Msg 156, Level 15, State 1, Line 1
Incorrect syntax near the keyword 'DESC'. Below is my query:
SELECT CDCLSVC.UNIT_ID,CAUNIT.DESC
FROM CDCLSVC
LEFT JOIN CAUNIT
ON CDCLSVC.RECNUM = CAUNIT.RECNUM
The problem is with DESC column. In SQL Server it is a reserved keyword:
Microsoft SQL Server uses reserved keywords for defining,
manipulating, and accessing databases. Reserved keywords are part of
the grammar of the Transact-SQL language that is used by SQL Server to
parse and understand Transact-SQL statements and batches. Although it
is syntactically possible to use SQL Server reserved keywords as
identifiers and object names in Transact-SQL scripts, you can do this
only by using delimited identifiers.
Possible solution:
Rename column e.g. description
Quote it with []
You could also use aliases to avoid typing full table names:
SELECT cd.UNIT_ID,ca.[DESC]
FROM CDCLSVC cd
LEFT JOIN CAUNIT ca
ON cd.RECNUM = ca.RECNUM
Which keywords are allowed to be used as names without brackets and which are not ?
Why some SQL keywords are allowed to be used as names when others are not ?
Is there any pattern in determination of which I can use and which I can't without trying to compile and getting error ?
I have impression that older keywords are not allowed, but newer are allowed so that newer SQL versions are as compatible as possible with older.
In the following example rollup can be used as name without brackets, but group cannot.
if object_id('accident') is not null drop table accident
create table accident(
state varchar(50)
,city varchar(50)
,zip varchar(50)
,person varchar(50)
,id int identity(1,1)
)
insert accident(state,city,zip,person)values
('NY','Manhattan',10001,'John')
,('NY','Manhattan',10001,'John')
,('NY','Manhattan',10001,'Barbara')
;with
rollup as (
select accident.*
,lvl = grouping_id(accident.state,accident.city,accident.zip,accident.person,accident.id)
,accidents=count(1)
,people=0
from accident
group by rollup(accident.state,accident.city,accident.zip,accident.person,accident.id)
)
select * from rollup
;with
[group] as (
select accident.*
,lvl = grouping_id(accident.state,accident.city,accident.zip,accident.person,accident.id)
,accidents=count(1)
,people=0
from accident
group by rollup(accident.state,accident.city,accident.zip,accident.person,accident.id)
)
select * from [group]
Attempt to use group as name without brackets
;with
group as (
select accident.*
,lvl = grouping_id(accident.state,accident.city,accident.zip,accident.person,accident.id)
,accidents=count(1)
,people=0
from accident
group by rollup(accident.state,accident.city,accident.zip,accident.person,accident.id)
)
select * from group
gives error:
Msg 156, Level 15, State 1, Line 28
Incorrect syntax near the keyword 'group'.
Msg 156, Level 15, State 1, Line 36
Incorrect syntax near the keyword 'group'.
There is a list of reserved keywords and you can find it in here. It's too long to paste it into this answer. All reserved keywords have to be escaped. From your example rollup is not a reserved keyword, whereas group is.
There are more kywords in ISO standards. Plase take a look at the following notes in the same link:
Additionally, the ISO standard defines a list of reserved keywords. Avoid using ISO reserved keywords for object names and identifiers. The ODBC reserved keyword list, shown in the following table, is the same as the ISO reserved keyword list.
The ISO standards reserved keywords list sometimes can be more restrictive than SQL Server and at other times less restrictive. For example, the ISO reserved keywords list contains INT. SQL Server does not have to distinguish this as a reserved keyword.
Transact-SQL reserved keywords can be used as identifiers or names of databases or database objects, such as tables, columns, views, and so on. Use either quoted identifiers or delimited identifiers. Using reserved keywords as the names of variables and stored procedure parameters is not restricted.
I've seen both used but I can't seem to understand when to use each?
To me is seems like you enter the name of the table you are referring from in the ( ) and the field name in the [ ]?
Could anyone explain?
The square brackets are used In Microsoft products to specify that what's within them is an identifier (the standard quoted identifiers are double quotes " ", which Microsoft SQL Sever also supports). This is used when you have a database name, user name, table name, field name, view name, procedure name (et.c.) that happens to be the same as a keyword, or contains characters that would break the syntax. This is often used in generated code to safeguard against identifiers that can't otherwise be used in the code. A generated query could look like this:
select [Id], [Name], [Password hint]
from [dbo].[MyDataBase].[User]
Here the field name Password hint would break the syntax if used without brackets, and the table name User could conflict with the keyword User.
Parentheses are used to group items, for example as part of the syntax of some clauses, for example an insert:
insert into someTable (field1, field2) values ('value1', 'value2')
They can also be used in expressions:
select Price * (Quantity + FreeItems) from Articles
They can also be used around queries to make subqueries:
select o.Name
from (select Name, Age from Persons where City = 'Oslo') as o
where o.Age > 18
() are used for passing parameters to functions and stored proceedures etc. [] are used to encapsulate field name (etc.) which include punctuation (spaces and special characters as per the comment above). [] are useful sometimes to name fields for display
SELECT FFgg AS [Some field discription] FROM table1;
Hope this helps.
I am linked to a Proficy Historian that allows periods in the column names. Because the data is stored in a non DBMS format I can not use openquery to get the data because there is no set schema to the tables. So I must use four part name syntax to get the data. This example works:
SELECT * FROM iHist...[SELECT * FROM ihTrend]
but this fails with Incorrect syntax near '.'.
SELECT * FROM iHist...[SELECT [SERVER.pid_astatus[07][0].F_CV.Value] FROM ihTrend]
where SERVER.pid_astatus[07][0].F_CV.Value is the name of the column
This fails as well with Incorrect syntax near the keyword 'from'.
SELECT * FROM
iHist...[SELECT [SERVER.pid_astatus[[07]][[0]].F_CV.Value] from ihTrend]`
Any ideas on how I can make SQL Server see this as a column?
EDIT:
Martins suggestion of the right brackets to escape the brackets work only on the outside of the sql call
SELECT [SERVER.pid_astatus[07]][0]].F_CV.Value] FROM iHist...[SELECT * FROM ihTrend]
However it does not work inside Incorrect syntax near the keyword 'from'.
SELECT * FROM iHist...[SELECT [SERVER.pid_astatus[07]][0]].F_CV.Value] FROM ihTrend]
EDIT
SELECT * FROM iHist...[SELECT [SERVER.pid_astatus[07]][0]].F_CV.Value]] FROM ihTrend]
I had to escape the column escape :)
You only need to escape these ]
[pid_astatus[07]][0]].F_CV.Value]
This works for me
CREATE TABLE #t(
[pid_astatus[07]][0]].F_CV.Value] int
)
SELECT [pid_astatus[07]][0]].F_CV.Value]
FROM #t
(Edited to reflect new knowledge, if you like this vote for Martin Smith's answer instead!)
Escape the ] by doubling them:
SELECT * FROM
iHist...[SELECT [SERVER.pid_astatus[07]][0]].F_CV.Value] from ihTrend]
Based on your comment, try:
SELECT [SERVER.pid_astatus[07]][0]].F_CV.Value] FROM iHist...ihTrend