How to get user input in ORACLE using ACCEPT? - sql

I need to get input from user in runtime in ORACLE. I am trying to do this.
Get input 'name' from user
Greet user with 'name' using DBMS_OUTPUT
My code so far:
ACCEPT name PROMPT 'Your name:'
declare
a varchar2(10);
begin
a := '&x';
end;

You need to use the name you assigned in the ACCEPT statement in your code. So
ACCEPT name PROMPT 'Your name:'
begin
dbms_output.put_line ('Hello &name !');
end;
/
Regarding your comment:
"ACCEPT name PROMPT 'Your name:' still is a unsuported command"
According to your comment you're using Oracle's LiveSQL, right? Well, accept is a SQL*Plus command for handling input parameters; SQL*Plus is part of a client install. LiveSQL is a different client. If you read its FAQs you will see:
Can I use input parameters?
No, we do not support input parameters at runtime.

Related

FireDAC could not handle parameter names longer than 30 chars

I use Oracle database version 19.3.0 and it can handle parameter names longer than 30 chars. If I execute in Delphi FireDAC-procedure "ExecProc" to call Stored Procedure in SQL, Delphi throws "ORA-01036 : illegal Variable name/number" exception and calling of the procedure terminates prematurely, because one parameter name is longer then 30 chars.
Is there some FireDAC property to change this behavior without changing parameters name length?
"C:\Program Files (x86)\Embarcadero\Studio\19.0\source\data\firedac\FireDAC.Phys.Oracle.pas"
Solution: Change pbByName to pbByNumber
procedure TForm5.Button1Click(Sender: TObject); begin
FDStoredProc1.ParamBindMode := pbByNumber;
FDStoredProc1.FetchOptions.Items := [fiBlobs, fiDetails]; //This disables automatic fetching of parameters from server
FDStoredProc1.Params.CreateParam(ftString,'Averylongparamternamethathopefullyislongerthanthirtycharacters',ptInput);
FDStoredProc1.params.CreateParam(ftString,'outparam',ptOutput);
FDStoredProc1.Params[0].AsString := 'Hello World';
FDStoredProc1.ExecProc;
ShowMessage(FDStoredProc1.Params[1].AsString);
end;

sqlplus variable value is not accepting in EOF block in shell

