Connecting oracle through Batch - oracle10g

I am trying to connect to oracle and execute one query
I have created abc.BAT
#ECHO off
sqlplus -S scott/tiger#orcl
ECHO SPOOL text.out
select * from emp;
ECHO SPOOL OFF
But i am not able to get any output when i execute abc.bat ,Whats wrong in this?

I suspect that you want to separate the SQL*Plus commands from the BAT file.
For example, if you create a SQL*Plus script (abc.sql)
C:\Users\jcave>type abc.sql
spool text.out;
select * from emp;
spool off;
exit;
And then create a batch file that calls SQL*Plus and passes in the SQL*Plus script you just created (abc.bat)
C:\Users\jcave>type abc.bat
sqlplus scott/tiger #abc.sql
Then when you invoke the BAT file, you'll get the output you want
C:\Users\jcave>abc.bat
C:\Users\jcave>sqlplus scott/tiger #abc.sql
SQL*Plus: Release 11.2.0.1.0 Production on Tue Jan 24 14:37:17 2012
Copyright (c) 1982, 2010, Oracle. All rights reserved.
Connected to:
Oracle Database 11g Enterprise Edition Release 11.2.0.1.0 - 64bit Production
With the Partitioning, OLAP, Data Mining and Real Application Testing options
EMPNO ENAME JOB MGR HIREDATE SAL COMM
---------- ---------- --------- ---------- --------- ---------- ----------
DEPTNO
----------
7369 SMITH CLERK 7902 17-DEC-80 801
20
7499 ALLEN SALESMAN 7698 20-FEB-81 1601 300
30
7521 WARD SALESMAN 7698 22-FEB-81 1251 500
30
EMPNO ENAME JOB MGR HIREDATE SAL COMM
---------- ---------- --------- ---------- --------- ---------- ----------
DEPTNO
----------
7566 JONES MANAGER 7839 02-APR-81 2976
20
7654 MARTIN SALESMAN 7698 28-SEP-81 1251 1400
30
7698 BLAKE MANAGER 7839 01-MAY-81 2851
30
EMPNO ENAME JOB MGR HIREDATE SAL COMM
---------- ---------- --------- ---------- --------- ---------- ----------
DEPTNO
----------
7782 CLARK MANAGER 7839 09-JUN-81 2451
10
7788 SCOTT ANALYST 7566 19-APR-87 3001
20
7839 KING PRESIDENT 17-NOV-81 5001
10
EMPNO ENAME JOB MGR HIREDATE SAL COMM
---------- ---------- --------- ---------- --------- ---------- ----------
DEPTNO
----------
7844 TURNER SALESMAN 7698 08-SEP-81 1501 0
30
7876 ADAMS CLERK 7788 23-MAY-87 1101
20
7900 JAMES CLERK 7698 03-DEC-81 951
30
EMPNO ENAME JOB MGR HIREDATE SAL COMM
---------- ---------- --------- ---------- --------- ---------- ----------
DEPTNO
----------
7902 FORD ANALYST 7566 03-DEC-81 3001
20
7934 MILLER CLERK 7782 23-JAN-82 1301
10
14 rows selected.
Disconnected from Oracle Database 11g Enterprise Edition Release 11.2.0.1.0 - 64bit Production
With the Partitioning, OLAP, Data Mining and Real Application Testing options
C:\Users\jcave>type text.out
EMPNO ENAME JOB MGR HIREDATE SAL COMM
---------- ---------- --------- ---------- --------- ---------- ----------
DEPTNO
----------
7369 SMITH CLERK 7902 17-DEC-80 801
20
7499 ALLEN SALESMAN 7698 20-FEB-81 1601 300
30
7521 WARD SALESMAN 7698 22-FEB-81 1251 500
30
EMPNO ENAME JOB MGR HIREDATE SAL COMM
---------- ---------- --------- ---------- --------- ---------- ----------
DEPTNO
----------
7566 JONES MANAGER 7839 02-APR-81 2976
20
7654 MARTIN SALESMAN 7698 28-SEP-81 1251 1400
30
7698 BLAKE MANAGER 7839 01-MAY-81 2851
30
EMPNO ENAME JOB MGR HIREDATE SAL COMM
---------- ---------- --------- ---------- --------- ---------- ----------
DEPTNO
----------
7782 CLARK MANAGER 7839 09-JUN-81 2451
10
7788 SCOTT ANALYST 7566 19-APR-87 3001
20
7839 KING PRESIDENT 17-NOV-81 5001
10
EMPNO ENAME JOB MGR HIREDATE SAL COMM
---------- ---------- --------- ---------- --------- ---------- ----------
DEPTNO
----------
7844 TURNER SALESMAN 7698 08-SEP-81 1501 0
30
7876 ADAMS CLERK 7788 23-MAY-87 1101
20
7900 JAMES CLERK 7698 03-DEC-81 951
30
EMPNO ENAME JOB MGR HIREDATE SAL COMM
---------- ---------- --------- ---------- --------- ---------- ----------
DEPTNO
----------
7902 FORD ANALYST 7566 03-DEC-81 3001
20
7934 MILLER CLERK 7782 23-JAN-82 1301
10
14 rows selected.

