Call sqlcl script hosted in a private repo in github - oracle

I can successfully do this:
sql my_connection_string #script1.sql
However, I am struggling to make this work, using a script hosted in a private repo.
sql my_connection_string https://my_private_repo_url/script1.sql
How can I make this work in a single line?

You've got it.
sql user/password#server:ip/service #https://internet.com/script.sql
Here's a public SQL file, courtesy Kris.
c:\sqlcl\22.4\sqlcl\bin>sql hr/oracle#localhost:1523/orclpdb1 #https://gist.githubusercontent.com/krisrice/68e23a2101fe10c8efc26371b8c59d5c/raw/e5a2512d4f74dc79d37e4328e36b81fba15d36a3/dbms_cloud_metric_sql_pkg.sql
SQLcl: Release 22.4 Production on Mon Feb 13 08:18:39 2023
Copyright (c) 1982, 2023, Oracle. All rights reserved.
Last Successful login time: Mon Feb 13 2023 08:18:20 -05:00
Connected to:
Oracle Database 23c Enterprise Edition Release 23.0.0.0.0 - Beta
Version 23.1.0.0.0
login.sql found in the CWD. DB access is restricted for login.sql.
Adjust the SQLPATH to include the path to enable full functionality.
Package OCI_METRICS compiled
Package Body OCI_METRICS compiled
LINE/COL ERROR
--------- -------------------------------------------------------------
0/0 PL/SQL: Compilation unit analysis terminated
12/10 PLS-00201: identifier 'DBMS_CLOUD_TYPES.RESP' must be declared
Errors: check compiler log
Error starting at line : 149 File # https://gist.githubusercontent.com/krisrice/68e23a2101fe10c8efc26371b8c59d5c/raw/e5a2512d4f74dc79d37e4328e36b81fba15d36a3/dbms_cloud_metric_sql_pkg.sql
In command -
begin
oci_metrics.setup('OCI$RESOURCE_PRINCIPAL', 'us-phoenix-1');
for r in ( /* MAIN QUERY for metrics Should only need to adjust this section to adhere to this structure
Expected Columns:
NAMESPACE - The namespace of the metric : 'my_namespace'
COMPARTMENT_ID - The OCID of the compartment to post the metric : 'ocid1.compartment....'
RESOURCE_GROUP - The resource group of the metric : 'my_resource_group'
NAME - The name of the metric : 'my_metric_name'
DIMENSIONS - The dimensions of the metric in json format : '{"factioid":"this","other":"else"}'
VALUE - The numberic value of the metric : 123.45
*/
select 'a_namespace' namespace,
'ocid1.compartment.oc1..aaaaaaaacw2ft7eu33tlaoppsu6mck7qn2wsqefuixcjhza6xhhsbnhvjorq' compartment_id,
'sample_resource_group' resource_group,
'A_SAMPLE_METRIC' name,
'{"machine":"'|| machine ||'","username":"' ||username ||'"}' dimensions,
count(1) value
from v$session
group by username,machine
/* END MAIN QUERY for metrics */
) loop
oci_metrics.addMetric(r.namespace, r.compartment_id, r.resource_group, r.name, r.dimensions, r.value, systimestamp);
end loop;
-- send any remaining metrics
oci_metrics.sendBatch;
end;
Error report -
ORA-04063: package body "HR.OCI_METRICS" has errors
ORA-06508: PL/SQL: could not find program unit being called: "HR.OCI_METRICS"
ORA-06512: at line 2
04063. 00000 - "%s has errors"
*Cause: Attempt to execute a stored procedure or use a view that has
errors. For stored procedures, the problem could be syntax errors
or references to other, non-existent procedures. For views,
the problem could be a reference in the view's defining query to
a non-existent table.
Can also be a table which has references to non-existent or
inaccessible types.
*Action: Fix the errors and/or create referenced objects as necessary.
SQL>
1:0 ¦ HR ¦ localhost:1523/orclpdb1 ¦ viins ¦ None ¦ 00:00:00.347
p_comments => 'see if we can say Hello!',
p_source =>
'select ''Aloha!'' HI_GREETING from dual'
);
COMMIT;
END;
SQL>

