Oracle kill sessions procedure - oracle

I want to create procedure which will kill all session. After I run the statement i get error:
[Warning] ORA-24344: success with compilation error 10/13 PL/SQL:
ORA-00942: table or view does not exist 6/6 PL/SQL: SQL Statement
ignored 15/31 PLS-00364: loop index variable 'V_KILL' use is invalid
15/9 PL/SQL: Statement ignored (1: 0): Warning: compiled but with
compilation errors
CREATE OR REPLACE PROCEDURE KILL_ORACLE_SESSIONS
IS
BEGIN
FOR v_kill IN
(SELECT
'alter system kill session '''
||sid||','||serial#||',#1'|| ''' immediate;' as statement
FROM
v$session
WHERE
sql_id='sql_id_here'
)
LOOP
dbms_output.put_line (v_kill.statement);
END LOOP;
END;
/
Where is the catch ?
Thanks

Most likely you don't have permissions to select view v$session because your user received this privilege by a ROLE. Privileges inside a PL/SQL block must be granted directly to the user (i.e. GRANT SELECT ON V$SESSION TO {username};). A role (for example DBA ROLE) does not apply inside PL/SQL.

Related

Oracle: call to a function fails weirdly

If being called under owner account, the following syntax works just fine:
SELECT "MY_OWNER"."MY_PACKAGE".Convert(t.Value) FROM My_Table t
but when called from under another user I get the following error:
ORA-00904: : invalid identifier
00904. 00000 - "%s: invalid identifier"
*Cause:
*Action:
Error at Line: 1 Column: ??
Where Column: ?? points to
SELECT "MY_OWNER"."MY_PACKAGE".Convert(t.Value) FROM My_Table t
⇑
What am I doing wrong?
UPDATE. It's a function that is being called:
FUNCTION Convert ...
You need to grant privileges on this package to the other user
GRANT EXECUTE ON MY_OWNER.MY_PACKAGE TO the_other_user;
You should also check to see whether a_horse_with_no_name is correct. Is this a function that you can call in the way you specify or a procedure which should be called in this way
DECLARE
v_value VARCHAR2(10);
BEGIN
MY_OWNER.MY_PACKAGE.CONVERT(v_value);
END;
Also check the package to see what rights are defined. Current User or package owner. More details can be found here

Ignoring User Exists Error in Oracle

I have created a script that creates Oracle users and grants them roles. I am unable to find a way to ignore the "user exists" error:
ORA-01920: user name '' conflicts with another user or role name.
I understand that when the script is ran, it is possible that the user already exists, but I want to ignore any returned errors. Is this possible?
My Oracle code:
CREATE USER "John" PROFILE "DEFAULT" IDENTIFIED BY "temppassword" ACCOUNT UNLOCK;
Edit:
This question is not asking how to create a user if it doesn't exist. This question is asking how to ignore "the user exists" error. According to a previously asked question, the top answer stated
In general, Oracle scripts simply execute the CREATE statement, and if
the object already exist, you'll get an error indicating that, which
you can ignore. This is whaat all the standard Oracle deployment
scripts do.
It isn't clear how you're running your script, but assuming its via SQL*Plus you can modify the behaviour when an error is encountered with the whenever sqlerror command.
If your script is setting that to exit at the moment, or you're picking that up from a startup script (login.sql, glogin.sql) you can change it back, or modify it temporarily:
...
-- do not stop on error
WHENEVER SQLERROR CONTINUE;
CREATE USER "John" PROFILE "DEFAULT" IDENTIFIED BY "temppassword" ACCOUNT UNLOCK;
-- to stop when later errors are encountered
WHENEVER SQLERROR EXIT FAILURE;
ALTER USER ...
You'll still see the ORA-01920 in the output but it will continue on to execute the next statement. This pattern is also useful for a protective drop of a schema object before attempting to create it.
Why can't you find if the user exists first?
SELECT COUNT(*)
INTO V_count
from ALL_USERS
where username = 'YourUserName'
IF v_count = 0 THEN
--create the user
--execute the grants
ELSE
---log that the user already exists
END IF;
SET SERVEROUTPUT ON;
DECLARE
TYPE t_list IS TABLE OF VARCHAR2 (30);
l_list t_list := t_list ('X0', 'X1', 'X2');
e_user_already_exists EXCEPTION;
PRAGMA EXCEPTION_INIT (e_user_already_exists, -1920);
BEGIN
FOR l_iterator IN 1 .. l_list.COUNT LOOP
DBMS_OUTPUT.PUT_LINE ('Creating user ' || l_list (l_iterator));
BEGIN
EXECUTE IMMEDIATE 'CREATE USER "' || l_list (l_iterator) || '" PROFILE DEFAULT IDENTIFIED BY "WELCOME" ACCOUNT UNLOCK';
EXECUTE IMMEDIATE 'GRANT SOME_APPLICATION_ROLE TO ' || l_list (l_iterator);
EXCEPTION
WHEN e_user_already_exists THEN
DBMS_OUTPUT.PUT_LINE ('User exists, ignored');
WHEN OTHERS THEN
RAISE;
END;
END LOOP;
END;
/