I am unable to give input to the variable "y" written in below code. Can any one help us Please.
Code :
#!/bin/bash
${ORACLE_HOME}/bin/sqlplus -s abcd/passabcd <<EOF
declare
res varchar2(9) := '&y';
begin
insert into abc values(10);
if res in ('commit;')
then
EXECUTE IMMEDIATE 'commit;';
elsif res in ('rollback;')
then
EXECUTE IMMEDIATE 'rollback;';
else
DBMS_OUTPUT.PUT_LINE('Enter Correct Input');
end if;
EXCEPTION
WHEN OTHERS THEN
dbms_output.put_line('Error msg ' || substr(sqlerrm,1,200));
end;
/
EOF
if i run this, below is the screen. automatically i am getting sp2-0546 error without entering any input.
[oracle#ssmz861 ~]$ ./mm.sh
Enter value for y:SP2-0546: User requested Interrupt or EOF detected.
[oracle#ssmz861 ~]$
here is the scenario: I have a pl/sql code i need to give to sqlplus to execute in following manner like <EOF .....EOF. in that one i have to see the output of one query and decide whether need to commit or rollback by giving input.
thanks in advance
Siva

Adding a header to an exported file sql

i'm trying to make a log file with sql, that contains a header, but i am getting this error : ORA-01756: quoted string not properly terminated , because of the semicolon, that my header should contains.
select 'User. Name;
User. Number;
User. Data;' from dual;
Kind regards,
run them in one line like this. If you want them in new line add chr(10) :
select 'User.Name;'||chr(10)||'user.Number;'||chr(10)||'User.Data;' from dual;
because oracle consider ; as to execute the command
If you need to write something in a spool file by SQLPlus, you can simply use PROMPT:
PROMPT User. Name; User. Number; User. Data;
or, if you need to print 3 rows:
PROMPT User. Name;
PROMPT User. Number;
PROMPT User. Data;
If you're using spool and want header to your query please execute before query:
SET HEADING ON
SET PAGESIZE 50000
PAGESIZE should be something big as this means after how many line header will be repeated.

Wrapping a Stored Procedure

I have created a Stored Procedure in Oracle (Via TOAD).
I need to deliver this procedure to some other developers. All is need to do is Wrap the procedure so that at basic level he/she should not be able to view the code.
At the same time, the developer should be able to create the procedure and execute/test it.
my procedure is saved in say FileName.sql whose content are like:
Create or Replace Procedure
IS
Declaration
Begin
Code
End;
Now i want this FileName.sql to be wrapped. So that the encrypted file can be send to other to check in different environment.
Please help me in how to wrap, and then how the other guy will be able to create the procedure, and execute the same.
Thanks in advance.
The Oracle WRAP utility does exactly that - allows you to encode a stored procedure in a form suitable for shipping.
The wrapped code is as portable as source code, but cannot be examined as plain text.
You will need access to the command line using a suitable system user (i.e. one that has access to the Oracle binary commands e.g. sqlplus etc.) and the basic syntax is:
wrap iname=input_file [ oname=output_file ]
If you do not specify any extensions, the default for input is .sql and output is .plb
Once you have generated a .plb file, you can execute it against the database to create your stored procedure.
See:
http://docs.oracle.com/cd/B28359_01/appdev.111/b28370/wrap.htm#LNPLS016
http://docs.oracle.com/cd/B10500_01/appdev.920/a96624/c_wrap.htm
WARNING
WRAPencoded procedures are not completely secure - it is possible to "unwrap" them.
A better way of using WRAP is to put the procedure(s) you wish to protect in a package and WRAP the package definition.
Let's say your procedure is:
CREATE OR REPLACE PROCEDURE PR_OUTPUT_TEXT(
P_TYPE IN CHAR,
P_TEXT IN VARCHAR2
) IS
V_TYPE CHAR(3) := UPPER(P_TYPE);
BEGIN
IF V_TYPE = 'LOG' THEN
APPS.FND_FILE.PUT_LINE(APPS.FND_FILE.LOG, TO_CHAR(SYSDATE,'HH24:MI:SS')||' - '||P_TEXT);
ELSE
APPS.FND_FILE.PUT_LINE(APPS.FND_FILE.OUTPUT, P_TEXT);
END IF;
END PR_OUTPUT_TEXT;
you would normally call it using:
EXECUTE PR_OUTPUT_TEXT('LOG', 'Kittehz!!!')
In a package you would define the package body and the procedure thus:
CREATE OR REPLACE PACKAGE BODY USER.MYPACKAGE AS
PROCEDURE PR_OUTPUT_TEXT(
P_TYPE IN CHAR,
P_TEXT IN VARCHAR2
) IS
BEGIN
IF V_TYPE = 'LOG' THEN
APPS.FND_FILE.PUT_LINE(APPS.FND_FILE.LOG, TO_CHAR(SYSDATE,'HH24:MI:SS')||' - '||P_TEXT);
ELSE
APPS.FND_FILE.PUT_LINE(APPS.FND_FILE.OUTPUT, P_TEXT);
END IF;
END PR_OUTPUT_TEXT;
END MYPACKAGE;
and you would call the package using:
EXECUTE USER.MYPACKAGE.PR_OUTPUT_TEXT('LOG', 'ERMAHGERD KERTERNS!!!')
From documentation, to be precise, this is what you need to do :
Running the wrap Utility
For example, assume that the wrap_test.sql file contains the following:
CREATE PROCEDURE wraptest IS
TYPE emp_tab IS TABLE OF employees%ROWTYPE INDEX BY PLS_INTEGER;
all_emps emp_tab;
BEGIN
SELECT * BULK COLLECT INTO all_emps FROM employees;
FOR i IN 1..10 LOOP
DBMS_OUTPUT.PUT_LINE('Emp Id: ' || all_emps(i).employee_id);
END LOOP;
END;
/
To wrap the file, run the following from the operating system prompt:
wrap iname=wrap_test.sql
The output of the wrap utility is similar to the following:
PL/SQL Wrapper: Release 10.2.0.0.0 on Tue Apr 26 16:47:39 2005
Copyright (c) 1993, 2005, Oracle. All rights reserved.
Processing wrap_test.sql to wrap_test.plb
If you view the contents of the wrap_test.plb text file, the first line is CREATE PROCEDURE wraptest wrapped and the rest of the file contents is hidden.
You can run wrap_test.plb in SQL*Plus to execute the SQL statements in the file:
SQL> #wrap_test.plb
After the wrap_test.plb is run, you can execute the procedure that was created:
SQL> CALL wraptest();
More information in docs

Is there a way to auto-input sql script prompts?

I have a sqlscript that contains statements like this:
prompt Enter 'html' for an HTML report, or 'text' for plain text
prompt Defaults to 'html'
column report_type new_value report_type;
set heading off;
select 'Type Specified: ',lower(nvl('&&report_type','html')) report_type from dual;
So when I run the script it prompts me to enter in values. I want to automate this script. How would I pass to the script parameters that are then used for the prompts? I cannot modify the script at all by removing the prompts.
I am using Windows Server 2008 and Windows 7.
On Unix you can use here documents:
#>sqlplus un #script <<EOF
yourpassword
parameter#1
parameter#2
EOF
Assuming you're passing values in from the command line or a batch file etc you can pass in parameters by using &1, &2, .. &n, where the number is the position of the parameter when calling
column report_type new_value report_type
set heading off
select 'Type Specified: ',lower(nvl('&1','html')) report_type from dual;
This would be called by
[call sqlplus schema/pw#db #] my_script.sql html
Incidentally you don't need semi-colons after SQL*Plus commands.