Related

How to drop a function that is a part of a module?

I'm using the DB2 10.1 fixpack 1 Enterprise Edition on Linux.
I would like to drop function
I checked if the function exists with:
select * from syscat.functions where funcschema = 'TEST' and funcname = 'REC1'
and have got the output:
ROUTINESCHEMA ROUTINEMODULENAME ROUTINENAME ROUTINETYPE OWNER OWNERTYPE SPECIFICNAME ROUTINEID ROUTINEMODULEID RETURN_TYPESCHEMA RETURN_TYPEMODULE RETURN_TYPENAME ORIGIN FUNCTIONTYPE PARM_COUNT LANGUAGE DIALECT SOURCESCHEMA SOURCESPECIFIC PUBLISHED DETERMINISTIC EXTERNAL_ACTION NULLCALL CAST_FUNCTION ASSIGN_FUNCTION SCRATCHPAD SCRATCHPAD_LENGTH FINALCALL PARALLEL PARAMETER_STYLE FENCED SQL_DATA_ACCESS DBINFO PROGRAMTYPE COMMIT_ON_RETURN AUTONOMOUS RESULT_SETS SPEC_REG FEDERATED THREADSAFE VALID MODULEROUTINEIMPLEMENTED METHODIMPLEMENTED METHODEFFECT TYPE_PRESERVING WITH_FUNC_ACCESS OVERRIDDEN_METHODID SUBJECT_TYPESCHEMA SUBJECT_TYPENAME CLASS JAR_ID JARSCHEMA JAR_SIGNATURE CREATE_TIME ALTER_TIME FUNC_PATH QUALIFIER IOS_PER_INVOC INSTS_PER_INVOC IOS_PER_ARGBYTE INSTS_PER_ARGBYTE PERCENT_ARGBYTES INITIAL_IOS INITIAL_INSTS CARDINALITY SELECTIVITY RESULT_COLS IMPLEMENTATION LIB_ID TEXT_BODY_OFFSET TEXT NEWSAVEPOINTLEVEL DEBUG_MODE TRACE_LEVEL DIAGNOSTIC_LEVEL CHECKOUT_USERID PRECOMPILE_OPTIONS COMPILE_OPTIONS EXECUTION_CONTROL CODEPAGE COLLATIONSCHEMA COLLATIONNAME COLLATIONSCHEMA_ORDERBY COLLATIONNAME_ORDERBY ENCODING_SCHEME LAST_REGEN_TIME INHERITLOCKREQUEST DEFINER SECURE REMARKS
------------- ----------------- ----------- ----------- ----- --------- ------------------ ----------- --------------- ----------------- ----------------- --------------- ------ ------------ ---------- -------- ---------- ------------ -------------- --------- ------------- --------------- -------- ------------- --------------- ---------- ----------------- --------- -------- --------------- ------ --------------- ------ ----------- ---------------- ---------- ----------- -------- --------- ---------- ----- ------------------------ ----------------- ------------ --------------- ---------------- ------------------- ------------------ ---------------- ----- ------ --------- ------------- -------------------------- -------------------------- --------- --------- ------------------------ ------------------------ ------------------------ ------------------------ ---------------- ------------------------ ------------------------ -------------------- ------------------------ ----------- -------------- ----------- ---------------- ---- ----------------- ---------- ----------- ---------------- --------------- ------------------ --------------- ----------------- -------- --------------- ------------- ----------------------- --------------------- --------------- -------------------------- ------------------ ------- ------ -------
TEST IZPISI REC1 F XXXXX U SQL151024081854400 74258 19 TEST IZPISI REC1 S S 1 - - Y Y Y 0 S 0 Y - - - - - - - 2015-10-24-08.18.54.372090 2015-10-24-08.18.54.372090 - TEST -1,00000000000000E+000 -1,00000000000000E+000 -1,00000000000000E+000 -1,00000000000000E+000 -1 -1,00000000000000E+000 -1,00000000000000E+000 -1 -1,00000000000000E+000 1 - - -1 - 0 0 - - - 912 SYSIBM UNIQUE SYSIBM UNIQUE 2015-10-24-08.18.54.372090 XXXXX Y -
I tried to drop a function with:
drop function TEST.REC1
And I got the error:
SQL0204N "TEST.REC1" is an undefined name. SQLSTATE=42704
How to drop this function?
Try
ALTER MODULE IZPISI DROP FUNCTION TEST.REC1
https://www.ibm.com/support/knowledgecenter/en/SSEPGG_11.5.0/com.ibm.db2.luw.sql.ref.doc/doc/r0054778.html

