Why SQL statements do not work in table created in Oracle10g - sql

I have an Oracle SQL database with 2 scripts I uploaded to Oracle, one includes tables, the other includes the registries.
I have a table DEPARTMENTS with 2 columns, CODE and NAME.
From Oracle 10g SQL home > SQL workshop > SQL commands, I type in this statement:
SELECT CODE, NAME
FROM DEPARTMENTS
The result is
"No data found"
I cannot retrieve results correctly either in Oracle 10g nor in livesql.oracle.com.
Is my statement incorrect? Is it not retrieving the data uploaded?

Result should actually be "no rows selected", not "no data found".
You said you have two scripts; I presume that one of them contains something like this:
SQL> create table departments
2 (code number,
3 name varchar2(20));
Table created.
Basically, you created an empty table. If you query it, there's nothing in there:
SQL> select code, name from departments;
no rows selected
Therefore, the second script most probably contains statements that populate tables created in the first script. Is that what you call "registries"?
SQL> insert into departments (code, name)
2 select 1, 'Accounting' from dual union all
3 select 2, 'Finance' from dual;
2 rows created.
Let's repeat the previous select statement:
SQL> select code, name from departments;
CODE NAME
---------- ----------
1 Accounting
2 Finance
SQL>
Right; now we got the result.
On the other hand, maybe you actually ran both scripts, created tables and inserted rows, but you didn't commit at the end of the second script. If you then opened a new session (in Oracle 10g SQL home > SQL workshop > SQL commands), then you saw tables (because create table is a DDL and it modified data dictionary so select you wrote didn't return "ORA-00942: table or view does not exist"), but insert is a DML and you have to COMMIT - otherwise only that session (that ran inserts) will see data.
Finally, check whether insert statements completed successfully; if not, there's a chance that you didn't insert anything and will first have to fix errors. Which ones? No idea, I don't have those scripts nor saw the screen (or log file, if there is any).

Related

Teradata query using a large list of values in WHERE predicate

I'm attempting to query Teradata in SQL Assistant, something along the lines of
select * from
addressTable
where accountNumber in
(
'AC001',
'AC098',
'AC711',
...
)
from a list of over 100,000 accountNumbers sent to me in a file.
What I have tried
StackOverflow past questions - all seem to say that you need to insert into a temp table using a select clause from a table within the same database
Teradata doco on the link below. When I go to create table globdb.gt1, I get error 'CREATE TABLE Failed 3802: Database 'globdb' does not exist.
https://docs.teradata.com/r/Teradata-Database-SQL-Fundamentals/June-2017/Database-Objects/Tables/Global-Temporary-Tables

Query emp table in oracle 19c

make a java program that queries the inbuilt table “emp” and display the first two columns (empno using column index and ename using column name ) of all the rows.
But problem here is :
ERROR:
ORA-04043: object emp does not exist
I am using oracle 19c on windows platform and using pluggable database orclpdb user "hr".
please tell me where to find this emp table.
And second question :
Even if I manually create a new Emp table in hr user ,what will be the sql command for the given
/*
* Using
* queries the inbuilt table “emp” and
* displays the first two columns
* (empno using column index and ename using column name )
* of all the rows.
*/
just tell me the sql command only.

Creation of a temporary table in postgres

I'm trying to create a temporary table in Postgres (to speed up joining, as there will be a lot of similar queries throughout a session). The SQL that will be called at the beginning of a session is the following:
CREATE TEMPORARY TABLE extended_point AS
SELECT (
point.id,
local_location,
relative_location,
long_lat,
region,
dataset,
region.name,
region.sub_name,
color,
type)
FROM point, region, dataset
WHERE point.region = region.id AND region.dataset = dataset.id;
The tables point has the columns id::int, region::int, local_location::point, relative_location::point, long_lat:point (longitude, latitude).
Region has the columns id::int, color::int, dataset::int, name::varchar, sub_name::varchar.
Dataset has the columns id::int, name::varchar, type:varchar.
When this is run, I get the error message: [25P02] ERROR: current transaction is aborted, commands ignored until end of transaction block.
As a side, the commands are executed in PyCharm, and is part of a Python project.
Any suggestions?
Thanks in advance :)
There is an important difference between these two queries:
select 1, 'abc';
select (1, 'abc');
The first query returns one row with two columns with values 1 and 'abc'. The second one returns a row with one column of pseudo-type record with value (1, 'abc').
Your query tries to create a table with one column of pseudo-type record. This is impossible and should end with
ERROR: column "row" has pseudo-type record
SQL state: 42P16
Just remove brackets from your query.
As a_horse stated, [25P02] ERROR does not apply to the query in question.
Btw, my advice: never use keywords as table/column names.

