Order table being read as ORDER command by the compiler - sql

I have this sql statment stored in a string variable
SELECT
o.id_order,
o.registerdate,
i.latest_invoice,
d.latest_delivery
FROM
order o
JOIN
(
SELECT
id_address,
max(registerdate) as latest_invoice
FROM
invoice
GROUP BY
id_address
) i
ON o.id_address = i.id_address
JOIN
(
SELECT
id_address,
max(registerdate) as latest_delivery
FROM
delivery
GROUP BY
id_address
) d
ON o.id_address = d.id_address
WHERE
o.id_address = '189'
When I execute the query I get an error at the position of order o, because the compiler thinks that it is the ORDER BY command. Do you know how can I avoid this?

You shouldn't be using reserved keywords as table names, but if you insist you have to use double quotes:
from "order" o
Note that with double quotes the name is case-sensitive. Depending on how you created that table, you might need "ORDER" or "Order". In psql you should check the correct case using the \d command.

Related

SQL Many-to-Many Query in Apex Oracle

I created a many-to-many relationship between the tables PRODUCT_TYPE and LABEL by creating and intermediate table PRODUCT_TYPE-LABELS. I wanted to retrieve all the products that have the labels 'Gluten free' and 'Lactose free' and found help on a similar subject (How to filter SQL results in a has-many-through relation) but never got it to work properly.
The tables are as follows:
PRODUCT_TYPE{
PRODUCT_TYPE ->Primary Key
CONTAINER
VOLUME_L
PRICE_EUROS
...
}
LABEL{
LABEL_NAME ->Primary Key
REQUIREMENTS
}
PRODUCT_TYPE-LABELS{
PRODUCT_TYPE
LABEL_NAME
}
In fact, even when creating the simplest command
SELECT PRODUCT_TYPE-LABELS.PRODUCT_TYPE
FROM PRODUCT_TYPE-LABELS
I obtain the following error ORA-00933: SQL command not properly ended that I can't solve. I'm working on Apex Oracle (Required for this course).
Thanks !
If your table is really named PRODUCT_TYPE-LABELS then you need to enclose it within double quotes. - is not a standard character that is allowed in table names so to use special characters such as that, you need to put quotes around the table. I would recommend AGAINST using a table name such as that and maybe use something like PRODUCT_TYPE_LABEL.
Does the query work from SQL Developer, SQLPlus or any other tool that you can use to run queries?
Try running the query:
SELECT PRODUCT_TYPE
FROM "PRODUCT_TYPE-LABELS"
select * from (
select rownum rowno, CUST_ID,CUST_NAME,no_of_orders,orders_amount,EMAIL from (
select mst.CUST_ID, mst.CUST_NAME, count(mst.INVONO) no_of_orders, sum(SUB_TOTAL) orders_amount, au.EMAIL
from sales_mst mst,CUSTOMER_INFO cust, APP_USERS au
where cust.USER_NAME = au.USERNAME
and cust.cust_id=mst.cust_id
and (au.gender= :P41_GENDER or :P41_GENDER is null)
and (au.DOB = :P41_DOB or :P41_DOB is null)
group by mst.CUST_ID, mst.CUST_NAME,au.EMAIL
having nvl(sum(SUB_TOTAL),0) > 0
order by 3 desc,4 desc
)) where rowno <=:P41_TO_CUSTOMER
/*select * from (
select rownum rowno, CUST_ID,CUST_NAME,no_of_orders,orders_amount from (
select mst.CUST_ID, mst.CUST_NAME, count(mst.INVONO) no_of_orders, sum(SUB_TOTAL) orders_amount
from sales_mst mst
group by mst.CUST_ID, mst.CUST_NAME
having nvl(sum(SUB_TOTAL),0) > 0
order by 3 desc,4 desc
)) where rowno <=:P41_TO_CUSTOMER
*/

column name in alias is getting shortened

I am using below query to create a table
CREATE TABLE FINAL_TBL AS SELECT /*+ leading(e) use_hash(t) full(t) parallel(t,2) */
t.customer_id
, max(decode(type,'C',t.num,null)) AS CONTACT_TELNO
, max(decode(type,'H',t.num,null)) AS HOME_TELNO
, max(decode(type,'W',t.num,null)) AS WORK_TELNO
, max(decode(type,'O',t.num,null)) AS OTHER_TELNO
from TABLE_A e, TMP_NUMBERS t
where t.num = e.num
and e.status = 'P'
group by t.customer_id
order by 1;
But the table is getting created with the truncated column name as below
CUSTOMER_ID CONTACT_TE HOME_TELNO WORK_TELNO OTHER_TELN
Why is So? How can I fix it?
It sounds like you must be running this in SQL*Plus... you need to use the command COLUMN with the FORMAT option, like so: column CONTACT_TELEPHONE format a18 (notice no semicolon since this is not a SQL command, it only applies to your interface). The SQL query runs as you expected, but then SQL*Plus formats the result according to its own settings.

Get columns of index on DB2

