Left join ON not null columns can't select not null column - sql

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

Related

The SELECT statement includes a reserved word or an argument that is misspelled or missing, or the punctuation is incorrect

I'm trying to create a query using two tables I got the following message:
The SELECT statement includes a reserved word or an argument that is misspelled or missing, or the punctuation is incorrect
I am SQL ignorant.
FROM [UIC ROSTER]
INNER JOIN [Azusa RSP] ON [UIC ROSTER].UIC = [Azusa RSP].UIC

Syntax error when selecting column 'Comment' in Sybase IQ

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"

Bigquery type cast on JOIN Clause

I'm trying to join two tables on two columns
-- query to join two tables
SELECT
*
FROM
[raw.raw_sales] AS game_records
JOIN
[facebook_aggregate.avg_aggregate] AS avg_aggregate
ON
(game_records.Away_team = avg_aggregate.team_name)
AND (game_records.game_date = avg_aggregate.time_update)
it gives me this error Error: 10.30 - 10.56: Timestamp literal or explicit conversion to timestamp is required. because game_records.game_date is type STRING and avg_aggregate.time_update is type DATE.
but if I do the conversion within the JOIN..ON.. clause
-- query to join two tables
SELECT
*
FROM
[raw.raw_sales] AS game_records
JOIN
[facebook_aggregate.avg_aggregate] AS avg_aggregate
ON
(game_records.Away_team = avg_aggregate.team_name)
AND (DATE(game_records.game_date) = DATE(avg_aggregate.time_update))
It gives me this error:
Error: ON clause must be AND of = comparisons of one field name from each table, with all field names prefixed with table name. .
Is there any way to do this without creating an intermediate table? Thanks!
Try using standard SQL (uncheck "Use Legacy SQL" under "Show Options"). You shouldn't need to do anything aside from remove the brackets around the table names:
SELECT
*
FROM
raw.raw_sales AS game_records
JOIN
facebook_aggregate.avg_aggregate AS avg_aggregate
ON
game_records.Away_team = avg_aggregate.team_name
AND game_records.game_date = avg_aggregate.time_update;
BigQuery Standard SQL (see Enabling Standard SQL) does not have this limitation for ON clause. Try running your query in Standard SQL.
As Elliott mentioned - make sure you are not using square brackets around tables references. In Standard SQL - when you need to escape special chars - yo should use back-ticks
Also check Migrating from legacy SQL if you will follow above direction

Using SQL keywords as names without brackets

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.

Hibernate - Raw Query execution_KEY Words Issue in query?

The setup consists of Hibernate 3. Am trying to execute the raw query as it is. The setup works fine for other simple queries , db inserts & updates.
The query in issue is :
org.hibernate.hql.ast.QuerySyntaxException: unexpected token: ( near line 1, column 29 [
SELECT keyMain, value FROM (select distinct K.[key] as keyMain,
( SELECT value FROM com.trans.dto.Resources as L WHERE L.[key] = K.[key]
and L.lang_code = 'A11' ) as value from com.trans.dto.Resources as K )
as test order by keyMain ]
Resources is the table & has mapping setup in hibernate.cfg.xml
I was under a thought "KEY" is name of one of the column which can not be changed. How do i escape key words ?
If not 1, then is the multi selects in sub query.
Please advise. Any suggestion is of great help.
From here:
You can force Hibernate to quote an identifier in the generated SQL
by enclosing the table or column name in backticks in the mapping document.
Hibernate will use the correct quotation style for the SQL Dialect.
This is usually double quotes, but the SQL Server uses brackets and MySQL uses backticks.
So, try to escape your field with double quotes or with square parenthesis('[key]').