Oracle 12c performance issue

I have some performance issue when querying on dictionary views. When using RULE hint the query is very fast. What can be the problem ? I have optimizer_mode=RULE in init.ora. Oracle 12.1.0.2.0 with p26635880_121020_Linux-x86-64.zip patch applied
SQL> SET TIMING ON;
SQL>
SQL> select /*+ RULE */
2 *
3 from
4 v$locked_object lo,
5 all_objects ao,
6 v$lock ll
7 where
8 lo.object_id = ao.object_id
9 and lo.xidsqn = ll.id2
10 and ao.data_object_id = dbms_rowid.rowid_object('AAAgW6AACAAM7UiAAA');
XIDUSN XIDSLOT XIDSQN OBJECT_ID SESSION_ID ORACLE_USERNAME OS_USER_NAME PROCESS LOCKED_MODE CON_ID OWNER OBJECT_NAME SUBOBJECT_NAME OBJECT_ID DATA_OBJECT_ID OBJECT_TYPE CREATED LAST_DDL_TIME TIMESTAMP STATUS TEMPORARY GENERATED SECONDARY NAMESPACE EDITION_NAME SHARING EDITIONABLE ORACLE_MAINTAINED ADDR KADDR SID TYPE ID1 ID2 LMODE REQUEST CTIME BLOCK CON_ID
---------- ---------- ---------- ---------- ---------- ------------------------------ ------------------------------ ------------------------ ----------- ---------- -------------------------------------------------------------------------------- -------------------------------------------------------------------------------- -------------------------------------------------------------------------------- ---------- -------------- ----------------------- ----------- ------------- ------------------- ------- --------- --------- --------- ---------- -------------------------------------------------------------------------------- ------------- ----------- ----------------- ---------------- ---------------- ---------- ---- ---------- ---------- ---------- ---------- ---------- ---------- ----------
Executed in 0,766 seconds
SQL>
SQL> select
2 *
3 from
4 v$locked_object lo,
5 all_objects ao,
6 v$lock ll
7 where
8 lo.object_id = ao.object_id
9 and lo.xidsqn = ll.id2
10 and ao.data_object_id = dbms_rowid.rowid_object('AAAgW6AACAAM7UiAAA');
XIDUSN XIDSLOT XIDSQN OBJECT_ID SESSION_ID ORACLE_USERNAME OS_USER_NAME PROCESS LOCKED_MODE CON_ID OWNER OBJECT_NAME SUBOBJECT_NAME OBJECT_ID DATA_OBJECT_ID OBJECT_TYPE CREATED LAST_DDL_TIME TIMESTAMP STATUS TEMPORARY GENERATED SECONDARY NAMESPACE EDITION_NAME SHARING EDITIONABLE ORACLE_MAINTAINED ADDR KADDR SID TYPE ID1 ID2 LMODE REQUEST CTIME BLOCK CON_ID
---------- ---------- ---------- ---------- ---------- ------------------------------ ------------------------------ ------------------------ ----------- ---------- -------------------------------------------------------------------------------- -------------------------------------------------------------------------------- -------------------------------------------------------------------------------- ---------- -------------- ----------------------- ----------- ------------- ------------------- ------- --------- --------- --------- ---------- -------------------------------------------------------------------------------- ------------- ----------- ----------------- ---------------- ---------------- ---------- ---- ---------- ---------- ---------- ---------- ---------- ---------- ----------
Executed in 83,39 seconds
SQL>
optimizer_adaptive_plans TRUE
optimizer_adaptive_statistics FALSE
db_file_multiblock_read_count 128
optimizer_features_enable 12.1.0.2
optimizer_mode RULE
optimizer_index_cost_adj 100
pga_aggregate_target 13474201600
workarea_size_policy AUTO
optimizer_dynamic_sampling 0
_optimizer_aggr_groupby_elim FALSE
optimizer_adaptive_reporting_only TRUE
optimizer_adaptive_features TRUE
_optimizer_reduce_groupby_key FALSE
13 rows selected
Executed in 0,156 seconds
SQL>