How can I get the columns, which an index of a table uses, in DB2?
I tried:
DESCRIBE INDEXES FOR TABLE 'MYTABLE' SHOW DETAIL;
But I get the error message
ILLEGAL SYMBOL "INDEXES". SOME SYMBOLS THAT MIGHT BE LEGAL ARE: PROCEDURE PROC. SQLCODE=-104, SQLSTATE=42601, DRIVER=4.16.53
Ideally I want information of all indexes a table uses with their corresponding columns.
I am using DB2 for z/OS V9.1
You can use this query to show the indexes and their columns of your tables:
SELECT IX.tbname,
KEY.ixname,
KEY.colname
FROM sysibm.syskeys KEY
JOIN sysibm.sysindexes IX
ON KEY.ixname = IX.name
WHERE IX.tbname IN ( 'SOMETABLE', 'ANOTHERTABLE' )
ORDER BY IX.tbname,
KEY.ixname,
KEY.colname;
SELECT * FROM SYSIBM.SYSKEYS WHERE IXNAME IN
(SELECT NAME FROM SYSIBM.SYSINDEXES WHERE TBNAME = 'your_table_name')
I have tested it, it is giving us all the columns which are used in indexes.
You can use below query also. it works fine if syskeys table is missing
SELECT * FROM SYSIBM.SYSINDEXCOLUSE where INDNAME IN (SELECT NAME FROM SYSIBM.SYSINDEXES si where si.TBNAME ='your_table_Name' ) ORDER BY INDNAME, COLSEQ
I had an issue with using "KEY" as a table alias. Also, if you have multiple schemas with the same table name, use the following:
SELECT IX.TABLE_SCHEMA, IX.TABLE_NAME, IX.INDEX_NAME, KY.ORDINAL_POSITION, KY.COLUMN_NAME
FROM SYSKEYS KY
JOIN SYSINDEXES IX ON (KY.INDEX_NAME = IX.INDEX_NAME AND KY.INDEX_SCHEMA = IX.INDEX_SCHEMA)
WHERE IX.TBNAME = 'table-name' AND IX.TABLE_SCHEMA = 'table-schema'
ORDER BY IX.TABLE_SCHEMA, IX.TABLE_NAME, IX.INDEX_NAME, KY.ORDINAL_POSITION
FOR READ ONLY WITH UR

RODBC, SQL Order By clause + field ID = Order conflict

Does this make sense? I otherwise don't see the error.
Using RODBC, R returns a 'Could not SQLExecDirect' error for a sqlQuery statement issued to a table containing a field ID = Order. The SQL otherwise works. I can however read the entire table to a df using sqlFetch (see below).
The target db is on SQL Server.
Example of table structure:
Taxon_Id = c(3000,3001,3002)
Group_Id = c(6,5,5)
Type = c('Fish','Fish','Fish')
Order = c('Petromyzontidae','Acipenseridae','Clupeidae')
Family = c('Petromyzontidae','Acipenseridae','Clupeidae')
txn = data.frame(Taxon_Id,Group_Id,Type,Order,Family)
Example of SQL issued to table:
txn2<-as.data.frame(sqlQuery(channel, paste('SELECT T.Taxon_Id,
T.GroupId,
T.Type,
T.Order,
T.Family
FROM Taxon T
ORDER BY 1
')) )
sqlFetch reads all table fields without error.
txn<-as.data.frame(sqlFetch(channel,"Taxon"))
Thanks for your comments.
This is your query:
SELECT T.Taxon_Id, T.GroupId, T.Type, T.Order, T.Family
FROM Taxon T
ORDER BY 1
In SQL (in general) and SQL Server (in particular), the word Order is a reserved word. You need to escape it with either double quotes or square braces:
SELECT T.Taxon_Id, T.GroupId, T.Type, T.[Order], T.Family
FROM Taxon T
ORDER BY 1

How To Handle Table Column Named With Reserved Sql Keyword?

I have a table that has a column named RANK which is a keyword in Oracle.
Now I need to insert data in this table :
insert into mytbl (RANK)
select RANK from other_table
when executing this query I got the following error :
ORA-00907: missing right parenthesis
How does one escape a keyword?
Oracle uses double quotes " to escape reserved words.
insert into mytbl ("RANK")
select "RANK"
from other_table
One other note, Oracle requires correct case as well.
First of all, You shall not use reserved keywords as column name and table name.
Oracle uses Double quotes " to parse reserved keywords so you can parse the keywords
by placing it in doubles quotes "".
insert into mytbl ("RANK")
select "RANK"
from other_table
In my case, there is , in my query.
UPDATE SCHEMA.TABLE SET PART_NO = '1S7F530400', WHERE PART_NO = '1S7?F5304?00';
This should be:
UPDATE SCHEMA.TABLE SET PART_NO = '1S7F530400' WHERE PART_NO = '1S7?F5304?00';
It has been mentioned before, but to emphasize, one must exactly match the case in all uses. When I needed to extract the GROUP column and order it, "Group" did not work. It had to be "GROUP" in both cases such as
select "GROUP" from PICKLIST
order by "GROUP"