Function with for loop and parameter table/column names ... syntax help
I'm trying to create a function that returns the distinct values and counts from a user defined schema/table/column.
The code below defines an object type [stats_on_column_obj] and creates a single table of this type [stats_on_column_tab].
The function is supposed to take three input variables: p_schema_name, p_table_name, p_column_name and return a table (above).
I can hard code a SELECT statement into the () ... but once I try to convert it to parameters & exec immediate I am stuck. The red section is where the issue is (i think).
Oracle 10g.
CREATE TYPE stats_on_column_obj IS OBJECT(
COL_VAL VARCHAR2(500),
COL_VAL_CNT NUMBER (7)
CREATE TYPE stats_on_column_tab IS TABLE OF stats_on_column_obj;
CREATE OR REPLACE FUNCTION get_STATS_ON_COLUMN
p_schema_name IN varchar2,
p_table_name IN varchar2,
p_column_name IN varchar2
RETURN STATS_ON_COLUMN_tab
IS
l_STATS_ON_COLUMN_tab STATS_ON_COLUMN_tab := STATS_ON_COLUMN_tab ();
n INTEGER := 0;
str_select_tbl varchar2(5000);
BEGIN
str_select_tbl := 'SELECT '||p_column_name||' as col_val, count(*) as col_val_cnt FROM '||p_schema_name||'.'||p_table_name||' group by '||p_column_name;
FOR r IN (str_select_tbl)
LOOP
l_STATS_ON_COLUMN_tab.EXTEND;
n := n + 1;
l_STATS_ON_COLUMN_tab (n) := STATS_ON_COLUMN_obj (r.col_val, r.col_val_cnt);
END LOOP ;
RETURN l_STATS_ON_COLUMN_tab;
END;
[Error] PLS-00103 (124: 4): PLS-00103: Encountered the symbol "LOOP" when expecting one of the following: * & - + / at mod remainder rem .. <an exponent (**)> || multiset year DAY_
[Error] PLS-00103 (126: 9): PLS-00103: Encountered the symbol "=" when expecting one of the following: constant exception <an identifier> <a double-quoted delimited-identifier> table LONG_ double ref char time timestam
[Error] PLS-00103 (127: 29): PLS-00103: Encountered the symbol "(" when expecting one of the following: constant exception <an identifier> <a double-quoted delimited-identifier> table LONG_ double ref char time timestam
[Error] PLS-00103 (128: 4): PLS-00103: Encountered the symbol "END" when expecting one of the following: begin function package pragma procedure subtype type use <an identifier> <a double-quoted delimited-identifier> form
SELECT * FROM TABLE (get_STATS_ON_COLUMN('SCHEMAS_X','TABLE_X','COLUMN_X'));
SCOTT@orcl > CREATE OR REPLACE
2 FUNCTION get_STATS_ON_COLUMN(
3 p_schema_name IN varchar2,
4 p_table_name IN varchar2,
5 p_column_name IN varchar2
6 )
7 RETURN STATS_ON_COLUMN_tab
8 IS
9 v_STATS_ON_COLUMN_tab STATS_ON_COLUMN_tab := STATS_ON_COLUMN_tab ();
10 v_n INTEGER := 0;
11 v_str_select_tbl VARCHAR2(5000);
12 BEGIN
13 v_str_select_tbl := 'SELECT stats_on_column_obj(' || p_column_name || ',count(*)) FROM ' ||
14 p_schema_name || '.' || p_table_name || ' group by ' || p_column_name;
15 EXECUTE IMMEDIATE v_str_select_tbl
16 BULK COLLECT
17 INTO v_STATS_ON_COLUMN_tab;
18 RETURN v_STATS_ON_COLUMN_tab;
19 END;
20 /
Function created.
SCOTT@orcl > select *
2 from table(
3 get_STATS_ON_COLUMN(
4 'SCOTT',
5 'EMP',
6 'JOB'
7 )
8 )
9 /
COL_VAL COL_VAL_CNT
CLERK 4
SALESMAN 4
PRESIDENT 1
MANAGER 3
ANALYST 2
SCOTT@orcl >
Or better change it to pipelined function.
SY.
Similar Messages
-
Syntax of DDL options and related (table) column names
Hi,
where can I find something like a mapping between DDL options and related table column names?
For example I do have the table options PCTFREE, FREELISTS and NOCOMPRESS. The related table columns out of user_tables are PCT_FREE, FREELIST and COMPRESS.
PCT(_)FREE wins an Underscore, FREELIST(S) wins an "S" and it is "NOCOMPRESS" if COMPRESS has a value "Y(es)".
Hope somebody can help.So far I didn't find any information that is not in
the DDL script gernerated from
DBMS_METADATA.get_DDL.Alright, I give you an example:
I create a table with the following DDL:
"CREATE TABLE IntBuch (
int_bunr integer NOT NULL,
int_sdat double precision NOT NULL,
int_hblz char(8) NOT NULL,
int_hkto char(7) NOT NULL,
int_hdat double precision NOT NULL,
KtoNr char(7) NOT NULL,
BLZ char(8) NOT NULL,
CONSTRAINT PK_IntBuch PRIMARY KEY (int_bunr)
USING INDEX
PCTFREE 10
STORAGE (
INITIAL 1000
NEXT 500
PCTINCREASE 0
MINEXTENTS 1
MAXEXTENTS 4096
PCTFREE 20
LOGGING
CREATE UNIQUE INDEX intid
ON IntBuch (int_bunr DESC)
CREATE INDEX hkto
ON IntBuch (int_hblz,int_hkto)
COMMENT ON TABLE IntBuch
IS 'Kommentar zu DB-Tabelle InBuch'
COMMENT ON COLUMN IntBuch.int_sdat IS 'Kommentar zu DB-Spalte int_sdat'
ALTER TABLE IntBuch
ADD CONSTRAINT Gutschrift FOREIGN KEY (int_hkto,int_hblz) REFERENCES Konto
ON DELETE CASCADE
ADD FOREIGN KEY (KtoNr,BLZ) REFERENCES Konto
ADD FOREIGN KEY (int_bunr) REFERENCES Buchung
ON DELETE CASCADE
After that I read the DDL with DBMS_METADATA.get_DDL and I get
" CREATE TABLE "UOENDE"."INTBUCH"
( "INT_BUNR" NUMBER(*,0) NOT NULL ENABLE,
"INT_SDAT" FLOAT(126) NOT NULL ENABLE,
"INT_HBLZ" CHAR(8) NOT NULL ENABLE,
"INT_HKTO" CHAR(7) NOT NULL ENABLE,
"INT_HDAT" FLOAT(126) NOT NULL ENABLE,
"KTONR" CHAR(7) NOT NULL ENABLE,
"BLZ" CHAR(8) NOT NULL ENABLE,
CONSTRAINT "PK_INTBUCH" PRIMARY KEY ("INT_BUNR")
USING INDEX PCTFREE 10 INITRANS 2 MAXTRANS 255 COMPUTE STATISTICS
STORAGE(INITIAL 16384 NEXT 1048576 MINEXTENTS 1 MAXEXTENTS 2147483645
PCTINCREASE 0 FREELISTS 1 FREELIST GROUPS 1 BUFFER_POOL DEFAULT)
TABLESPACE "USERS" ENABLE,
CONSTRAINT "GUTSCHRIFT" FOREIGN KEY ("INT_HKTO", "INT_HBLZ")
REFERENCES "UOENDE"."KONTO" ("KTONR", "BLZ") ON DELETE CASCADE ENABLE,
FOREIGN KEY ("KTONR", "BLZ")
REFERENCES "UOENDE"."KONTO" ("KTONR", "BLZ") ENABLE,
FOREIGN KEY ("INT_BUNR")
REFERENCES "UOENDE"."BUCHUNG" ("BU_NR") ON DELETE CASCADE ENABLE
) PCTFREE 20 PCTUSED 40 INITRANS 1 MAXTRANS 255 NOCOMPRESS LOGGING
STORAGE(INITIAL 65536 NEXT 1048576 MINEXTENTS 1 MAXEXTENTS 2147483645
PCTINCREASE 0 FREELISTS 1 FREELIST GROUPS 1 BUFFER_POOL DEFAULT)
TABLESPACE "USERS"
If there are no more DDLOptions possible than that, it is fine for me. If not, but all kinds of options are to find in only one or two tables of the database ( user_tables for table options, dba_constraints and dba_segments for column options, primary and foreign keys, user_indexes and user_segments for indexes ...), that is fine for me, too. (As long as I get to know which tables are "sufficient" for that.) If it is different than both of that, that's bad.
However if you want to use defaults instead of
absolut values, it is better to remove certain parts,
like the storage clause, from the generated output.I understand this, but I am more interested to have all and not leaving out some of them in order to have the defaults in the database after running the DDL.
A totally different approach could be to
a) create a database link from one DB to another.
b) create table <new_table> as select * from
<old_table@dbLink> where 1=2;I keep that in mind, thanks for that hint. -
Help with for loop and remove-item command
Hi all here is the code so far
$a = Get-Content C:\Users\cody-horton\Desktop\test.txt
for($i=0;$i -lt $a.Length;$i++){
#$temp = [int32]::Parse($a[$i])
Remove-Item '\\ceit2551202x0'$a[$i]'\C$\Program Files\MATLAB\R2013a\*' -Force
I have a text file with a few number in it that i want to get. They are in the array $a but I'm getting an error that says a positional parameter cannot be found that accepts argument '53' (it does this for all the numbers in the array.) I know in java you
could have something like this string + int + string.
I'm not sure what to do I also tried to parse it with no luck. Thanks any help is appreciated.Your issue is with the path for Remove-Item, it is not complete so try this
Remove-Item "\\ceit2551202x0$($a[$i])\C$\Program Files\MATLAB\R2013a\*" -Force
I am unable to test, so I would add the -WhatIf switch to make sure it outputs what you expect
If you find that my post has answered your question, please mark it as the answer. If you find my post to be helpful in anyway, please click vote as helpful.
Don't Retire Technet -
My app store is not working after installing mavericks. When I open app store it repeatedly asking me to login with apple ID and to provide User name and Password for proxy authentication in a loop.I am a newbie to mac,Please help me.
Hmmmm... would appear that you need to be actually logged in to enable the additional menu features.
Have you tried deletting the plists for MAS?
This page might help you out...
http://www.macobserver.com/tmo/answers/how_to_identify_and_fix_problems_with_the _mac_app_store
Failing that, I will have to throw this back to the forum to see if anyone else can advise further.
Let me know how you get on?
Thanks. -
Extending a function with logic stored in a table?
Let's say I have a function with local variables and I have a select statement in it. This statement populates (using into) these variables, one of which contains a condition which has the name of some local variables in it. I would like for this to be evaluated. Is this possible?
for example, if I have variables named local_value1, local_value2 and local_condition and local_condition is loaded from a table column in which the expression is: local_value1 < local_value2
I'd then need to do a: if local_condition then blah....
I've been looking at ways to use execute immediate to do this, but it doesn't look like I can do that, as it wont do substitution of local variables, only bound variables.
I've done this with various scripting languages in the past, but I've not seen anything on the forums on how to do this with pl/sql.
Any suggestions?wether this is really meaningful or not ... here is a way:
michaels> CREATE TABLE demo_logic
( demo_logic_id NUMBER,
demo_logic_statement VARCHAR2(4000),
CONSTRAINT demo_logic_pk PRIMARY KEY (demo_logic_id) ENABLE
Table created.
michaels> -- The data:
michaels> INSERT INTO demo_logic
VALUES (1, 'local_value1 < local_value2')
1 row created.
michaels> INSERT INTO demo_logic
VALUES (2, '(local_value1 = local_value2) or (local_value1 < 7)')
1 row created.
michaels> -- Then the function:
michaels> CREATE OR REPLACE FUNCTION func_demo (condition_id IN NUMBER)
RETURN NUMBER
IS
local_value1 NUMBER := 5;
local_value2 NUMBER := 2;
local_value_to_return NUMBER := 0;
local_condition demo_logic.demo_logic_statement%TYPE;
BEGIN
SELECT demo_logic_statement
INTO local_condition
FROM demo_logic
WHERE demo_logic_id = condition_id;
--next we would evaluate 'local_condition' and if true we would then continue and do more work
--and assign something to local_value_to_return.
EXECUTE IMMEDIATE 'DECLARE
local_value1 number := :1;
local_value2 number := :2;
BEGIN
IF '
|| local_condition
|| ' THEN
:out := 1;
ELSE
:out := 0;
END IF;
END;
USING local_value1, local_value2, OUT local_value_to_return;
RETURN local_value_to_return;
END func_demo;
Function created.
michaels> SELECT func_demo ('1') res
FROM DUAL
RES
0
1 row selected.
michaels> SELECT func_demo ('2') res
FROM DUAL
RES
1 -
Function Modules for Data and Time
Hi all,
I need 2 function modules for date and time. when we pass current data(sy-datum) and current time (sy-uzeit) into function modules, shoud get date in <b>dd/mm/yyyy or dd.mm.yyyy</b> and time in<b> HH:MM:SS</b> formats.
Thanks in advanceHi Ranjith,
i think this will b usefull for you..
SAP Bar Chart Function Modules and what they are used for
Function module
Used for
BARC_GRAPHIC_PBO
Starting bar chart at PBO time, using a graphic profile (parameter PROFILE)
BARC_GRAPHIC_PAI
Analyzing data returned by the graphic
BARC_SET_TIME_AXIS
Setting start and end of time axis
BARC_SET_OPTIONS
Setting options
BARC_ADD_CHART
Creating a chart
BARC_SET_CHART_ATTRIB
Setting chart attributes
BARC_ADD_SECTION
Creating a section on the time axis
BARC_SET_SECTION_ATTRIB
Setting section attributes
BARC_ADD_RIBBON
Adding a ribbon to the time axis
BARC_SET_RIBBON_ATTRIB
Setting attributes for ribbons in the chart
BARC_ADD_GRID
Adding a time grid
BARC_SET_GRID_ATTRIB
Setting grid attributes
BARC_ADD_LAYER
Adding a layer (graphic elements)
BARC_SET_LAYER_ATTRIB
Setting layer attributes
BARC_ADD_LINE
Adding a line
BARC_ADD_CALENDAR
Creating a calendar
BARC_SET_CALENDAR_ATTRIB
Setting attributes for a calendar
BARC_ADD_TIME_PROFILE
Creating time profiles
BARC_SET_TIME_PROFILE_ATTRIB
Setting attributes for time profile
BARC_ADD_INTERVAL
Adding a time interval
BARC_SET_INTERVAL_ATTRIB
Setting time interval attributes
BARC_ADD_TIME_OBJECT
Creating a time object
BARC_CONVERT_DATE
Creating a date string in bar chart format
BARC_REVERT_DATE
Converting a date string in bar chart format to date and time
BARC_ADD_DATELINE
Creating a date line
BARC_SET_DATELINE_ATTRIB
Setting dateline attributes
BARC_GET_PROFILE_CONTENTS
Obtaining profile contents for customizing a chart
BARC_GET_COLUMN_WIDTH
Selecting new column width
BARC_SET_COLUMN_WIDTH
Setting the column width
BARC_GET_TEXTINDEX
Obtaining the text index of a field
BARC_SET_LABELS
Positioning the chart display
BARC_SET_COLUMN_ATTRIB
Setting column attributes
BARC_SET_ROW_ATTRIB
Setting row attributes
BARC_SET_ROW_HEIGHT
Setting the line height
BARC_SET_MAXCHARTS
Setting the maximum number of charts sent
<b>If its usefull reward points
</b> -
How to use reverse function in FOR loop
Hi,
Can we use REVERSE function in for loop.
If so can u please give an explanation with some example.
Thanks in advance.
Satya.In SQL you could do this...
SQL> ed
Wrote file afiedt.buf
1 select empno, ename, replace(sys_connect_by_path(ch,'|'),'|') as rev_ename
2 from (
3 select empno, ename, case when rn <= length(ename) then substr(ename, length(ename)-rn+1, 1) else null end as ch, rn
4 from emp
5 ,(select rownum as rn from dual connect by rownum <= (select max(length(ename)) from emp))
6 where case when rn <= length(ename) then substr(ename, length(ename)-rn+1, 1) else null end is not null
7 )
8 where connect_by_isleaf = 1
9 connect by empno = prior empno and rn = prior rn + 1
10 start with rn = 1
11* order by empno
SQL> /
EMPNO ENAME REV_ENAME
7369 SMITH HTIMS
7499 ALLEN NELLA
7521 WARD DRAW
7566 JONES SENOJ
7654 MARTIN NITRAM
7698 BLAKE EKALB
7782 CLARK KRALC
7788 SCOTT TTOCS
7839 KING GNIK
7844 TURNER RENRUT
7876 ADAMS SMADA
7900 JAMES SEMAJ
7902 FORD DROF
7934 MILLER RELLIM
14 rows selected.
SQL> -
How to save and retrive table column values added via view enhancement
Hi,
I want to enhance standard webdynpro component FITE_VC_REVIEW.
I want to add new column to Fight Info Table and also to Hotel Info table.I can do UI changes via view enhancement .
First Approach: I add new fields to Flight Info and Hotel Info structure.
Create an additional Coolum in Flight Info and Hotel Info Table and Bind it to newly created field in structures.
Question :
What all changes I have to make to save and retrieve values from database table.
Kindly guide with Steps
Second Approach: I create a new table/structure for my requirement and bind table column to these fields.
Question:
What all changes I have to make to save and retrieve values from database table.
Kindly guide with Steps
And which approach will be good.
Regards,
Madhvikano ans yet
-
How to save and retrieve table column values added via view enhancement
Hi,
I want to enhance standard webdynpro component FITE_VC_REVIEW.
I want to add new column to Fight Info Table and also to Hotel Info table.
First Approach: I add new fields to Flight Info and Hotel Info structure.
Create an additional Coolum in Flight Info and Hotel Info Table and Bind it to newly created field in structures.
Question
What all changes I have to make to save and retrieve values from database table.
Kindly guide with Steps
Second Approach: I create a new table/structure for my requirement and bind table column to these fields.
Question:
What all changes I have to make to save and retrieve values from database table.
Kindly guide with Steps
And which approach will be good.
Regards,
Madhvika
Moderator message: please have a look in the dedicated "Web Dynpro ABAP" forum.
Edited by: Thomas Zloch on Feb 16, 2011 3:02 PMno ans yet
-
I have done extensive programming with the SQL Toolkit with LabVIEW versions through 6.1. My customer now wants to upgrade to Windows 7, so I am trying to upgrade to LabVIEW 2009 (my latest purchased version) using the Database Connectivity Toolkit, and the SQL Toolkit Compatibility vis. Everything seemed to be going okay with the higher level SQL operations, but I ran into trouble with the Get Column Name.vi.
The pictures below show the problem. The original SQL Toolkit connected the Prepare SQL.vi with the Get Column Name.vi with a cluster of two references, one for connection, and one for sql. The new compatibility vis have a class conflict in the wire because the Prepare SQL.vi contains a cluster with connection, and command references, but the Get Column Name.vi expects a cluster with connection and recordset references.
How do I resolve this conflict?
Thank You.
DanI've never worked with the old version of the toolkit, so I don't know how it did things, but looking inside the SQL prep VI, it only generates a command, and the the column name VI wants a recordset. I'm not super familiar with all the internals of ADO, but my understanding is that is standard - you only have the columns after you execute the command and get the recordset back. What you can apparently do here is insert the Execute Prepared SQL VI in the middle and that will return what you need.
I'm not sure why it worked before. Maybe the execute was hidden inside the prep VI or maybe you can get the column names out of the command object before execution. In general, I would recommend considering switching to the newer VIs.
Try to take over the world! -
Finding source database table/column name for a column in a view
Hi i need to be able to identify the original database table/column name for a column in a view.
e.g. say i have a view like this
create v1 as select a.name fname, b.name bname, c.name cname,......
from u1.names a, u2.names b. u3.names c
where .....
Now I want to find out that the database table/column name for the fname, bname and cname columns in the view v1, which in this instance is u1.name.name, u2.names.name, u3.names.name.
But i need to be able to do it for any view. Short of parsing the SQL is there an easy way of doing this?
Now obviusly I can't do this for virtual columns but I will know my column is not virtual as it has an index on it.But i need to be able to do it for any view. Short of
parsing the SQL is there an easy way of doing this?No, parsing the SQL is the only way. Good luck it is not something I would want to attempt. -
Remain case for table column name
Hello, i am a java developer. I had helped my company to develop a java application all the while and before this we are using ms sql server database. Recently, we want to migrate the database to oracle database. The problem is, in our application, we use the resultsetmetadata to retrieve all the table column name and respective data and store in hashtable, but in ms sql server the table column name is not all in uppercase. When our application try to get the data using hashtable.get("aColumnName"), it will fail because in oracle the column name is all in uppercase. There are more than 1000 jsp page and java code that used the same method to get data, so if i want to change all the code to uppercase, it will consume a lot of unproductive time, is there any way to solve this?
I assume that your code builds an SQL statement like "select * from ??? where table_name = '????';".
If this is correct just wrap the table_name in UPPER(table_name).
If my assumption is incorrect then please give more information.
James. -
Function modules for material and description
Hi,
Pls let me know the function module for material and description.
Regards,
BalaHi,
Please use FM: MD_MATERIAL_GET_TEXT
Reagrds
Raju -
Function Module for .xlsx to Internal table
Hi Gurus,
Please let me know if there is any function module for .xlsx to internal table.
Kind regards,
VarunHi,
try the below FM's
ALSM_EXCEL_TO_INTERNAL_TABLE
TEXT_CONVERT_XLS_TO_SAP
Thanks,
Vinayaka -
How to use simple types for table column names ?
Hi,
can any one tell how to to use simple types for table column names?
It is required in internationalizing of webdynpro applications.
Regards,
RajeshHi,
1: define required column names in <SimpleType>
2:use the following code to get those values
3:bind 'text' property of Column headers to context attributes
4:take a context attribute 'Value' as type of <SimpleType>
5:set these values to context attributes
IWDAttributeInfo objAttrInfo=wdContext.getNodeInfo().getAttribute(IPrivate<ViewName>View.IContextElement.VALUE);
ISimpleTypeModifiable simple=objAttrInfo.getModifiableSimpleType();
Map m=simple.getEnumerationTexts();
Collection c=m.values();
Iterator it=c.iterator();
if(it.hasNext())
wdContext.currentContextElement.set<att1>(it.next().toString);
if(it.hasNext())
wdContext.currentContextElement.set<att2>(it.next().toString);
if(it.hasNext())
wdContext.currentContextElement.set<att3>(it.next().toString);
Regards
LakshmiNarayana
Maybe you are looking for
-
How do I change Apple ID on iPod 5 and iPad mini while keeping the same ID on iPad and iPhone. I only want to change ID on 2 devices. Currently they share same ID. Thanks
-
User exits/ BADI for batch determination
Hi, I need any user exits or BADI's which are triggered even before the batch determination is done. In One of my requirement ,during TO creation i need to pass, ship to KOMPH table( since SHIP TO is not available during a TO) to make it available fo
-
hi, while trying to upload a csv file to SBO using DTW , the following error occurs. I have changed the data according to specified data type but still the error comes as following, <ErrorMessage>12345 Invalid Code [OHEM.bankCode]Application-defined
-
Visio 2010 reverse engineering ora-12504 error
I am trying to create Database Model Diagram via MS Visio 2010 from my existing Oracle Database. But everytime i am having ORA-12504:TNS:listener was not given the SERVICE_NAME in CONNECT_DATA. If i use Oracle Data Modeler i dont have any error. What
-
Chanage TS Drive & Preserve Historic Back Ups?
I have a 300 GB TS drive (OWC Ministack v. 3) - working fine. Have another OWC external for SuperDuper to do clones before OS and security updates. I would like to go to one external - a 1 Tb and partition so that TS and SuperDuper are on one externa