How to Create a VIEW in oracle - oracle

So I'm supposed to create a view product_view that presents the information about how many products of a particular type are in each warehouse: product ID, product name, category_id, warehouse id, total quantity on hand for this warehouse.
So I used this query and tried to change it so many times but I keep getting errors
CREATE OR REPLACE VIEW PRODUCT_VIEW AS
SELECT p.product_id, p.product_name,
COUNT(p.product_id), SUM(i.quantity_on_hand)
FROM oe.product_information p JOIN oe.inventories i
ON p.product_id=i.product_id
ORDER BY i.warehouse_id;
ERROR at line 2:
ORA-00928: missing SELECT keyword
Please help... Thanks
Image showing the Tables in the OE schema
Image showing the error that occurs

When I get errors creating a view, I firstly drop the CREATE ... AS line and fix the query until it works. Then you need to name all the columns, for instance COUNT(p.product_id) won't work, you'll need to write something like COUNT(p.product_id) AS product_count or specify a list of aliases, like so
I'm not sure what the output of your query should look like. You'll get better answers quicker on stackexchange if you type a minimal example including the CREATE statments, some input data and your desired output, leaving out columns that are not essential.

Related

Insert Into from another table causing Can't update 'table' in stored function/trigger because it is already used by statement which invoked

I'm pretty new to SQL so if you can keep it as as simple as possible that would be appreciated.
Currently trying to add all the contact IDs from the contact table (civicrm_contacts) to a new custom field table which is a button to show/hide a contact from the main website. At the moment whenever I run my query I get back the following:
"SQL Error [1442] [HY000]: Can't update table 'civicrm_contact' in stored function/trigger because it is already used by statement which invoked this stored function/trigger."
My query is going something like this:
INSERT INTO civicrm_value_appears_on_we_69 (entity_id)
SELECT cc.id
FROM civicrm_contact cc
WHERE contact_type='Organization'
There are 3 columns in total for the 'civicrm_value_appears_on_we_69': 'ID', 'entity_id', and 'appears_on_website_45' which is a 0 or 1 for the button value. All I want is to insert the contact IDs for all the Organisation contacts, I don't mind about nulls for now as long as it makes the rows.
I've looked up a bit about the error and I'm unsure why it's happening. As far as I can gather this errors happpens when you're using a trigger that using the same table as you're inserting into/from. I'm not using a trigger at the moment, or perhaps I have this wrong.
Any suggestions?

Run 2 SETs in an Update Statement Oracle

Is it possible to update 2 columns in an Update statement that are in different tables? - The reason for the"scripted":
Where "Scripted" will be the "flag" so the formula does not run again on the same records if this field is filled in.
MERGE INTO arinvt_lot_docs ALD
USING
(SELECT arinvt.id,arinvt.class,fgmulti.in_date fgmulti.cuser3 FROM arinvt,fgmulti
WHERE arinvt.class LIKE 'CP%'
OR arinvt.class LIKE 'FG%'
OR arinvt.class LIKE 'IN%'
OR arinvt.class LIKE 'LA%'
OR arinvt.class LIKE 'PK%') Classes
ON (ALD.arinvt_id = classes.id
AND to_date(in_date) = '31-Dec-2015') --just picked a date to validate
WHEN MATCHED THEN
UPDATE SET non_conform_id = '21', fgmulti.cuser3 = 'SCRIPTED' --this text "Scripted" will fill in a field that will tell us in our reports if this was set by the script
I would like to join the tables using the arinvt.id field that is present in all 3 tables ARINVT_LOT_DOCS, FGMULTI & obviously ARINVT. ARINVT_LOT_DOCS & FGMULTI contain the NON_CONFROM_ID field that needs to be changed to '21'. The FGMULTI table also contains the CUSER3 field that would have "SCRIPTED" entered in it. The ARINVT table contains the Class of the inventory item which reflects in the conditions mentioned.
You cannot update two tables in one query in Oracle and other DBMS such as SQL Server but you can use transaction to achieve similar result.
This oracle community answers exactly that, if you try to join two tables, you will get this error
ORA-01776: cannot modify more than one base table through a join view
You can use transactions to update two tables in batch-like statement.
This https://stackoverflow.com/a/2044520 shows how to do it but for SQL Sever though. You need similar statement in Oracle.

