Developer
Developer

Reputation: 329

Not able to run SQL queries in AS400, run into Invalid Token errors

In AS400, how can i perform arithmetic operations (like +, -) on fields.

Could someone please tell me what's wrong with my sql queries.

Upvotes: 0

Views: 29907

Answers (2)

James Allman
James Allman

Reputation: 41148

TABLE is a reserved word. SQL Reference: Reserved schema names and reserved words.

  • Single quotes escape a string literal
  • Double quotes escape a reserved word (similar to brackets in TSQL)

SQL Reference: Identifiers

The queries could be re-written as:

SELECT ID, SUM(FIELD1 + FIELD2) AS TOTAL FROM "TABLE" GROUP BY ID
SELECT COUNT(*) FROM (SELECT DISTINCT FIELD1 FROM "TABLE" WHERE FIELD2 = 'ABC')

UPDATE

DB/2 for i does not support your method of numeric to character conversion or the type of character comparison used in your LIKE query.

The query can be re-written as:

SELECT eds, SUM(INT(sds)) AS totalh 
FROM tbl1 
WHERE eds BETWEEN 20130500 AND 20130599 
AND siteds IN (
    SELECT DISTINCT site 
    FROM tbl2 
    WHERE H_04 IN ('1234') AND PERIOD = 201305
) 
GROUP BY eds
ORDER BY eds

You may need to use DEC instead of INT depending upon the definition of field sds.

SQL Reference: - INT - DEC - BETWEEN

Upvotes: 6

David G
David G

Reputation: 4014

Is the table's name really 'table'?

Odd as it seems, if I use the SQL statement you indicated as is, I get the same error... but if I change 'table' to 'table1', it just complains that it can't find 'table1'.

Upvotes: 1

Related Questions