Ubuntu Shell Script for ORACLE db

I'm new to working with ORACLE over SSH (putty) terminal on an Ubuntu OS, my workstation has Windows OS.
I'm programming a shell that connects to the oracle DB then exports a table into a text file and then delete some records depending on a column´s value.
No SQL Plus is installed and this is a must because we need to simulate the infraestructure of our client.
This is part of the script's code:
#!/bin/bash
su oracle
export ORACLE_SID=ORA_SID
export ORACLE_HOME=/uxx/app/oracle/product/11.2.0/client_1
export TWO_TASK=[hostname]:[port]/[ORA_SID]
isql -v [db] [user] [pass] <<EOF
CREATE OR REPLACE DIRECTORY test AS '/xxx/yyy/';
DECLARE
f utl_file.file_type;
BEGIN
f := utl_file.fopen('TEST','sample.txt','W');
for s in (select * from [table]);
loop
utl_file.put_line(f,s);
end loop;
utl_file.fclose(f);
END;
quit;
exit
EOF
When I run the script I got the following output:
SQL> SQLRowCount returns -1
SQL> [S1000][Oracle][ODBC][Ora]ORA-06550: line 1, column 7:
PLS-00103: Encountered the symbol "end-of-file" when expecting one of the following:
begin function pragma procedure subtype type <an identifier>
<a double-quoted delimited-identifier> current cursor delete
exists prior
[ISQL]ERROR: Could not SQLExecute
SQL> [37000][Oracle][ODBC][Ora]ORA-00900: invalid SQL statement
[ISQL]ERROR: Could not SQLExecute
SQL> [S1000][Oracle][ODBC][Ora]ORA-06550: line 1, column 5:
PLS-00103: Encountered the symbol "end-of-file" when expecting one of the following:
( begin case declare exit for goto if loop mod null pragma
raise return select update while with <an identifier>
<a double-quoted delimited-identifier> <a bind variable> <<
continue close current delete fetch lock insert open rollback
savepoint set sql execute commit forall merge pipe purge
[ISQL]ERROR: Could not SQLExecute
SQL> [37000][Oracle][ODBC][Ora]ORA-00900: invalid SQL statement
[ISQL]ERROR: Could not SQLExecute
SQL> [37000][Oracle][ODBC][Ora]ORA-00900: invalid SQL statement
[ISQL]ERROR: Could not SQLExecute
SQL> [37000][Oracle][ODBC][Ora]ORA-00900: invalid SQL statement
Any help would be greatly appreciated.
It resulted SqlPlus was indeed installed on the system but since it has been years since the last time I worked on UNIX like system I didn't know how to make the application work.
I first exported the bin path to my looged session:
export PATH=$ORACLE_HOME/bin:$PATH
Then I could run the sql plus and use the spool tool.
sqlplus usr/pass
Now with SqlPlus I can just use the regular spool command.
Thanks all who helped.

Insufficient Priviledges error when trying to execute the procedure from package

Step 1 : I have created one package with procedures to create context and set value to the context.
create or replace PACKAGE Context_check AS
PROCEDURE set_context_vpd_proc (V_ISID in varchar2);
procedure set_context (v_isid_a in varchar2);
END Context_check;
create or replace PACKAGE BODY Context_check AS
PROCEDURE set_context_vpd_proc (V_ISID in varchar2)
AS
v_STAT VARCHAR2(200);
v_chk varchar2(2000);
BEGIN
DBMS_SESSION.SET_CONTEXT('VPD_CTX', 'ISID', V_ISID );
--v_STAT := '';
EXCEPTION
WHEN NO_DATA_FOUND THEN NULL;
END;
procedure set_context (v_isid_a in varchar2)
as
begin
EXECUTE IMMEDIATE 'CREATE OR REPLACE CONTEXT VPD_CTX using set_context_vpd_proc';
set_context_vpd_proc (v_isid_a);
EXCEPTION
WHEN NO_DATA_FOUND THEN NULL;
end set_context;
END Context_check;
Step 2: When I am trying to executing the procedure I am getting an error
EXECUTE Context_check.set_context('Ana');
Error starting at line 43 in command:
EXECUTE Context_check.set_context('Ana')
Error report:
ORA-01031: insufficient privileges
ORA-06512: at "SYS.DBMS_SESSION", line 114
ORA-06512: at "SEC_ADMIN.CONTEXT_CHECK", line 8
ORA-06512: at "SEC_ADMIN.CONTEXT_CHECK", line 20
ORA-06512: at line 1
01031. 00000 - "insufficient privileges"
*Cause: An attempt was made to change the current username or password
without the appropriate privilege. This error also occurs if
attempting to install a database without the necessary operating
system privileges.
When Trusted Oracle is configure in DBMS MAC, this error may occur
if the user was granted the necessary privilege at a higher label
than the current login.
*Action: Ask the database administrator to perform the operation or grant
the required privileges.
For Trusted Oracle users getting this error although granted the
the appropriate privilege at a higher label, ask the database
administrator to regrant the privilege at the appropriate label.
I have already given all the grants on that package.Still I am not able to execute this procedure.
Note : If I create the same procedures as stand alone ,its working fine and setting the context.
You need to create a context using a package, not using a procedure inside of a package.
Instead of
EXECUTE IMMEDIATE 'CREATE OR REPLACE CONTEXT VPD_CTX using set_context_vpd_proc';
Write
EXECUTE IMMEDIATE 'CREATE OR REPLACE CONTEXT VPD_CTX using Context_check';

Execute copy from command from plsq

How to execute copy from command inside a plsql block?
E.g. I have copy from test/test#test insert emp using select * from emp;
How can I call this in a plsql block? I have tried with
execute immediate 'copy from test/test#test insert emp using select * from emp';
However when I execute my script which has plsql block gives me
ORA-00900: invalid SQL statement
How can I resolve this issue
COPY is a SQL*Plus command. So it only works in the SQL*Plus client. Find out more.
EXECUTE IMMEDIATE is a PL/SQL command to run dynamic calls, and it only recognises SQL and PL/SQL.
"I am executing sqlscript from sqlplus"
Yes, but you are calling COPY in an anonymous block, so that's with a PL/SQL scope; which means PL/SQL and SQL only.
The way to do this is with a shell script. These are operating system dependent, but something like this would work on a Linux environment.
#!/bin/bash
echo Please enter local Username:
read USERNAME
echo "Please enter local Password:"
read -s PASS
SID=${ORACLE_SID}
if [ "${ORACLE_SID}" != 'TEST' ]
then
sqlplus -s -l $USERNAME/$PASS#$SID << EOF
copy from test/test#test insert emp using select * from emp
exit
EOF
else
echo "Can't copy from TEST to TEST"
fi
Obviously this is just a wild guess at what your program actually does, but I hope you can understand the principle.
In a plsql code if we directly use the command as follows shall serve the similar output
begin
insert into emp1 select * from emp;
end;
emp1 is target table
emp is source table
There are similar ask where one wants to create blank structure or structure with data for backup kindly of activity.Refer link https://oracle-concepts-learning.blogspot.com/2019/09/copy-table-structure-or-data.html
1) Creating blank structure from existing table
--Execute on sql prompt
begin
execute immediate 'create table emp1 as select * from emp where 1=2';
end;
--Execute on sql prompt
select count(1) from emp1;
2) Creating structure from existing table with data
--Execute on sql prompt
begin
execute immediate 'create table emp1 as select * from emp';
end;
--Execute on sql prompt
select count(1) from emp1;

Resources