Home » SQL & PL/SQL » SQL & PL/SQL » decode functionality in DBMS_OUTPUT (10g)
decode functionality in DBMS_OUTPUT [message #462946] Mon, 28 June 2010 15:51 Go to next message
geneeyuss
Messages: 53
Registered: June 2010
Member


I would like to use the below decode in DBMS_OUTPUT in place of cur_rec.data_type. Could I know how can I achieve this.
The DBMS_OUTPUT is inside a loop and output is shown below as an example.

NOT NULL -- EMP_ID NUMBER
,NOT NULL -- EMP_NAME NUMBER
,NOT NULL -- HIRE_DT DATE
,NOT NULL -- SALARY NUMBER

DBMS_OUTPUT STMT:

Dbms_Output.Put_Line(nullable ||CHR(9)||CHR(9)||' -- ' ||upper(cur_rec.column_name)||CHR(9)||CHR(9)||cur_rec.data_type);


STMT to use in place of cur_rec.data_type:

substr(decode( data_type, 'NUMBER', decode( data_precision, NULL, NULL,'('||data_precision||','||data_scale||')' ),data_length),1,11)


Also I am not able to align the output using CHR(9). Could I know a better method to do this.

Thanks
geneeyuss
Re: decode functionality in DBMS_OUTPUT [message #462950 is a reply to message #462946] Mon, 28 June 2010 16:38 Go to previous messageGo to next message
Littlefoot
Messages: 21826
Registered: June 2005
Location: Croatia, Europe
Senior Member
Account Moderator
As far as I can tell, you'll have to use DECODE within the SELECT statement. So, you'd
select substr(decode(data_type, blabla
into a_variable
from ...
and then DBMS_OUTPUT.PUT_LINE(a_variable).

By the way, what tool do you use? Does it use a non-proportional font (which probably means that you use a GUI) (as you can't align those values properly)? If not, what this "alignment" question means? Could you provide an example?
Re: decode functionality in DBMS_OUTPUT [message #462951 is a reply to message #462950] Mon, 28 June 2010 17:15 Go to previous messageGo to next message
BlackSwan
Messages: 26766
Registered: January 2009
Location: SoCal
Senior Member
This thread is just a continuation of the thread below

http://www.orafaq.com/forum/m/461813/136107/#msg_461813

PL/SQL is not a very good report generation tool & you are learning this reality first hand.
You could always run your results through any available code formatter to make it look presentable.
Re: decode functionality in DBMS_OUTPUT [message #462965 is a reply to message #462946] Tue, 29 June 2010 00:40 Go to previous messageGo to next message
delna.sexy
Messages: 941
Registered: December 2008
Location: Surat, The Diamond City
Senior Member
Can not understand your question.
Describe it properly with proper test case and required output.

regards,
Delna
Re: decode functionality in DBMS_OUTPUT [message #462966 is a reply to message #462946] Tue, 29 June 2010 00:44 Go to previous messageGo to next message
Michel Cadot
Messages: 68775
Registered: March 2007
Location: Saint-Maur, France, https...
Senior Member
Account Moderator
Quote:
STMT to use in place of cur_rec.data_type:

substr(decode( data_type, 'NUMBER', decode( data_precision, NULL, NULL,'('||data_precision||','||data_scale||')' ),data_length),1,11)

Why don't you use this expression in the query/cursor instead of just "data_type"?

Regards
Michel
Re: decode functionality in DBMS_OUTPUT [message #463062 is a reply to message #462966] Tue, 29 June 2010 09:59 Go to previous messageGo to next message
geneeyuss
Messages: 53
Registered: June 2010
Member

My aim is to create an insertTemplate for my oncvinience in testing my data.
The script I wrote for that is below:


SET LINESIZE 1000
SET SERVEROUTPUT ON
SET FEEDBACK OFF
SET PAGESIZE 0
SET VERIFY OFF
SET TRIMOUT ON

ALTER SESSION SET nls_date_format = 'DD-MON-YYYY HH24:MI:SS';

DECLARE

CURSOR c_columns (p_table_name IN VARCHAR2,
p_owner IN VARCHAR2) IS
SELECT Lower(a.column_name) column_name,
a.data_type, a.nullable, a.data_precision, a.data_scale, a.data_length
FROM all_tab_columns a
WHERE a.table_name = p_table_name
AND a.owner = p_owner
AND a.data_type IN ('CHAR','VARCHAR2','DATE','NUMBER','INTEGER');

v_table_name VARCHAR2(30) := Upper('&TABLE_NAME');
v_owner VARCHAR2(30) := Upper('&SCHEMA');
nullable VARCHAR2(10);


FUNCTION Format_Col(p_column IN VARCHAR2,
p_datatype IN VARCHAR2,
p_precision IN NUMBER,
p_scale IN NUMBER,
p_length IN NUMBER)
RETURN VARCHAR2 IS

result VARCHAR2(20);
BEGIN
IF p_datatype IN ('NUMBER') THEN
IF p_precision IS NULL THEN
result := NULL;
ELSE
result := '('||p_precision||','||p_scale||')';
END IF;
ELSIF p_datatype IN ('DATE') THEN
result := NULL;
ELSE
result := '('||p_length||')';
END IF;
RETURN substr(result, 1, 11);
END;

BEGIN

Dbms_Output.Disable;
Dbms_Output.Enable(1000000);

Dbms_Output.Put_Line('INSERT INTO ' || Lower(v_owner) || '.' || Lower(v_table_name) || ' VALUES');
Dbms_Output.Put_Line('(');
FOR cur_rec IN c_columns (v_table_name, v_owner) LOOP
IF c_columns%ROWCOUNT != 1 THEN
Dbms_Output.Put(',');
END IF;
IF cur_rec.nullable = 'Y' THEN
nullable := 'NULL';
ELSE
nullable := 'NOT NULL';
END IF;
Dbms_Output.Put_Line(nullable || CHR(9)||CHR(9)||CHR(9)||CHR(9)||CHR(9)||CHR(9)||' -- ' ||upper(cur_rec.column_name)||CHR(9)||CHR(9)||cur_rec.data_type||Format_Col(cur_rec.column_name, cur_rec.data_type, cur_rec.data_precision, cur_rec.data_scale, cur_rec.data_length));
END LOOP Columns_Loop;
Dbms_Output.New_Line;
Dbms_Output.Put_Line(');');

END;
/


SET LINESIZE 100

SET PAGESIZE 14
SET FEEDBACK ON

When I run this the output I get is:

10:31:30 IR2_DXU_TOOL@doirtx01 29-JUN-2010 10:31:30 SQL> @insertTemplate
Elapsed: 00:00:00.01
Enter value for table_name: emp
Enter value for schema: ar
INSERT INTO ar.emp VALUES
(
NOT NULL -- EMP_ID NUMBER(15,0)
,NOT NULL -- ENAME VARCHAR2(30)
,NULL -- HIRE_DT DATE
,NOT NULL -- SALARY NUMBER(10,2)
);

I want this template to be formatted to appear like below:

INSERT INTO ar.emp VALUES
(
NOT NULL -- EMP_ID NUMBER(15,0)
,NOT NULL -- ENAME VARCHAR2(30)
,NULL -- HIRE_DT DATE
,NOT NULL -- SALARY NUMBER(10,2)
);

I tried using CHR(9) for that but doesn't seem to be working properly. Could I know a better solution.

thanks
geneeyuss
Re: decode functionality in DBMS_OUTPUT [message #463064 is a reply to message #463062] Tue, 29 June 2010 10:04 Go to previous messageGo to next message
BlackSwan
Messages: 26766
Registered: January 2009
Location: SoCal
Senior Member
>I want this template to be formatted to appear like below:
FORMATTED? I don't think so.

>Could I know a better solution.
It depends upon the "tool" accessing your results.
Re: decode functionality in DBMS_OUTPUT [message #463065 is a reply to message #463062] Tue, 29 June 2010 10:06 Go to previous messageGo to next message
cookiemonster
Messages: 13975
Registered: September 2008
Location: Rainy Manchester
Senior Member
geneeyuss wrote on Tue, 29 June 2010 15:59

When I run this the output I get is:

10:31:30 IR2_DXU_TOOL@doirtx01[/email] 29-JUN-2010 10:31:30 SQL> @insertTemplate
Elapsed: 00:00:00.01
Enter value for table_name: emp
Enter value for schema: ar
INSERT INTO ar.emp VALUES
(
NOT NULL -- EMP_ID NUMBER(15,0)
,NOT NULL -- ENAME VARCHAR2(30)
,NULL -- HIRE_DT DATE
,NOT NULL -- SALARY NUMBER(10,2)
);

I want this template to be formatted to appear like below:

INSERT INTO ar.emp VALUES
(
NOT NULL -- EMP_ID NUMBER(15,0)
,NOT NULL -- ENAME VARCHAR2(30)
,NULL -- HIRE_DT DATE
,NOT NULL -- SALARY NUMBER(10,2)
);

I tried using CHR(9) for that but doesn't seem to be working properly. Could I know a better solution.

thanks
geneeyuss


I can't actually see any difference between the two. It might help if you used [code] tags to preserve formatting.
Re: decode functionality in DBMS_OUTPUT [message #463078 is a reply to message #463065] Tue, 29 June 2010 11:30 Go to previous messageGo to next message
geneeyuss
Messages: 53
Registered: June 2010
Member
I want the formatting to be like below:

INSERT INTO ar.emp VALUES
(
NOT NULL                                        --  EMP_ID                NUMBER(15,0)
,NOT NULL                                       --  ENAME                 VARCHAR2(30)
,NULL                                           --  HIRE_DT               DATE
,NOT NULL                                       --  SALARY                NUMBER(10,2)
);
Re: decode functionality in DBMS_OUTPUT [message #463082 is a reply to message #463078] Tue, 29 June 2010 11:38 Go to previous messageGo to next message
BlackSwan
Messages: 26766
Registered: January 2009
Location: SoCal
Senior Member
>I want the formatting to be like below:
You need to repeat the process that was done to produce message #463078
Re: decode functionality in DBMS_OUTPUT [message #463083 is a reply to message #463078] Tue, 29 June 2010 11:40 Go to previous messageGo to next message
cookiemonster
Messages: 13975
Registered: September 2008
Location: Rainy Manchester
Senior Member
Well chr(9) is a tab and oracle isn't going to miss it out, so presumably whatever you are using to view the file doesn't recognize it.
So what are you using to view the file?
Re: decode functionality in DBMS_OUTPUT [message #463089 is a reply to message #462946] Tue, 29 June 2010 12:04 Go to previous messageGo to next message
geneeyuss
Messages: 53
Registered: June 2010
Member
it doesnt omit tabs. but doesnt get exactly aligned because of the NULL and NOT NULL. It does give 5 tabs if we have CHR(9) written 5 times. But its not exactly aligned.
Any suggestions on this?
INSERT INTO ar.emp VALUES
(
NOT NULL                                        --  EMP_ID                NUMBER(15,0)
,NOT NULL                                       --  ENAME                 VARCHAR2(30)
,NULL                                        	--  HIRE_DT               DATE
,NOT NULL                                       --  SALARY                NUMBER(10,2)
);
Re: decode functionality in DBMS_OUTPUT [message #463090 is a reply to message #462946] Tue, 29 June 2010 12:05 Go to previous messageGo to next message
geneeyuss
Messages: 53
Registered: June 2010
Member
it doesnt omit tabs. but doesnt get exactly aligned because of the NULL and NOT NULL. It does give 5 tabs if we have CHR(9) written 5 times. But its not exactly aligned.
Any suggestions on this?
INSERT INTO ar.emp VALUES
(
NOT NULL                                        --  EMP_ID                NUMBER(15,0)
,NOT NULL                                       --  ENAME                 VARCHAR2(30)
,NULL                                     --  HIRE_DT               DATE
,NOT NULL                                       --  SALARY                NUMBER(10,2)
);
Re: decode functionality in DBMS_OUTPUT [message #463094 is a reply to message #463090] Tue, 29 June 2010 12:12 Go to previous messageGo to next message
cookiemonster
Messages: 13975
Registered: September 2008
Location: Rainy Manchester
Senior Member
Maybe instead of using tabs you should try rpading the NULL and NOT NULL with spaces to a pre-determined length and then concatenate the rest on the end.
Otherwise you're going to have to use a different number of tabs depending on whether it's NULL or NOT NULL.
Re: decode functionality in DBMS_OUTPUT [message #463099 is a reply to message #463094] Tue, 29 June 2010 12:21 Go to previous message
geneeyuss
Messages: 53
Registered: June 2010
Member
thank you a lot. that solved my problem
Previous Topic: not spooling currently error
Next Topic: Missing records while performing Parallel inserts
Goto Forum:
  


Current Time: Mon Feb 16 07:22:43 CST 2026