Extracting each line from a text after a certain character and extract the result to a table using postgres

I have a text looking like this:
>Sequenz: Test 1
MTEITAAMVKELRESTGAGMMDCKNALSETNGDFDKAVQLLREKGLGKAAKKADRLAAEG
>Sequenz 2 1234 Organism: Treponema
MTEITAAMVKELRESTGAGMMDCKNALSETNGDFDKAVQLLREKGLGKAAKKADRLAAEG
>Sequenz 3
MTEITAAMVKELRESTGAGMMDCKNALSETNGDFDKAVQLLREKGLGKAAKKADRLAAEG
There is not necessarily a blank line between the text chunks and it might be a various number of lines of 'MTEITAAMVKELRESTGAGM'. The only thing, which is known for sure is a > before each line.
I want resulting in a table like this:
HEADER
----------
Sequenz: Test 1
----------
Sequenz 2 1234 Organism: Treponema
----------
Sequenz 3
I tried:
SELECT regexp_matches(regexp_split_to_table( 'text from above', '\n>'),'([A-Z,a-z,0-9]+\s)');
resulting in
HEADER
----------
Sequenz
----------
Sequenz
----------
Sequenz
and
Select regexp_split_to_table('text from bove', '[\\\n>+(.)\\\n]+')
resulting in
HEADER
----------
----------
Sequenz: Test 1
----------
MTEITAAMVKELRESTGAGMMDCKNALSETNGDFDKAVQLLREKGLGKAAKKADRLAAEG
----------
----------
Sequenz 2 1234 Organism: Treponema
----------
MTEITAAMVKELRESTGAGMMDCKNALSETNGDFDKAVQLLREKGLGKAAKKADRLAAEG
----------
----------
Sequenz 3
----------
MTEITAAMVKELRESTGAGMMDCKNALSETNGDFDKAVQLLREKGLGKAAKKADRLAAEG
Try this:
SELECT split_part(regexp_split_to_table(trim(leading '>' from '>Sequenz: Test 1
MTEITAAMVKELRESTGAGMMDCKNALSETNGDFDKAVQLLREKGLGKAAKKADRLAAEG
>Sequenz 2 1234 Organism: Treponema
MTEITAAMVKELRESTGAGMMDCKNALSETNGDFDKAVQLLREKGLGKAAKKADRLAAEG
>Sequenz 3
MTEITAAMVKELRESTGAGMMDCKNALSETNGDFDKAVQLLREKGLGKAAKKADRLAAEG'), E'>'),E'\n', 1) AS res
Remove trim() function if you want to keep first empty row.
Demo: http://rextester.com/LQXY98290

Merging pairs of rows with one DateTime column into single rows with start and end date columns

EDIT: Much needed clarification regarding the fact that a name can have many pairs of non-overlapping starts and ends. So a merged pairs can be identified as "Run".
I'm guessing this is a duplicate question but I can't seem to find the other one. If someone can point me to it, then this can be closed or deleted or whatever.
I have a table that looks like this:
Id Name Action ActionDate
---- ------- ------ ----------
1 John Start 01/15/2013
2 Mary Start 01/17/2013
3 Nancy Start 01/17/2013
4 John End 01/18/2013
5 Mary End 01/18/2013
6 Nancy End 01/20/2013
7 John Start 01/19/2013
8 Mary Start 01/20/2013
9 Nancy Start 01/25/2013
10 John End 01/21/2013
11 Mary End 01/22/2013
12 Nancy End 01/30/2013
And I need a result set that looks like this using a set based query:
Id Name Run StartActionDate EndActionDate
---- ------- ---- --------------- -------------
1 John 1 01/15/2013 01/18/2013
2 John 2 01/19/2013 01/21/2013
3 Mary 1 01/17/2013 01/18/2013
4 Mary 2 01/20/2013 01/22/2013
5 Nancy 1 01/17/2013 01/20/2013
6 Nancy 2 01/25/2013 01/30/2013
Check this out
select MIN(Id),Name,MIN(case Action when 'Start' then ActionDate End) as StartActionDate,MIN(case Action When 'End' then ActionDate End) as EndActiondate from dbo.Table group by Name
Assuming that there's only 1 record with "Start" and 1 record with "End" for each Name, something like this should work:
SELECT T1.Id, T1.Name, T1.ActionDate AS [StartActionDate], T2.ActionDate AS [EndActionDate]
FROM MyTable T1
INNER JOIN MyTable T2 ON T1.Name = T2.Name
WHERE T1.Action = 'Start' AND T2.Action = 'End'
While it's not very clear what id you want to return if you have multiple pairs of each name, you should be able to use a windowing function like row_number() to generate a unique sequence for each name with action and then generate the final result:
;with cte as
(
select id, name, action, actiondate,
row_number() over(partition by name, action
order by actiondate) seq
from yourtable
)
select
min(id) id,
name,
max(case when action = 'start' then actiondate end) StartActionDate,
max(case when action = 'end' then actiondate end) EndActionDate
from cte
group by name, seq
order by id;
See SQL Fiddle with Demo
Based on your data changes, you should be able to use something similar to:
;with cte as
(
select id, name, action, actiondate,
row_number() over(partition by name, action
order by actiondate) seq
from yourtable
)
select
row_number() over(order by name) id,
name,
seq run,
max(case when action = 'start' then actiondate end) StartActionDate,
max(case when action = 'end' then actiondate end) EndActionDate
from cte
group by name, seq
order by id
See SQL Fiddle with Demo. This gives a result:
| ID | NAME | RUN | STARTACTIONDATE | ENDACTIONDATE |
|----|-------|-----|--------------------------------|--------------------------------|
| 1 | John | 1 | January, 15 2013 00:00:00+0000 | January, 18 2013 00:00:00+0000 |
| 2 | John | 2 | January, 19 2013 00:00:00+0000 | January, 21 2013 00:00:00+0000 |
| 3 | Mary | 1 | January, 17 2013 00:00:00+0000 | January, 18 2013 00:00:00+0000 |
| 4 | Mary | 2 | January, 20 2013 00:00:00+0000 | January, 22 2013 00:00:00+0000 |
| 5 | Nancy | 1 | January, 17 2013 00:00:00+0000 | January, 20 2013 00:00:00+0000 |
| 6 | Nancy | 2 | January, 25 2013 00:00:00+0000 | January, 30 2013 00:00:00+0000 |

Dynamic SQL SELECT column names and related data using UNPIVOT

I have a table called PERSONNELSERVICELEVELS with general info such as ID and Name, but also many columns identified as ServiceLevel% as follows:
Person_ID Last_Name First_Name ServiceLevel1 ServiceLevel2 ServiceLevel3 etc.
--------- --------- ---------- ------------- ------------- -------------
222 Doe John 4 5 NULL
555 Doe Jane 2 6 9
I would like to create a SELECT statement to produce this output:
Person_ID Last_Name First_Name ServiceLevel Level
--------- --------- ---------- ------------ -----
222 Doe John ServiceLevel1 4
222 Doe John ServiceLevel2 5
222 Doe John ServiceLevel3 NULL
555 Doe Jane ServiceLevel1 2
555 Doe Jane ServiceLevel2 6
555 Doe Jane ServiceLevel3 9
Thanks.
To turn columns into rows, so to speak, you might use a UNION, as in:
select Person_ID, Last_Name, First_Name, 'ServiceLevel1' as ServiceLevel, ServiceLevel1 as [Level]
from PERSONNELSERVICELEVELS
union all
select Person_ID, Last_Name, First_Name, 'ServiceLevel2' as ServiceLevel, ServiceLevel2 as [Level]
from PERSONNELSERVICELEVELS
union all
-- etc.
I'm using union all here because union implicitly runs a distinct operation as well, and that is unnecessary here.
You can also do this type of thing with UNPIVOT.