Showing posts with label Accept. Show all posts
Showing posts with label Accept. Show all posts

Tuesday, September 2, 2008

Jobname Check for Data Pump

rem +--------------------------------------------------------------------------+
rem | Script ID: ckjob.sql
rem |
rem | Purpose: Check the job name to see if a table exists with that name
rem | before Data Pump job is run
rem |
rem | Developer: Lynn Tobias
rem | Script Date: 9/2/2008
rem | Oracle Ver: 10g
rem |
rem | Table(s) Used: All_Catalog
rem |
rem | Variables: Schema JobName Ctr OnOff
rem |
rem | Revisions: Dvl Date Ver Comment/Change
rem | --- -------- --- -----------------------------------------+
rem |
rem +--------------------------------------------------------------------------+
Define OnOff = Off
Clear Screen
Set Echo &OnOff Term On

Prompt +--------------------------------------------------------------------------+
Prompt | This will check your Data Pump jobname against the schema. |
Prompt | Since Data Pump creates a table with this name, it cannot already exist. |
Prompt +--------------------------------------------------------------------------+
Prompt
--+-----------------------------------------------------------------------------
--| Ask them for schema (if the current it will be equal to _User) and jobname
--+-----------------------------------------------------------------------------
Accept Schema Default &_User Prompt 'Type Schema or press enter for default (&_user.): '
Prompt
Accept JobName Prompt 'Your jobname : '

--+-----------------------------------------------------------------------------
--| Turn off the display and get the count of tables like the job name
--+-----------------------------------------------------------------------------
Set Term &OnOff
Column Ctr New_Value Ctr

Select Count(*) Ctr
From All_Catalog
Where Table_Name = Upper('&JobName')
And Owner = '&Schema';

--+-----------------------------------------------------------------------------
--| Based on the count, display an error or not
--+-----------------------------------------------------------------------------
Set Pagesize 50 Term On Heading &OnOff Feedback &OnOff

Ttitle Left 'Results of jobname check: ' Skip 1 -
'------------------------- ' Skip 2

Select Case when &Ctr > 0
Then 'Error: A jobname can''t be the name of a table or view in the schema'
Else 'JobName is ok to use.'
End
From Dual;
Ttitle off
Undefine Schema JobName Ctr

Wednesday, June 25, 2008

An Easy Way to Debug SQL Queries


This is a separate piece of code that makes it easy to debug another query. When there is a problem with your code, run this from the prompt. In my case, I've cleverly named this file D.SQL. When I type @D and answer 'on,' any query I run next will have echo, headings, verify, timing, feedback and term set to 'on.' Additionally, any columns not being printed (coded with 'noprint') will be displayed.

You use the following variables in your queries:

  • &OnOff For example:
   Set Term &OnOff
Set Echo &OnOff
  • &xPrint For example:
     Column Loc_ID &xPrint
Use these only when you normally want the function Off. If you want Term On at a certain point (i.e., with Accept), don't use this variable.

--+-----------------------------------------------------------------------+
--| Ask the question
--+-----------------------------------------------------------------------+
Set Echo Off
Set Term On

Prompt
Accept OnOff Format A3 Default Off -
Prompt 'Do you want Echo/Feedback/Verify/Timing/Print On or Off? '
Prompt

Set Term Off
--+-----------------------------------------------------------------------+
--| Set everything off so it doesn't show up in this program
--+-----------------------------------------------------------------------+

Set Feedback Off Timing Off Verify Off Heading Off Newpage None Echo off
Ttitle Off
Repheader Off

--+-----------------------------------------------------------------------+
--| Set up 'amper'-xprint to be used in code in place of 'noprint'
--+-----------------------------------------------------------------------+

Column PrintOrNo New_Value xPrint Noprint

Select Decode(Upper('&OnOff'), 'ON' , 'Print' ,
'OFF', 'NoPrint' ,
'NoPrint'
)
As
PrintOrNo
From Dual;
--+-----------------------------------------------------------------------+
--| Set Each Debugging Tool On Or Off As Requested.
--+-----------------------------------------------------------------------+
Set Feedback &Onoff
Set Timing &Onoff
Set Verify &Onoff
Set Echo &Onoff
Set Heading On
Set Term On
See Oracle documentation for further detail:
Accept
Decode
Set Feedback Timing Verify Heading Newpage Echo

Friday, June 6, 2008

Use Variables to Insert Data














In this example, a user is asked for three kinds of variables: Character, Date & Number. They are then inserted into an 'INSERT INTO' statement. (In entering the date, the year is assumed to be the current one.) After insertion, a SELECT is performed to show the user the data entered as it is in the table.
Set Verify Off
Set Feedback Off
Prompt Please Enter...

--+------------------------------------------------------------------
--| Ask for data
--+------------------------------------------------------------------

Accept Who Prompt 'Name: '
Accept Month Format 99 Prompt 'Month: '
Accept Day Format 99 Prompt 'Day: '
Accept Amount Prompt 'Amount: '

--+- 1 --------------------------------------------------------------
--| Insert the record based on the values typed (Assume current year)
--+------------------------------------------------------------------

Insert Into TestData Values
(
Test_Seq.NextVal
,
Upper('&Who')
,
&Day
|| '-'
|| Decode(&Month, 1,'JAN', 2,'FEB', 3,'MAR', 4,'APR',
5,'MAY', 6,'JUN', 7,'JUL', 8,'AUG',
9,'SEP', 10,'OCT', 11,'NOV', 12,'DEC',
'???')
|| '-'
|| To_Char(Sysdate,'YY')
,
&Amount
);
Commit;

--+- 2 --------------------------------------------------------------
--| Get the current value
--+------------------------------------------------------------------

Set Term Off

Column CurrVal New_Value CurrVal

Select Test_Seq.Currval CurrVal
From Dual;

--+- 3 --------------------------------------------------------------
--| Display the record just entered.
--+------------------------------------------------------------------

Set Term On

Select *
From Test_Data
Where Test_Id = &CurrVal
;
Undefine Who Month Day Amount
Set Feedback On
Set Verify On

Oracle documentation:

Monday, May 26, 2008

Defining a Variable

There are three ways to set up a variable: Define, Accept and New_Value.


Use Define when you know the value and you want to hard-code it in the query.

Define variable = text
Define DNo1 = 10

Select EmpNo, EName
From Emp
Where Deptno = &DNo1;

EMPNO ENAME
---------- ----------
7782 CLARK
7839 KING
7934 MILLER



Use Accept when you the query operator knows the value and you want them to type it.

Accept variable [NUMBER|CHAR|DATE [FORMAT format] [DEFAULT default] [PROMPT text|NOPROMPT] [HIDE]

The query will pause and the prompt display. The 'Number' option will assure a number is entered. 'Format 99' will make sure only two digits are entered. (I typed in the '20' when the query paused.)

Accept DNo2 Number Format 99 Prompt 'Please enter Department Number: '

Please enter Department Number: 20

Select EmpNo, EName
From Emp
Where DeptNo = &DNo2;

EMPNO ENAME
---------- ----------
7369 SMITH
7566 JONES
7788 SCOTT
7876 ADAMS
7902 FORD



Use New_Value when the value can be retrieved from a table.

Column column New_Value variable

Column DeptNo New_Value DNo3

Select Ename, Deptno
From Emp
Where Ename = 'ALLEN';

ENAME DEPTNO
---------- ----------
ALLEN 30

Select *
From Dept
Where Deptno = &DNo3;

DEPTNO DNAME LOC
---------- -------------- -------------
30 SALES CHICAGO