can't insert records in new table from existing table in sql server 2005

I need to insert records into a new table from an existing table. I used the following query to do this:
Insert into Newtable
Select * from Oldtable where date1 = #date
This query works most of the time, but in one scenario I get 10 million records to be inserted for the date1 value. In this case I'm getting the following error message:
Error : The transaction log for database "tempDB" is full. To find out why space in the log cannot be reused, see the log_reuse_wait_desc column in sys.databases
Should I break the query into parts and insert them sequentially, or is there a way to do this with the current query?
This is, perhaps, a distasteful suggestion. But, you can try exporting the data to a file and then inserting using bulk-insert, with database logging set to SIMPLE or BULK-LOGGED.
More information is at http://msdn.microsoft.com/en-us/library/ms190422.aspx.

A trigger to find the sum of one field in a different table and error if it's over a certain value in oracle

I have two tables
moduleprogress which contains fields:
studentid
modulecode
moduleyear
modules which contains fields:
modulecode
credits
I need a trigger to run when the user is attempting to insert or update data in the moduleprogress table.
The trigger needs to:
look at the studentid that the user has input and look at all modules that they have taken in moduleyear "1".
take the modulecode the user input and look at the modules table and find the sum of the credits field for all these modules (each module is worth 10 or 20 credits).
if the value is above 120 (yearly credit limit) then it needs to error; if not, input is ok.
Does this make sense? Is this possible?
#a_horse_with_no_name
This looks like it will work but I will only be using the database to input data manually so it needs to error on input. I'm trying to get a trigger similar to this to solve the problem(trigger doesn't work) and forget that "UOS_" is before everything. Just helps me with my database and other functions.
CREATE OR REPLACE TRIGGER "UOS_TESTINGS"
BEFORE UPDATE OR INSERT ON UOS_MODULE_PROGRESS
REFERENCING NEW AS NEW OLD AS OLD
DECLARE
MODULECREDITS INTEGER;
BEGIN
SELECT
m.UOS_CREDITS,
mp.UOS_MODULE_YEAR,
SUM(m.UOS_CREDITS)
INTO MODULECREDITS
FROM UOS_MODULE_PROGRESS mp JOIN UOS_MODULES m
ON m.UOS_MODULE_CODE = mp.UOS_MODULE_CODE
WHERE mp.UOS_MODULE_YEAR = 1;
IF MODULECREDITS >= 120 THEN
RAISE_APPLICATION_ERROR(-20000, 'Students are only allowed to take upto 120 credits per year');
END IF;
END;
I get the error message :
8 23 PL/SQL: ORA-00947: not enough values
4 1 PL/SQL: SQL Statement ignored
I'm not sure I understand your description, but the way I understand it, this can be solved using a materialized view, which might give better transactional behaviour than the trigger:
CREATE MATERIALIZED VIEW LOG
ON moduleprogress WITH ROWID (modulecode, studentid, moduleyear)
INCLUDING NEW VALUES;
CREATE MATERIALIZED VIEW LOG
ON modules with rowid (modulecode, credits)
INCLUDING NEW VALUES;
CREATE MATERIALIZED VIEW mv_module_credits
REFRESH FAST ON COMMIT WITH ROWID
AS
SELECT pr.studentid,
SUM(m.credits) AS total_credits
FROM moduleprogress pr
JOIN modules m ON pr.modulecode = m.modulecode
WHERE pr.moduleyear = 1
GROUP BY pr.studentid;
ALTER TABLE mv_module_credits
ADD CONSTRAINT check_total_credits CHECK (total_credits <= 120)
But: depending on the size of the table this might however be slower than a pure trigger based solution.
The only drawback of this solution is, that the error will be thrown at commit time, not when the insert happens (because the MV is only refreshed on commit, and the check constraint is evaluated then)