I'm using SQL developer in one of my database classes and I have homework where certain formatting is needed. When I use the commands given by the professor on my SQL Developer I get an error
Here are the commands:
col price format $99,990.99
col quoted_price format $99,990.99
col balance format $99,990.99
col credit_limit format 99,990.99
The errors I get are:
SP2-0246: Illegal FORMAT string "$99,"
SP2-0246: Illegal FORMAT string "$99,"
SP2-0246: Illegal FORMAT string "$99,"
SP2-0246: Illegal FORMAT string "99,"
When I run these same commands on the lab computer it works but the lab uses an outdated version of SQL developer. It's version 4.1.1
Would appreciate any help
The homework is asks us to input a command that would display query results for example a question would be "x" situation
My command will be:
SELECT item_num, price, description, balance
from item;
The query output below will have $ if needed and will have the commas and periods.
If you are storing salary as Number, then you can use to_char(column,'$9,999.99');
SQL> CREATE TABLE EMP
2 (EMPNO NUMBER(4) NOT NULL,
3 ENAME VARCHAR2(10),
4 JOB VARCHAR2(9),
5 MGR NUMBER(4),
6 HIREDATE DATE,
7 SAL NUMBER(7, 2),
8 COMM NUMBER(7, 2),
9 DEPTNO NUMBER(2)
10 );
SQL> INSERT INTO EMP VALUES (7369, 'SMITH', 'CLERK', 7902,TO_DATE('17-DEC-1980', 'DD-MON-YYYY'), 800, NULL, 20);
SQL> select ename, to_char( sal, '$9,999.99' ) "Salary" from emp;
ENAME Salary
---------- ----------
SMITH $800.00
Related
I have a table that contains XML of HUGECLOB data type, I need to extract CLOB data as XML and get some specific XML tag value to insert it into another table.
I used dbms_lob to get XML and the following is my code to insert XML into another table.
create or replace procedure xml_into_table(l_xml in xmltype)
as
begin
insert into emp( EMPNO,ENAME,JOB,MGR,HIREDATE,SAL,COMM,DEPT)
SELECT * FROM xmltable('employees' passing l_xml
columns EMPNO NUMBER PATH ' /employee/empno',
ENAME VARCHAR2 PATH '/employee/ename',
JOB VARCHAR2 PATH '/employee/job',
HIREDATE DATE PATH '/employee/hiredate');
END;
/
Error(7,56): PL/SQL: ORA-00906: missing left parenthesis.
Can some one please guide me, what is the right way for achieving this.
The VARCHAR2 data type needs a size and you are missing the columns MGR, SAL, COMM and DEPT so SELECT * will only get 4 columns and not the 8 you have named in the INSERT.
create or replace procedure xml_into_table(l_xml in xmltype)
as
begin
insert into emp( EMPNO,ENAME,JOB,MGR,HIREDATE,SAL,COMM,DEPT)
SELECT empno,
ename,
job,
NULL,
hiredate,
NULL,
NULL,
NULL
FROM xmltable(
'employees'
passing l_xml
columns
EMPNO NUMBER PATH ' /employee/empno',
ENAME VARCHAR2(200) PATH '/employee/ename',
JOB VARCHAR2(200) PATH '/employee/job',
HIREDATE DATE PATH '/employee/hiredate'
);
END;
/
I am working on a project where I can't connect directly to the database. I wrote a query that get all the data that I need so I can export them. The result of this SQL query have the great data_type but When I export the result, I choose the insert format and at this point all the data_type are switching to string.
Example of the exported data where the number are in the string format wherease in the original database and in the respons of the query, the type is respected :
Insert into MY_TABLE (POSTCODE, NUMBER, ADRESS, CODE, CITY, VALUE) values ('90000', '303', 'HultonLane', '2845', 'BOLTON', '3');
Do you know if there is any way to export the data with the same data_type from the original DB?
Thank for your help, if you need more information I will provide them.
That behavior (or bug?) exists in e.g. SQL Developer version 19.4, but is fixed in 20.2 so - if you're on a lower version, I suggest you upgrade.
Here's an example, based on Scott's DEPT table where DEPTNO column's datatype is NUMBER. Result - when this:
desc dept;
select /*insert*/ * from dept;
is ran as a script - is:
Name Null? Type
------ -------- ------------
DEPTNO NOT NULL NUMBER
DNAME VARCHAR2(20)
LOC VARCHAR2(20)
REM INSERTING into DEPT
SET DEFINE OFF;
Insert into DEPT (DEPTNO,DNAME,LOC) values (10,'ACCOUNTING','NEW YORK');
Insert into DEPT (DEPTNO,DNAME,LOC) values (20,'RESEARCH','DALLAS');
Insert into DEPT (DEPTNO,DNAME,LOC) values (30,'SALES','CHICAGO');
Insert into DEPT (DEPTNO,DNAME,LOC) values (40,'OPERATIONS','BOSTON');
As you can see, all DEPTNO values are numbers. In 19.4, you'd have e.g.
Insert into DEPT (DEPTNO,DNAME,LOC) values ('10','ACCOUNTING','NEW YORK');
('10', a string).
You don't have to do anything about it; the result is the same regardless you run such a select statement, or right-click table name and "Export" data as insert statements.
BTW, our versions match: Mine is 20.2.0.175, build 175.1842.
I'm hoping to dynamically reference last Fridays date in the weekly sales tables in Oracle SQL Developer i.e. SELECT * FROM Sales_DDMMYY
I can do this in SQL Server (DECLARE / SET / EXECUTE) but haven't had any joy with SQL Developer.
Even the ability to create a date variable to be referenced within the code would be a great start.
Stop!
I strongly suggest you not to do that. That's not the way to create a data model. If you have a table which contains values related to different dates, then date should be a column in that table, such as
create table sales
(id number,
datum date,
amount number
);
Insert rows as
insert into sales (id, datum, amount)
select 1, date '2020-06-01', 100 from dual union all
select 2, date '2020-05-13', 240 from dual union all
select 3, date '2020-05-13', 160 from dual;
and use it as
select sum(amount)
from sales
where datum = date '2020-05-13'
That is the way to do it. Naming columns by dates is ... well, close to a suicide.
Aha, now I see: it is a table name that contains dates. Doesn't really matter, my suggestion still stands. Do not do that. Use a date column within a single table.
If you want - and if you can afford it - partition the table on date value. Note that partitioning option exists in Oracle Enterprise Edition which is quite expensive. So - date column it is.
If there's nothing you can do about it, then dynamic SQL it is. For example:
Sample table:
SQL> create table sales_200620 as select * From dept;
Table created.
Function that accepts ddmmyy value as a parameter, composes table name and returns a refcursor:
SQL> create or replace function f_test (par_ddmmyy in varchar2)
2 return sys_refcursor
3 is
4 l_table_name varchar2(30) := 'sales_' || par_ddmmyy;
5 l_rc sys_refcursor;
6 begin
7 open l_rc for 'select * from ' || dbms_assert.sql_object_name(l_table_name);
8 return l_rc;
9 end;
10 /
Function created.
Testing:
SQL> select f_test('200620') from dual;
F_TEST('200620')
--------------------
CURSOR STATEMENT : 1
CURSOR STATEMENT : 1
DEPTNO DNAME LOC
---------- -------------- -------------
10 ACCOUNTING NEW YORK
20 RESEARCH DALLAS
30 SALES CHICAGO
40 OPERATIONS BOSTON
SQL>
Without creating a function: use substitution variable:
SQL> select * From &tn;
Enter value for tn: sales_200620
DEPTNO DNAME LOC
---------- -------------- -------------
10 ACCOUNTING NEW YORK
20 RESEARCH DALLAS
30 SALES CHICAGO
40 OPERATIONS BOSTON
SQL>
Is the character underscore ( '_' ) acceptable (insertable) in varchar2 type column?
There is a value that needs to be inserted into a varchar2 column of one of my tables, but the value contains an underscore, like 'DOC_OTHER'
The value does not get inserted into the varchar2 column, whereas other values which do not contain an underscore character in their values get easily inserted (as usual).
I'm unable to understand this unusual behavior. Suggest!!
Edit:
Since the value is being picked up from a web service response xml, this is how its done.
The sql code is fine. All other values are getting retrieved, plus there is nothing wrong with the path as well.
Looping through the response:
begin
for p in (
select * from
---- some xmlnamespaces of the response ----
columns
"DocData" varchar2(240) path 'path to DocData in response'
,"RepID" varchar2(200) path 'path to RepID in response'
,"DocInit" varchar2(400) path 'path to DocInit in response'
)
loop
begin
insert into pooling_info
(session_id, user_id, DocData, RepID, DocInit)
values(
p_session, p_user, p."DocData", p."RepID", p."DocInit"
);
end;
end loop;
end;
If the value of DocData does not contain an underscore character, it is inserted in the table, this is strange but if the data contains an underscore, the value is not inserted!
It does not throw any error. The surrounding values are inserted except for this (DocData) which contains an underscore
There is nothing out of the ordinary about adding an '_' character(s) to a VARCHAR2 column.
As others have pointed out, the '_' character is a wildcard so it does have a special meaning when used with a LIKE condition.
More than likely, I suspect the problem you are experiencing is either a size issue (e.g. you are trying to place something too large for the VARCHAR2 that you are modifying) or a constraint issue.
Here is an example of modifying a column using the example schema, SCOTT.
SCOTT#dev>
1 CREATE table EMP3 as
2 (SELECT *
3* FROM emp)
SCOTT#dev> /
Table created.
SCOTT#dev> select empno, ename from emp3 where empno = 7369;
EMPNO ENAME
========== ==========
7369 SMITH
1 row selected.
SCOTT#dev> DESC emp3;
Name Null? Type
-------------------------
EMPNO NOT NULL NUMBER (4)
ENAME VARCHAR 2(10)
JOB VARCHAR 2(9)
MGR NUMBER (4)
HIREDATE DATE
SAL NUMBER (7,2)
COMM NUMBER (7,2)
DEPTNO NUMBER (2)
SCOTT#dev> UPDATE emp3
2 SET ename = ename || '_BOB'
3 WHERE empno = 7369
4 ;
1 row updated.
SCOTT#dev> select ename
2 from
3 emp3 where empno = 7369;
ENAME
==========
SMITH_BOB
I have table employees with columns eno, ename, job, sal, comm
and the query like
INSERT a new employee
eno as 7787,
ename as 'abc',
job as 'salesman'
sal as 2000,
comm as tax amount
this tax is the function like
CREATE OR REPLACE FUNCTION tax
( p_sal employees.sal%type
)
RETURN NUMBER
IS
v_tax employees.sal%type;
BEGIN
v_tax:= CASE
WHEN SAL> 4000 THEN SAL * 0.33
WHEN SAL >2500 THEN SAL *0.25
WHEN SAL >1500 THEN SAL * 0.20
ELSE 0
END;
RETURN v_tax
END tax;
At the INSERT statement I can't use function tax for the column comm.
Is there any other method to do this, or how can this be best achieved?
When you say
I can't use function tax for the column comm
do you mean you're not allowed to use this function, or you can't figure out how to use it?
Assuming the latter, I don't see why you shouldn't be able to use a function in an INSERT statement. You have, however, got the syntax of the INSERT statement completely wrong.
Try
INSERT INTO employee (eno, ename, job, sal, comm)
VALUES (7787, 'abc', 'salesman', 2000, tax(2000));
I don't know where amount in your INSERT statement comes from, but given that your function takes a parameter called p_sal, I'm guessing it's applied to the value in the column sal.