How to see Table detail when writing query?

How to see Table detail when writing query?
Hi, i am new to using Embarcadero DBArtisan for Oracle and Syabase queries executions.
but i am unable to see the tables and fields information when i write queries.
Forexample:
Databasname.User.Table.Fields
Suppose if i write first Database name and write .(dot) it should show me all users and when i pick one user and write .(dot) agian it should show me all tables inside and when i pick one table and write .(dot) it should show me all the fields inside.
But it is not doing that way. every time i have to describe table to find the specific field in the table.
I am woundering if i have to choose some option to see this information.
Thank you very much for your help.
Sybase, there is no User.
I think you have to write a cursor to loop over the DBs on a server.
For the rest of your question:
select db_name() 'databasename', b.name 'table', a.name 'fields'
from syscolumns a, sysobjects b
where a.id = b.id
-- and b.type = 'U' -- User tables only
It seems that there is a setting in Code Workbench. Please see page 733 in the DBArtisan User Guide.
From the Manual,
In the ISQL Window, when Enable Auto Replacement is selected on the Setting Tab,
the application uses "dot completion" auto population to display the list of columns
for the target table.

Getting "ORA-01482: unsupported character set" when using WHERE IN pattern

I have a table with the following structure in Oracle database:
CREATE TABLE PASSENGERS
(ID VARCHAR2(6),
PASSPORTNO VARCHAR2(14));
I want to get the IDs of the passengers who have been registered more than once. For that I run the following query.
SELECT ID FROM PASSENGERS WHERE PASSPORTNO IN
(SELECT PASSPORTNO FROM PASSENGERS
GROUP BY PASSPORTNO
HAVING COUNT(*)>1);
But I get "unsuported character set" error. What's the point I'm missing?
Since all queries related with PASSPORTNO are running fine you have at least two more things to do:
Run SELECT ID FROM PASSENGERS and check for errors, if the error cames up, then it may be releated with content stored in your table
Try another SQL tool to execute your queries, your client OS may be using a system enconding which the database can't understand both when processing your query of to display the returning rows.
Since both ID and PASSPORTNO are varchar fields, there's a big change to one of then have data in a enconding which oracle can't decode properly.
Mostly seems like a data issue. Try checking the exact data row which is causing the issue.
Use : DML Error Logging - http://www.oracle-base.com/articles/10g/dml-error-logging-10gr2.php
Btw, you are doing GROUP BY passportno .Is that correct? (This implies multiple passports can have same passport number). I guess it should be GROUP BY id

Quering Oracle materialized view - column ambiguously defined

I have a trouble querying view in Oracle.
Here is the view (my_cool_view) definition:
SELECT *
FROM mview1 JOIN
mview2 USING(col_id)
where mview1 and mview2 is materialized views. I can't access to definition of this materialized views. I've tried this query:
select r.title from my_cool_view r;
and got ORA-00918: column ambiguously defined error. I got this error for subset of columns in my_cool_view.
If I run this query:
select * from my_cool_view;
all works fine.
You are asking about desc my_cool_view in comments, sorry I can't provide schema information because it's confidential. The only I can say it contains about 80 columns, and only one (col_id) don't cause above error.
Do you have any ideas what is the case and how to fix it?
Thanks in advance.
Sorry for misled you, the first version of question was too bad.
You probably have 2 columns called title from different tables/views that you've joined. If you select * then this will work as the duplicate title columns will get a different alias (like TITLE1). When you explicitly ask for one of the title columns the database needs to know which one you want and thus throws the error.
Just make sure that the title columns have a different alias:
select EMP.TITLE EMP_TITLE, DEPT.TITLE DEPT_TITLE
FROM ...

Resources