Related

How to check if i have the rights to create functions?

So i'm trying to create a stored function but i keep getting this error:
Error report -
ORA-00604: error occurred at recursive SQL level 1
ORA-20900: No access to modify schema
ORA-06512: at line 3
00604. 00000 - "error occurred at recursive SQL level %s"
*Cause: An error occurred while processing a recursive SQL statement
(a statement applying to internal dictionary tables).
*Action: If the situation described in the next error on the stack
can be corrected, do so; otherwise contact Oracle Support.
Fact is, 2-3 hours ago i created some functions and worked like a charm. I can't really understand what is happenning.
Code here (i don't know if this is relevant, as the function doesnt even go to compile)
CREATE OR REPLACE FUNCTION wsxsxfunct(x_data number)
return ECHIPE.id_echipa%type IS
y ECHIPE.id_echipa%type;
BEGIN
SELECT ID_ECHIPA INTO y
FROM ECHIPE E, PILOTI P, REZULTATE R, CURSE C
WHERE P.ID_PILOT = R.ID_PILOT
AND E.ID_ECHIPA = P.ECHIPA
AND R.ID_CURSA = C.ID_CURSA
AND EXTRACT(MONTH FROM C.DATA_CURSA) = x_data;
RETURN y;
EXCEPTION
WHEN NO_DATA_FOUND
THEN dbms_output.put_line('Nu a avut loc nicio cursa in luna ' || x_data);
RAISE_APPLICATION_ERROR(-20000,
'Nu exista angajati cu numele dat');
WHEN TOO_MANY_ROWS
THEN dbms_output.put_line('Au avut loc mai multe curse in luna ' || x_data);
RAISE_APPLICATION_ERROR(-20001,
'Exista mai multi angajati cu numele dat');
WHEN OTHERS
THEN dbms_output.put_line('Trebuie sa apelezi functia cu un numar intre 1-12, reprezentand numarul lunii');
RAISE_APPLICATION_ERROR(-20002,'Alta eroare!');
END;
/
Ignore those messages from the code. thanks
EDIT:
I tried old codes from the functions i created a few hours ago, and i'm still getting the same error report.
ORA-20900 (and generally errors between 20000 and 20999) are 'custom' errors, namely, they come from a call to RAISE_APPLICATION_ERROR.
Thus this is not a (native) privileges issue. Most likely is that an administrator has created a DDL trigger which blocks your attempt to create a function (and potentially other objects).
Speak to your DBA.

Oracle SQL*Loader WHEN Clause Raising Error 2

I am trying to exclude the last line of a data file using SQL*Loader, using the WHEN clause, but when it gets to that line it populates both the bad and discard file, and raises an Error 2.
The line to ignore is the last line and starts with "NOL".
After some reading, Error 2 is a warning about the synatx of the CTL file, but cannot find out where it is wrong. Note, if l remove the last line and then run the SAME CTL file, no ERROR is raised, so the issue cannot be the synatx of the CTL file.
To resolve the issue, l am removing the last line BEFORE loading the data, but would like to find out what the issue is for any future use of the WHEN clause.
I have tried:
file_dt != 'NOL'
(1:1) != 'N'
. . .
But l get the same Error 2.
Has anybody else come across this issue? Or have something that l can try?
Oracle Docs
SQL*Loader Command-Line Reference
For UNIX, the exit codes are as follows:
EX_SUCC 0
EX_FAIL 1
EX_WARN 2
EX_FTL 3
Data:
File-Date,Number
2021-05-04,24
2021-05-04,24
2021-05-04,24
2021-05-04,24
NOL: 4
CTL File:
OPTIONS (READSIZE=51200001, BINDSIZE=51200000, ROWS=5000, ERRORS=0, SKIP=1)
load data
append
into table SOME_SCHEMA.SOME_TABLE
WHEN (01) 'NOL'
fields terminated by ';'
OPTIONALLY ENCLOSED BY '"' AND '"'
trailing nullcols
(
file_dt DATE "YYYY-MM-DD",
a_number
)
Result:
Path used: Conventional
Commit point reached - logical record count 5
Table SOME_SCHEMA.SOME_TABLE:
4 Rows successfully loaded.
Check the log file:
loading-file.log
for more information about the load.
2021-05-06 09:42:12: Finished Loading Data into Table
2021-05-06 09:42:12: Status: 2
I'm not on Unix. Nonetheless, loading should work the same.
Table:
SQL> desc test
Name Null? Type
----------------------------------------------------- -------- --------------------------
FILE_DT DATE
A_NUMBER NUMBER
SQL>
Control file (I included sample data into it, for simplicity):
OPTIONS (READSIZE=51200001, BINDSIZE=51200000, ROWS=5000, ERRORS=0, SKIP=1)
load data
infile *
replace
into table test
WHEN (01) <> 'NOL'
fields terminated by ','
trailing nullcols
(
file_dt DATE "YYYY-MM-DD",
a_number
)
begindata
File-Date,Number
2021-05-04,24
2021-05-04,24
2021-05-04,24
2021-05-04,24
NOL: 4
Loading session and result:
SQL> $sqlldr scott/tiger#orcl control=test38.ctl log=test38.log
SQL*Loader: Release 11.2.0.1.0 - Production on ╚et Svi 6 11:38:00 2021
Copyright (c) 1982, 2009, Oracle and/or its affiliates. All rights reserved.
Commit point reached - logical record count 4
SQL> select * from test;
FILE_DT A_NUMBER
------------------- ----------
04.05.2021 00:00:00 24
04.05.2021 00:00:00 24
04.05.2021 00:00:00 24
04.05.2021 00:00:00 24
Seems to be OK.
So, what did I do differently?
modified WHEN clause
fields are terminated by comma, not semi-colon
removed superfluous information

Oracle / SQL Tuning Advisor

Can an STA work on a query that is not even run once in the database? I mean, if it's not there in the cache, can SQL tuning advisor get some result for us?
SQL Tuning Advisor can work on queries that have never been run before. I'm not sure what interface you're using, but the DBMS_SQLTUNE package makes it easy to pass in the SQL text.
For example, let's create a simple table with no indexes:
--Create a simple table with no indexes.
create table table_missing_index(a number);
insert into table_missing_index
select level from dual connect by level <= 100000;
begin
dbms_stats.gather_table_stats(user, 'TABLE_MISSING_INDEX');
end;
/
Then pass in a query that could obviously benefit from an index:
--Create, execute, and display the tuning task.
declare
v_task varchar2(64);
begin
v_task := dbms_sqltune.create_tuning_task
(
sql_text => 'select a from table_missing_index where a = 1'
);
dbms_sqltune.execute_tuning_task(task_name => v_task);
dbms_output.put_line('Task name: '||v_task);
end;
/
Grab the task name from the output, and plug it into this query to see the results:
select dbms_sqltune.report_tuning_task('TASK_362') from dual;
The output should look something like this:
GENERAL INFORMATION SECTION
-------------------------------------------------------------------------------
Tuning Task Name : TASK_362
Tuning Task Owner : JHELLER
Workload Type : Single SQL Statement
Scope : COMPREHENSIVE
Time Limit(seconds): 1800
Completion Status : COMPLETED
Started at : 04/04/2021 14:26:15
Completed at : 04/04/2021 14:26:15
-------------------------------------------------------------------------------
Schema Name : JHELLER
Container Name: ORCLPDB
SQL ID : 0g6v1x1c7kcjt
SQL Text : select a from table_missing_index where a = 1
-------------------------------------------------------------------------------
FINDINGS SECTION (1 finding)
-------------------------------------------------------------------------------
1- Index Finding (see explain plans section below)
--------------------------------------------------
The execution plan of this statement can be improved by creating one or more
indices.
Recommendation (estimated benefit: 98.56%)
------------------------------------------
- Consider running the Access Advisor to improve the physical schema design
or creating the recommended index.
create index JHELLER.IDX$$_016A0001 on JHELLER.TABLE_MISSING_INDEX("A");
... [removed rest of the large report] ...

Getting Unknown Command error on IF-THEN-ELSE

I have the following query that I am using in Oracle 11g
IF EXISTS (SELECT * FROM EMPLOYEE_MASTER WHERE EMPID='ABCD32643')
THEN
update EMPLOYEE_MASTER set EMPID='A62352',EMPNAME='JOHN DOE',EMPTYPE='1' where EMPID='ABCD32643' ;
ELSE
insert into EMPLOYEE_MASTER(EMPID,EMPNAME,EMPTYPE) values('A62352','JOHN DOE','1') ;
END IF;
On running the statement I get the following output:
Error starting at line : 4 in command -
ELSE
Error report -
Unknown Command
1 row inserted.
Error starting at line : 6 in command -
END IF
Error report -
Unknown Command
The values get inserted with error when I run it directly. But when I try to execute this query through my application I get an oracle exception because of the error generated :
ORA-00900: invalid SQL statement
And hence the values are not inserted.
I am relatively new to Oracle. Please advise on what's wrong with the above query so that I could run this query error free.
If MERGE doesn't work for you, try the following:
begin
update EMPLOYEE_MASTER set EMPID='A62352',EMPNAME='JOHN DOE',EMPTYPE='1'
where EMPID='ABCD32643' ;
if SQL%ROWCOUNT=0 then
insert into EMPLOYEE_MASTER(EMPID,EMPNAME,EMPTYPE)
values('A62352','JOHN DOE','1') ;
end if;
end;
Here you you the update on spec, then check whether or not you found a matching row, and insert in case you didn't.
"what's wrong with the above query "
What's wrong with the query is that it is not a query (SQL). It should be a program snippet (PL/SQL) but it isn't written as PL/SQL block, framed by BEGIN and END; keywords.
But turning it into an anonymous PL/SQL block won't help. Oracle PL/SQL does not support IF EXISTS (select ... syntax.
Fortunately Oracle SQL does support MERGE statement which does the same thing as your code, with less typing.
merge into EMPLOYEE_MASTER em
using ( select 'A62352' as empid,
'JOHN DOE' as empname,
'1' as emptype
from dual ) q
on (q.empid = em.empid)
when not matched then
insert (EMPID,EMPNAME,EMPTYPE)
values (q.empid, q.empname, q.emptype)
when matched then
update
set em.empname = q.empname, em.emptype = q.emptype
/
Except that you're trying to update empid as well. That's not supported in MERGE. Why would you want to change the primary key?
"Does this query need me to add values to all columns in the table? "
The INSERT can have all the columns in the table. The UPDATE cannot change the columns used in the ON clause (usually the primary key) because that's a limitation of the way MERGE works. I think it's the same key preservation mechanism we see when updating views. Find out more.

Oracle - Selecting from remote database taken from PL/SQL parameter

I am creating a procedure to merge two tables across different database instances. The DB_FROM and the DB_TO are given as parameters to the procedure. Everything else is hardcoded.
PROCEDURE MERGE_TABLE_1(DB_FROM, DB_TO) AS
BEGIN
MERGE INTO TABLE_1#DB_TO DSTN
USING (SELECT * FROM TABLE_1#DB_FROM) SRC
ON (DSTN.ID = SRC.ID)
WHEN MATCHED THEN
WHEN NOT MATCHED THEN
END MERGE_TABLE_1
I get the below error when I attempt to compile
Error(1): ORA-04052: error occurred when looking up remote object
TABLE_1#DB_TO ORA-00604: error occurred at recursive SQL level 1
ORA-02019: connection description for remote database not found
Nope, it will not work that way. You cannot use variables as table names, column names or db link names in static SQL. You can achieve what you want using dynamic sql:
EXECUTE IMMEDIATE
'MERGE INTO TABLE_1#' || DB_TO || ' DSTN
USING (SELECT * FROM TABLE_1#' || DB_FROM || ') SRC
ON (DSTN.ID = SRC.ID)
WHEN MATCHED THEN
WHEN NOT MATCHED THEN ...';

Resources