Oracle - How to use & without being asked about the value? [duplicate] - oracle

This question already has answers here:
How to declare variable and use it in the same Oracle SQL script?
(11 answers)
Closed 11 months ago.
Basically, I don't want to be asked about the value like this::
SQL> select &test from dual;
Enter value for test:
I want only declare the &test along the script, something like it:
&test varchar2(100):= 'some value'; --of course, this don't work.

Execute
SQL> set define off
before running your code.
SQL> select '&test' from dual;
'&TES
-----
&test
SQL>
If you want to "declare" it, then use var:
SQL> var test varchar2(200);
SQL> exec :test := 'some value';
PL/SQL procedure successfully completed.
SQL> print test
TEST
----------------------------------------------------------------------------------------------------
some value
SQL>
In dynamic SQL: I won't lock anyone, but - I'll change my password.
SQL> connect scott/tiger
Connected.
SQL> var test varchar2(200);
SQL> exec :test := 'lion';
PL/SQL procedure successfully completed.
SQL> print test
TEST
----------------------------------------------------------------------------------------------------
lion
SQL> begin
2 execute immediate 'alter user scott identified by ' || :test;
3 end;
4 /
PL/SQL procedure successfully completed.
SQL> connect scott/tiger
ERROR:
ORA-01017: invalid username/password; logon denied
Warning: You are no longer connected to ORACLE.
SQL> connect scott/lion
Connected.
SQL>

Related

PL/SQL command not properly working though successfully completed [duplicate]

This question already has answers here:
Printing the value of a variable in SQL Developer
(9 answers)
Closed 10 months ago.
I have written a simple PL/SQL command of printing hello world to console but it prints nothing and still message is prompted that PL/SQL procedure successfully completed. I am not able to figure it out as to what to do in this case?
Code:
BEGIN
dbms_output.put_line ('Hello World..');
END;
OUTPUT:
You're missing the set serveroutput on.
This is what you have:
SQL> begin
2 dbms_output.put_line('Hello world');
3 end;
4 /
PL/SQL procedure successfully completed.
This is what you should have:
SQL> set serveroutput on --> this
SQL> begin
2 dbms_output.put_line('Hello world');
3 end;
4 /
Hello world --> here's the result
PL/SQL procedure successfully completed.
SQL>

pl /sql set ORA-00922: missing or invalid option on set serveroutput on;

