I have a table which called Logiciel :
Name Null? Type
---------- -------- -------------
NLOG NOT NULL VARCHAR2(5)
NOMLOG NOT NULL VARCHAR2(20)
DATEACH DATE
VERSION VARCHAR2(7)
TYPELOG VARCHAR2(9)
PRIX NUMBER(6,2)
Using PL/SQL I want to add an entry which has the same data as the entry with NLOG = 'log3', and the value PRIX as the average of PRIX : AVG(PRIX) of all entries.
This is the script I wrote:
DECLARE
unLog LOGICIEL%ROWTYPE;
moy LOGICIEL.PRIX%TYPE;
BEGIN
SELECT AVG(PRIX) INTO moy FROM LOGICIEL;
SELECT * INTO unLog FROM LOGICIEL WHERE NLOG='log5';
unLog.PRIX := moy;
unLog.NLOG := 'logS';
INSERT INTO LOGICIEL SELECT * FROM unLog;
END;
/
The problem is when I execute this script I get this error :
ERROR at line 9:
ORA-06550: line 9, column 37:
PL/SQL: ORA-00942: table or view does not exist
ORA-06550: line 9, column 2:
PL/SQL: SQL Statement ignored
How can I solve this problem ?
Just use insert . . . select:
INSERT INTO LOGICIEL(NLOG, NOMLOG, DATEACH, VERSION, TYPELOG, PRIX)
SELECT NLOG, NOMLOG, DATEACH, VERSION, TYPELOG,
(SELECT AVG(PRIX) FROM LOGICIEL) as PRIX
FROM LOGICIEL
WHERE NLOG = 'log3';
EDIT:
Is this what you mean?
INSERT INTO LOGICIEL(NLOG, NOMLOG, DATEACH, VERSION, TYPELOG, PRIX)
SELECT unlog.NLOG, unlog.NOMLOG, unlog.DATEACH, unlog.VERSION, unlog.TYPELOG, unlog.PRIX
FROM dual;
Related
Using Oracle SQL, I have several SQL Queries with manual bind variables that are used to create a table as follows:
What I would like to do is to change the define anfang to (select c_year_beginning from master_date_automation).
I have tried several combinations of bind variables but could not make it work (edited with DEL comments):
begin
execute immediate 'DROP TABLE A_TEST_TABLE'
end;
/
begin
execute immediate 'create table A_TEST_TABLE (
user varchar2(100),
product varchar2(100),
datum date)';
end;
/
BEGIN
DECLARE
v_c_year_ytd DATE;
BEGIN
SELECT c_year_ytd Into v_c_year_ytd FROM master_date_automation;
BEGIN
SELECT usr.datum || ','
|| usr.user || ','
|| usr.product
INTO A_TEST_TABLE
FROM users_table usr
WHERE usr.datum = v_c_year_ytd
AND kto.kontonummer = '00510199065';
END;
END;
END;
ERROR message (edited after Del comment):
Error report -
ORA-06550: line 12, column 6:
PLS-00403: expression 'A_TEST_TABLE' cannot be used as an INTO-target of a SELECT/FETCH statement
ORA-06550: line 12, column 21:
PL/SQL: ORA-00904: : invalid identifier
ORA-06550: line 9, column 1:
PL/SQL: SQL Statement ignored
06550. 00000 - "line %s, column %s:\n%s"
*Cause: Usually a PL/SQL compilation error.
*Action:
Would be really thankful if someone could help me out.
Thousand thanks in advance!
So, to sum up my comments from above, you don't need PL/SQL to do this. You could do all of your script with straight SQL:
CREATE TABLE A_TEST_TABLE
(
user varchar2(100),
product varchar2(100),
datum date
);
INSERT INTO a_test_table
SELECT usr.user,
usr.product,
usr.datum
FROM users_table
WHERE usr.datum = (SELECT c_year_ytd FROM master_date_automation)
AND usr.user= '123456789'
Drop the table if you need to first.
You don't need to use dynamic SQL inside a PL/SQL block to create the tables; you can do it using SQL statements:
DROP TABLE A_TEST_TABLE;
CREATE TABLE A_TEST_TABLE (
user_name varchar2(100),
product varchar2(100),
datum date
);
Note: USER is a reserved word and you CANNOT use it as an unquoted identifier; you will need to find another identifier such as user_name or you will have to use a quoted identifer and refer to it as "USER" everywhere it is used (however, that is bad practice).
Then if you want to use PL/SQL you can use:
DECLARE
v_c_year_ytd DATE;
BEGIN
SELECT c_year_ytd
Into v_c_year_ytd
FROM master_date_automation;
INSERT INTO a_test_table (datum, user_name, product)
SELECT datum,
user_name
product
FROM users_table
WHERE datum = v_c_year_ytd
AND kontonummer = '00510199065';
END;
/
But it would be much simpler to just use one SQL statement:
INSERT INTO a_test_table (datum, user_name, product)
SELECT datum,
user_name
product
FROM users_table
WHERE datum = (SELECT c_year_ytd FROM master_date_automation)
AND kontonummer = '00510199065';
I wan to create random data in Oracle table:
CREATE TABLE EVENTS(
EVENTID INTEGER NOT NULL,
SOURCE VARCHAR2(50 ),
TYPE VARCHAR2(50 ),
EVENT_DATE DATE,
DESCRIPTION VARCHAR2(100 )
)
/
I tried this:
BEGIN
FOR loop_counter IN 1..1000
LOOP
INSERT INTO EVENTS (EVENTID, SOURCE, TYPE, EVENT_DATE, DESCRIPTION) VALUES (loop_counter, loop_counter, 'warning',
DBMS_RANDOM.VALUE(TO_CHAR(DATE '2000-01-01','J') ,TO_CHAR(DATE '9999-12-31','J')), dbms_random.value(1,100));
END LOOP;
COMMIT;
END;
I get this error exception
Error report - ORA-06550: line 5, column 13: PL/SQL: ORA-00932:
inconsistent datatypes: expected DATE got NUMBER ORA-06550: line 4,
column 1: PL/SQL: SQL Statement ignored
06550. 00000 - "line %s, column %s:\n%s"
*Cause: Usually a PL/SQL compilation error.
*Action:
Can you give me advice how I can fix this issue?
Use:
BEGIN
FOR loop_counter IN 1..1000 LOOP
INSERT INTO "EVENTS" (EVENTID, "SOURCE", TYPE, EVENT_DATE, DESCRIPTION)
VALUES (loop_counter, loop_counter, 'warning',
TO_DATE(TRUNC(DBMS_RANDOM.VALUE(TO_CHAR(DATE '2000-01-01','J') ,TO_CHAR(DATE '9999-12-31','J'))),'J')
,dbms_random.value(1,100)
);
END LOOP;
COMMIT;
END;
SqlFiddleDemo
Changes:
Add mising ; after final END
Quote keywords
Rewrite random date generation
Also, if you use PL/SQL Developer by Allroundautomations, you can find out
good tool for this job: Data Generator.
It can be very useful, because it can help to generate some data
in any types and place it to a tables.
(see screenshot attached)
INSERT INTO EVENTS (EVENTID, "SOURCE", TYPE, EVENT_DATE, DESCRIPTION)
SELECT level, level, 'warning',
TO_DATE(TRUNC(DBMS_RANDOM.VALUE(TO_CHAR(DATE '2000-01-01','J') ,TO_CHAR(DATE '9999-12-31','J'))),'J')
,dbms_random.value(1,100)
FROM DUAL
CONNECT BY LEVEL <= 1000;
I'm trying to learn a bit of PL-SQL using a tutorial by examples book, but one of the suggested codes return the following error when run:
ORA-06550: line 10, column 48: PL/SQL: ORA-00947: not enough values
ORA-06550: line 9, column 1: PL/SQL: SQL Statement ignored
Could you please help me understand what I'm doing wrong?
Many thanks in advance!
Simone.
SQL Fiddle
Oracle 11g R2 Schema Setup:
create table product (code integer primary key, name varchar2 (20), type varchar2(8),price number(4,2),update_dt date);
insert into product values(1,'Mela','Frutta',1,to_date('1-MAY-2015','DD-MON-YYYY'));
insert into product values(2,'Pera','Frutta',2,to_date('2-MAY-2015','DD-MON-YYYY'));
insert into product values(3,'Carota','Ortaggio',3,to_date('3-MAY-2015','DD-MON-YYYY'));
insert into product values(4,'Zucchina','Ortaggio',4,to_date('4-MAY-2015','DD-MON-YYYY'));
insert into product values(5,'Arancia','Frutta',5,to_date('5-MAY-2015','DD-MON-YYYY'));
Query 1:
declare
code_var integer;
type_var varchar2(8);
name_var varchar2(20);
price_var number(4,2);
update_dt_var date;
price_too_high exception;
begin
select code, type,name, price, update_dt
into code_var,type_var,price_var,update_dt_var
from product
where name='Arancia';
if price_var > 4.5 then
raise price_too_high;
end if;
exception
when price_too_high then
dbms_output.put_line('price is too damn high!');
end;
Results:
you are trying to insert 5 values from your select into four variables.
This is correct:
select code, type, name, price, update_dt
into code_var, type_var, name_var, price_var, update_dt_var
from product
where name='Arancia';
I'm using Oracle Database 10g Express Edition Release 10.2.0.1.0 - Production
I've created an object type:
SQL> create or replace type TestObject as Object (
2 id INTEGER,
3 name VARCHAR2(10)
4 );
5 /
Then I create a table containint objets of that type:
SQL> CREATE TABLE TestTable of TestTable;
and I populate the table with some data:
SQL> INSERT INTO TestTable VALUES (10, 'John');
Now, if I want to perform a SELECT in PL/SQL:
SQL> declare
2 aTest TestObject;
3 begin
4 select * into aTest from TestTable;
5 end;
6 /
I get the error ORA-00947 (not enough values):
select * into aTest from TestTable;
*
ERROR en línea 4:
ORA-06550: línea 4, columna 21:
PL/SQL: ORA-00947: no hay suficientes valores
ORA-06550: línea 4, columna 1:
PL/SQL: SQL Statement ignored
But the TestTable contains objects of type TestObject and the variable aTest is also of type TestObject ... ¿Where is the mismatch in the number of values?
For me this worked:
declare
ta TestObject;
begin
select TestObject(t.id, t.name) into ta from TestTable t;
end;
/
Try This..
select TestObject(id,name) from TestTable
/
I have a sales table:
Name Null? Type
SALE_ID NOT NULL NUMBER(4)
SALE_DATE DATE
NO_OF_PRODS NUMBER(4)
PROD_ID NOT NULL NUMBER(4)
CUST_ID NOT NULL NUMBER(4)
DESP_ID NOT NULL NUMBER(4)
SALE_RECEIPT NOT NULL NUMBER(5)
I am trying to insert randomly generated data into the sales table. I am using iSQL plus for oracle. This is just test data that I have to create.
I run the following script to generate the data:
begin
insert into sales
select sale_id_seq.nextval,
sysdate,
trunc(dbms_random.value(000,999)),
p.prod_id, c.cust_id
FROM dba_xy.product p, dba_xy.customer c,
desp_id_seq.nextval,
trunc(dbms_random.value(0000,9999));
end;
/
But when I do, the following error message appears:
trunc(dbms_random.value(0000,9999));
*
ERROR at line 9:
ORA-06550: line 9, column 21:
PL/SQL: ORA-00933: SQL command not properly ended
ORA-06550: line 2, column 2:
PL/SQL: SQL Statement ignored.
What have I done wrong?
I just realised that the DESP_ID is a foreign key within the sales table, but currently the despatch table is empty and when I try to insert data into either tables, I'm not able to cause one table needs the data from the other table. I end up getting this error message:
PL/SQL: ORA-02289: sequence does not exist
You cannot select FROM
FROM dba_xy.product p, dba_xy.customer c,
desp_id_seq.nextval,
trunc(dbms_random.value(0000,9999));
Try:
insert into sales
(select
sale_id_seq.nextval,
sysdate,
trunc(dbms_random.value(000,999)),
p.prod_id,
c.cust_id,
desp_id_seq.nextval,
trunc(dbms_random.value(0000,9999))
FROM dba_xy.product p, dba_xy.customer c;
BTW, are you sure that you want an Cartesian product here, maybe some join is missed ?