set serveroutput on;
DECLARE
message varchar2(20):= 'Hello, World!';
BEGIN dbms_output.put_line(message);
END;
/
As others have indicated "set server output on" is a SQL*Plus command. If you need that functionality in plsql the you're looking for is DBMS_OUTPUT.ENABLE. Your above block becomes:
declare
message varchar2(20) := 'Hello World';
begin
dbms_output.enable;
dbms_output.put_line(message);
end ;
If use SQL*plus then this code works fine.
[oracle#krw-sql-ora12-01 ~]$ sqlplus scott/tiger
SQL*Plus: Release 11.2.0.3.0 Production on Fri Feb 22 08:07:25 2019
Copyright (c) 1982, 2011, Oracle. All rights reserved.
Connected to:
Oracle Database 11g Enterprise Edition Release 11.2.0.3.0 - 64bit Production
With the Partitioning, OLAP, Data Mining and Real Application Testing options
SQL> set serveroutput on;
SQL> DECLARE
2 message varchar2(20):= 'Hello, World!';
3 BEGIN dbms_output.put_line(message);
4 END;
5 /
Hello, World!
PL/SQL procedure successfully completed.
SQL>
It seems that you used SET SERVEROUTPUT ON within the PL/SQL procedure (or an anonymous block), e.g.
SQL> begin
2 set serveroutput on;
3 end;
4 /
set serveroutput on;
*
ERROR at line 2:
ORA-06550: line 2, column 7:
PL/SQL: ORA-00922: missing or invalid option
ORA-06550: line 2, column 3:
PL/SQL: SQL Statement ignored
SQL>
Perhaps you didn't post everything you really have; is that SET command part of a larger procedure? If so, move it out.
You mention that you are using toad. You have to use the lightning bolt instead of the green triangle. Hover your mouse over the two icons. You will see the green triangle says: Execute/compile statement at caret. You will see the lightning bolt says: Execute as script.
set serveroutput on;
DECLARE
message varchar2(20):= 'Hello, World!';
BEGIN dbms_output.put_line(message);
END;
/

Accessing a function against another owner in oracle?

I am having a function called fn_export and its owner is bhist. I am calling this function from ohist user using bhist.fn_export. While calling like this I am facing the below issue.
ORA-00942: table or view does not exist
ORA-06512: at "bhist.fn_export", line 442
ORA-06512: at line 20
I tried to verify all the tables in that function and I am able to access all those tables from ohist. I have execute grant on bhist.fn_export to ohist. Still I am having this issue. Can any one of you please help in resolving this issue?
Thanks,
Venkat
You need to grant EXECUTE privilege on this function to ohist user.
A syntax is:
GRANT EXECUTE ON function_name TO username;
You can connect as bhist user and grant the privilege using:
GRANT EXECUTE ON fn_export TO ohist;
You can also connect as SYS or SYSTEM, and use this command:
GRANT EXECUTE ON bhist.fn_export TO ohist;
See a below simple example (one user is named TEST and the other is named DEV):
SQL> connect test
Enter password:
Connected.
SQL> CREATE FUNCTION fn_export RETURN number AS
2 BEGIN
3 RETURN 20;
4 END;
5 /
Function created.
SQL> connect dev
Enter password:
Connected.
SQL> SELECT test.fn_export FROM dual;
SELECT test.fn_export FROM dual
*
ERROR at line 1:
ORA-00904: "TEST"."FN_EXPORT": invalid identifier
SQL> connect test
Enter password:
Connected.
SQL> grant execute on fn_export to dev;
Grant succeeded.
SQL> connect dev
Enter password:
Connected.
SQL> SELECT test.fn_export FROM dual;
FN_EXPORT
----------
20
SQL>

Using dollar sign in sqlplus spool file

How to use file name with dollar sign (ie, '$') in unix like below
SQL> spool DIR$work.sql
SP2-0332: Cannot create spool file.
and i tried like below
SQL> spool DIR\$work.sql
SP2-0332: Cannot create spool file.
SQL> spool 'DIR\$work.sql'
SP2-0332: Cannot create spool file.
SQL> spool 'DIR$work.sql'
SP2-0332: Cannot create spool file.
I couldn't succeed in any way to create such file in oracle.
I have oracle 11g version.
In windows sqlplus it works fine.
You can use the set escchar setting to stop Oracle interpreting the dollar sign:
SQL> show escchar
escchar OFF
SQL> spool /tmp/$work.sql
SP2-0332: Cannot create spool file.
SQL> set escchar $
SQL> spool /tmp/$work.sql
SQL>
You are now spooling to that file name.
SQL> select * from dual;
D
-
X
1 row selected.
SQL> spool off
SQL> exit
Disconnected from Oracle Database 11g Enterprise Edition Release 11.2.0.4.0 - 64bit Production
...
$ cat /tmp/\$work.sql
SQL> select * from dual;
D
-
X
1 row selected.
SQL> spool off
Also see My Oracle Support document 761384.1 for more information.

PL SQL output is not getting displayed

I have fairly simply code ..running in Oracle Virtualbox. However for some reason it is not displaying pl/sql output.
Here is code snippet
SQL> set serveroutput on
SQL> list
1 Create or Replace procedure mytz
2 IS
3 v_mytz TIMESTAMP WITH TIME ZONE DEFAULT '2013-05-05 12:00:00 AM';
4 BEGIN
5 DBMS_OUTPUT.PUT_LINE ('Default timestamp is ' );
6* end mytz ;
SQL> /
Procedure created.
SQL>
Is there anything I need to do special to see the output on SQL prompt ?
You have to actually run the procedure, not just create it, e.g.:
set serverputput on
exec mytz;
The set serveroutput SQL*Plus command has to be in the session the procedure is executed, not the one where it is created (if they are different).
You are not showing the value of your variable at the moment; maybe you wanted this?
dbms_output.put_line('Default timestamp is: ' || v_mytz);

Resources