Below is an example query that I would like my procedure to generate
select *
from Registration
where Loc_ID = 6
AND CROP_ID = 163
AND REG_NAME = 'Apiro MX';
REG_NAME is varchar2()
I have created one procedure, where I want to execute one query like below
query := 'select REG_ID from Registration where loc_id = ' ||
countryid || ' AND Crop_id = ' || cropid ||
' AND Reg_name = '|| ''' || productid || ''' || ';
I am getting error in REG_NAME part, where it is taking productid as " || productid ||"
can you please help me with the exact query for that.
You don't need to use dynamic sql:
CREATE PROCEDURE get_registration (
i_countryid IN REGISTRATION.LOC_ID%TYPE,
i_crop_id IN REGISTRATION.CROP_ID%TYPE,
i_reg_name IN REGISTRATION.REG_NAME%TYPE,
o_cursor OUT SYS_REFCURSOR
)
AS
BEGIN
OPEN o_cursor FOR
SELECT *
FROM Registration
WHERE Loc_ID = i_countryid
AND CROP_ID = i_crop_id
AND REG_NAME = i_reg_name;
END;
/
If you do need dynamic SQL (however, you can almost always do it without):
CREATE PROCEDURE get_registration (
i_countryid IN REGISTRATION.LOC_ID%TYPE,
i_crop_id IN REGISTRATION.CROP_ID%TYPE,
i_reg_name IN REGISTRATION.REG_NAME%TYPE,
o_cursor OUT SYS_REFCURSOR
)
AS
BEGIN
OPEN o_cursor
FOR 'SELECT *
FROM Registration
WHERE Loc_ID = :i
AND CROP_ID = :j
AND REG_NAME = :k'
USING i_countryid, i_crop_id, i_reg_name;
END;
/
In PLSQL you can escape quote by adding another one, therefore,
you should put double quotes around productid.
Try something like:
query := 'select REG_ID from Registration where loc_id = ' ||
countryid || ' AND Crop_id = ' || cropid ||
' AND Reg_name = '|| '''' || productid || '''' || ';
Or don't use dynamic SQL and try creating stored procedure
(check the link for the instruction).
http://plsql-tutorial.com/plsql-procedures.htm
Related
I m trying to execute the procedure but it is showing the errror of missing expression while executing this line execute immediate sqlQuery into test;
My query returns 6 columns which i need to store into the variables , please help to execute this query.
procedure getAttributeOptions(subID number, compID number,docNumber varchar2,transType varchar2 ,rowNum varchar2 ,
Attribute1 out varchar2,Attribute2 out varchar2,Attribute3 out varchar2,Attribute4 out varchar2,
Attribute5 out varchar2, Attribute6 out varchar2) is
sqlQuery varchar2(4000);
storageColumns varchar2(4000);
colName varchar2(1000);
tableName varchar2(1000);
primaryKey varchar2(1000);
test varchar2(4000);
begin
colName:=' ATTRIBUTE_OPTION_1,ATTRIBUTE_OPTION_2,ATTRIBUTE_OPTION_3,ATTRIBUTE_OPTION_4,ATTRIBUTE_OPTION_5,ATTRIBUTE_OPTION_6 ';
storageColumns:=' Attribute1,Attribute2,Attribute3,Attribute4,Attribute5,Attribute6 ';
if transType = TY_ISSUE then
tableName:='preq_master_detail';
primaryKey:='req_number';
elsif transType=TY_TRANSFER then
tableName:='pinv_transfer_detail';
primaryKey:='transfer_id';
elsif transType=TY_RECEIVE then
tableName:='ppo_master_detail';
primaryKey:='po_number';
elsif transType=TY_MANUAL then
tableName:='part_manual_adjustment';
primaryKey:='adjustment_id';
end if;
dbms_output.put_line(tableName);
if tableName is not null then
begin
sqlQuery:='select ' || colName || 'into ' || storageColumns || 'from ' || tableName ;
sqlQuery:=sqlQuery || ' where subscriber_id=' || subID;
sqlQuery:=sqlQuery || ' and company_id=' || compID;
sqlQuery:=sqlQuery || ' and ' || primaryKey ||'='|| '''' || docNumber || '''' || ' and row_number ='||rowNum;
dbms_output.put_line(sqlQuery);
execute immediate sqlQuery into test;--issue in this line
dbms_output.put_line(Attribute1);
dbms_output.put_line(Attribute2);
dbms_output.put_line(Attribute3);
dbms_output.put_line(Attribute4);
dbms_output.put_line(Attribute5);
dbms_output.put_line(Attribute6);
exception when NO_DATA_FOUND then return ;
end;
end if;
end;
You are placing the list of variables for your INTO clause inside the SQL, rather than outside in PL/SQL. You need to use put the INTO clause outside. Hopefully you can hard-code that clause:
sqlQuery:='select ' || colName || 'from ' || tableName ;
. . .
execute immediate sqlQuery into Attribute1,Attribute2,Attribute3,Attribute4,Attribute5,Attribute6;
While you are at it, you should use bind variables for your WHERE clause:
. . .
sqlQuery:=sqlQuery || ' where subscriber_id=:subid';
sqlQuery:=sqlQuery || ' and company_id=:compID';
sqlQuery:=sqlQuery || ' and ' || primaryKey ||'= :docno'|| ' and row_number =:rowNum';
execute immediate sqlQuery
INTO Attribute1,Attribute2,Attribute3,Attribute4,Attribute5,Attribute6
USING subid,compid,docNumber,rowNum;
That will make your DBA happy.
I am writing a plpgsql function that should update a table based on a provided JSON object. The JSON contains a table representation with all the same columns as the table itself has.
The function currently looks as follows:
CREATE OR REPLACE FUNCTION update (updated json)
BEGIN
/* transfrom json to table */
WITH updated_vals AS (
SELECT
*
FROM
json_populate_recordset(NULL::my_table, updated)
),
/* Retrieve all columns from mytable and also with reference to updated_vals table */
cols AS (
SELECT
string_agg(quote_ident(columns), ',') AS table_cols,
string_agg('updated_vals.' || quote_ident($1), ',') AS updated_cols
FROM
information_schema
WHERE
table_name = 'my_table' -- table name, case sensitive
AND table_schema = 'public' -- schema name, case sensitive
AND column_name <> 'id' -- all columns except id and user_id
AND column_name <> 'user_id'
),
/* Define the table columns separately */
table_cols AS (
SELECT
table_cols
FROM
cols
),
/* Define the updated columns separately */
updated_cols AS (
SELECT
updated_cols
FROM
cols)
/* Execute the update statement */
EXECUTE 'UPDATE my_table'
|| ' SET (' || table_cols::text || ') = (' || updated_cols::text || ') '
|| ' FROM updated_vals '
|| ' WHERE my_table.id = updated_vals.id '
|| ' AND my_table.user_id = updated_vals.user_id';
COMMIT;
END;
I noticed that the combination of the WITH clause combined with the EXECUTE will always trigger the error syntax error at or near EXECUTE, even if those are very simple and straightforward. Is this indeed the case, and if so, what would be an alternative approach to provide the required variables (updated_vals, table_cols and updated_cols) to EXECUTE?
If you have any other improvements on this code I'd be happy to see those for I am very new to sql/plpgsql.
If you wrote table name (my_table) in your function, this means that you will update always only one specified table from JSON data. Because of this, you can write table names and column names in your function manually, not using information_schema. This is the simple and easy way.
For example:
CREATE OR REPLACE FUNCTION rbac.update_users_json(updated json)
RETURNS boolean
LANGUAGE plpgsql
AS $function$
begin
update rbac.users usr
set
username = jsn.username,
first_name = jsn.first_name,
last_name = jsn.last_name
from (
select * from json_populate_recordset(NULL::rbac.users, updated)
) jsn
where jsn.id = usr.id;
return true;
END;
$function$
;
For dynamic tables:
CREATE OR REPLACE FUNCTION rbac.update_users_json_dynamic(updated json)
RETURNS boolean
LANGUAGE plpgsql
AS $function$
declare
f record;
exec_sql text;
sep text;
begin
exec_sql = 'update rbac.users usr set ' || E'\n';
sep = '';
for f in
select clm.column_name
from
information_schema."tables" tbl
inner join
information_schema."columns" clm on
clm.table_name = tbl.table_name
and clm.table_schema = tbl.table_schema
where
tbl.table_schema = 'test'
and tbl.table_name = 'users'
and clm.column_name <> 'id'
loop
exec_sql = exec_sql || sep || f.column_name || ' = ' || 'jsn.' || f.column_name;
sep = ', ' || E'\n';
end loop;
exec_sql = exec_sql || E'\n' || 'from (select * from json_populate_recordset(NULL::rbac.users, ''' ||
updated::text || ''')) jsn ' || E'\n' || 'where jsn.id = usr.id';
execute exec_sql;
return true;
END;
$function$
;
I am trying to generate a Create Table Statement. To do this, I have created 2 procedures to Extract Tables and Columns from my Schema. The Output of these procedures is then spooled to a SQL file and is supposed to look like this:
The Formatted Output
Right now, My output Looks like this:
----
---- Run on October 04, 2020 at 22:00
----
-- Start Extracting table IMAGE
CREATE TABLE IMAGE (
MFR CHAR(3,)
, PRODUCT CHAR(5,)
, IMAGE BLOB(4000,)
, TECHSPECS BFILE(530,)
);-- END of Table IMAGE creation
--
--
-- Start Extracting table ORDERS
CREATE TABLE ORDERS (
ORDERNUM NUMBER(227,0)
, ORDERDATE DATE(7,)
, CUST NUMBER(223,0)
, REP NUMBER(223,0)
, MANUF CHAR(3,)
, PROD CHAR(5,)
, QTY NUMBER(225,0)
, AMOUNT NUMBER(225,2)
);-- END of Table ORDERS creation
--
--
-- Start Extracting table PRODUCTS
CREATE TABLE PRODUCTS (
MFR CHAR(3,)
, PRODUCT CHAR(5,)
, DESCRIPTION VARCHAR2(100,)
, PRICE NUMBER(225,2)
, QTYONHAND NUMBER(225,0)
);-- END of Table PRODUCTS creation
--
--
---- Oracle Catalog Extract Utility V1.0 ----
---- Run on October 04, 2020 at 22:00
As you can see, the Data types, along with their Data_length, Data_scale, and DAta_precision are not perfectly formatted as per the requirements. I also haven't been able to make a column that displays the null status of each column. The Date, BFILE, and BLOB as shown in the Output are especially troublesome for me to format properly.
This is my code as of now:
SET ECHO OFF
SET FEEDBACK ON
SET WRAP OFF
--The Procedure to Extract The Columns
SET SERVEROUTPUT ON
CREATE OR REPLACE PROCEDURE Extract_Columns (
--Creating Variables of passed values
wSee IN OUT varchar2,
wTable IN USER_TABLES.table_name%type
)
AS
--The Cursor to run through the Columns
CURSOR Extract_C IS
SELECT COLUMN_NAME, DATA_TYPE, DATA_PRECISION, DATA_SCALE, DATA_LENGTH
FROM USER_TAB_COLUMNS
WHERE TABLE_NAME = wTable
ORDER BY Column_ID;
CurrentRow Extract_C%ROWTYPE;
--Creating variables
wItterations NUMBER(1):=0;
BEGIN
FOR CurrentRow IN Extract_C LOOP
IF wItterations = 0 THEN
wsee := wsee || CHR(10) || ' ' || RPAD(currentrow.column_name, 15) || currentrow.data_type || '(' || currentrow.DATA_LENGTH || currentrow.DATA_SCALE || ')';
ELSE
wsee := wsee || CHR(10) || ', ' || RPAD(currentrow.column_name, 15) || currentrow.data_type || '(' || currentrow.DATA_LENGTH || currentrow.DATA_PRECISION || ',' || currentRow.DATA_SCALE || ')';
END IF;
wItterations:= wItterations +1;
END LOOP;
END;
/
SHOW ERRORS;
SET SERVEROUTPUT ON
CREATE OR REPLACE PROCEDURE Extract_Tables
AS
l_crt varchar2(356);
CURSOR Extract_T IS
SELECT TABLE_NAME
FROM USER_TABLES
ORDER BY TABLE_NAME;
CurrentRow Extract_T%ROWTYPE;
--Creating Variables
wvers VARCHAR2(256) := 'V1.0';
wcur_Tim VARCHAR2(200) := '' || TO_CHAR( CURRENT_DATE, 'Month DD, YYYY') || ' at ' || To_CHAR(CURRENT_DATE, 'HH24:MI');
wheader VARCHAR2(45) := 'CREATE TABLE ' || CurrentRow.table_name ||' (';
wspacing NUMBER := length(wheader);
BEGIN
DBMS_OUTPUT.PUT_LINE('---- Oracle Catalog Extract Utility ' || wvers || ' ----');
DBMS_OUTPUT.PUT_LINE('----');
DBMS_OUTPUT.PUT_LINE('---- Run on ' || wcur_Tim);
DBMS_OUTPUT.PUT_LINE('----');
FOR CurrentRow IN Extract_T LOOP
DBMS_OUTPUT.PUT_LINE('-- Start Extracting table ' || CurrentRow.table_name || '');
DBMS_OUTPUT.PUT_LINE('CREATE TABLE ' || CurrentRow.table_name ||' (');
--This is where i should be calling the other procedure
Extract_Columns(l_crt, CurrentRow.table_name);
--This is where create tables procedure begins again
DBMS_OUTPUT.PUT_LINE(l_crt || chr(10) || LPAD(' ', 15 + length(currentrow.Table_name)) || ');' || '-- END of Table ' || currentrow.Table_name || ' creation');
DBMS_OUTPUT.PUT_LINE('--' || CHR(10) || '--' );
l_crt := NULL;
END LOOP;
--Ending Statement
DBMS_OUTPUT.PUT_LINE('---- Oracle Catalog Extract Utility ' || wvers || ' ----');
DBMS_OUTPUT.PUT_LINE('---- Run on ' || wcur_Tim);
END;
/
SHOW ERRORS;
To execute, I have to compile the 'Extract_column' procedure and then 'Extract_tables' procedure, then I run the tables procedure to generate my output.
How can I format my current output in a way that it exactly matches the picture above?
I am using execute immediate statement in one of my queries.
procedure p1 (p_pk1_column, p_pk2_column , p_conv_table_name ,p_MODUE_NAME )
is
v_select_string := 'SELECT'''||p_MODUE_NAME||''',''' ||p_pk1_column || ''',''' || p_pk2_column ||''' FROM ' ||p_conv_table_name || v_where_condition;
execute immediate v_select_string ;
dbms_output.put_line('string:'||v_select_string );
end p1;
Here I am calling p1 procedure in another procedure p2
PROCEDURE P2 IS
v_pk1_column:='a';
v_pk2_columnm:='b';
v_mod_name:='mOD1';
p1(v_pk1_column,v_pk2_columnm);
end p2;
/
In p2 procedure a, b are the column names of p_conv_table_name . I want to execute the select statement like select p_mod_name, a, b from p_conv_table_name where condition; so that it should give values for a and b columns in p_conv_table_name .
But it is executing like select p_mod, p_pk1_col,p_pk2_col from p_conv_table_name where condition;
So simply column names are selecting instead of values in that column.
Please suggest some approach to achieve values in that column.
Thanks in advance
When the SELECT statement is built the column names are surrounded in single-quotes, which turns them into string literals. Change your procedure to something like:
CREATE OR REPLACE PROCEDURE P1 (p_pk1_column IN VARCHAR2,
p_pk2_column IN VARCHAR2,
p_conv_table_name IN VARCHAR2,
p_MODUE_NAME IN VARCHAR2)
IS
v_select_string VARCHAR2(2000);
v_where_condition VARCHAR2(2000) := ' WHERE SOMETHING = SOMETHING_ELSE';
csr SYS_REFCURSOR;
v_val_1 VARCHAR2(2000);
v_val_2 VARCHAR2(2000);
v_mod_name VARCHAR2(2000);
BEGIN
v_select_string := 'SELECT ' || p_MODUE_NAME || ',' ||
p_pk1_column || ',' ||
p_pk2_column ||
' FROM ' || p_conv_table_name ||
v_where_condition;
dbms_output.put_line('string:' || v_select_string);
OPEN csr FOR v_select_string;
LOOP
FETCH csr INTO v_mod_name, v_val_1, v_val_2;
EXIT WHEN csr%NOTFOUND;
DBMS_OUTPUT.PUT_LINE('v_mod_name=''' || v_mod_name || ''' ' ||
'v_val_1=''' || v_val_1 || ''' ' ||
'v_val_2=''' || v_val_2 || '''');
END LOOP;
CLOSE csr;
END P1;
I've also changed the code to OPEN and FETCH a cursor rather than using EXECUTE IMMEDIATE. OPEN and FETCH are generally more appropriate for use with a dynamic SELECT statement.
Share and enjoy.
This question is unlikely to help any future visitors; it is only relevant to a small geographic area, a specific moment in time, or an extraordinarily narrow situation that is not generally applicable to the worldwide audience of the internet. For help making this question more broadly applicable, visit the help center.
Closed 9 years ago.
To whom it may respond to,
We would like to use SELECT function with PIVOT option at a 11g r2 Oracle DBMS.
Our query is like :
select * from
(SELECT o.ship_to_customer_no, ol.item_no,ol.amount
FROM t_order o, t_order_line ol
WHERE o.NO = ol.order_no and ol.item_no in (select distinct(item_no) from t_order_line))
pivot --xml
( SUM(amount) FOR item_no IN ( select distinct(item_no) as item_no_ from t_order_line));
As can be seen, XML is commented out, if run as PIVOT XML it gives the correct output in XML format, but we are required to get the data as unformatted pivot data, but this sentence throws error :
ORA-00936: missing expression
Any resolutions or ideas would be welcomed,
Best Regards
-------------dirty but working proc is below------------------------
updated the procedure by 17.01.2011 16:39 GMT :
PROCEDURE pr_pivot_item_by_ship_to (
p_location_code IN t_customer.location_code%TYPE,
p_customer_price_group IN t_customer.customer_price_group%TYPE,
p_shipment_date IN t_order.shipment_date%TYPE,
p_fasdat_status IN t_order.fasdat_status%TYPE,
p_order_type IN t_order.order_type%TYPE,
cur_pivot_item_by_ship_to OUT sys_refcursor
)
IS
v_sql VARCHAR2 (15000);
v_pivot_items VARCHAR2 (15000) := '';
v_query_items VARCHAR2 (15000) := '';
v_pivot_orders VARCHAR2 (15000) := '';
v_continue INT := 0;
BEGIN
/*GET ORDER NUMBERS*/
FOR cur_order_loop IN (SELECT DISTINCT (o.NO) AS order_no
FROM t_order o,
vw_customer_with_ship_to_info wwc
WHERE wwc.customer_price_group =
p_customer_price_group
AND wwc.location_code =
p_location_code
AND o.shipment_date = p_shipment_date
AND o.fasdat_status = p_fasdat_status
AND o.order_type = p_order_type
AND wwc.NO = o.customer_no)
LOOP
v_pivot_orders :=
''',''' || TO_CHAR (cur_order_loop.order_no)
|| v_pivot_orders;
v_pivot_orders := LTRIM (v_pivot_orders, ''',''');
END LOOP;
/*USE ORDER NUMBERS TO FIND ITEMS TO PIVOT BY SHIPMENT PLACE*/
FOR cur_loop IN
(SELECT DISTINCT (ol.item_no) AS item_no,
REPLACE
(REPLACE (SUBSTR (i.description, 1, 20), '''',
''),
'"',
' inch'
) AS description
FROM t_order_line ol, t_item i
WHERE ol.item_no = i.NO
AND ol.order_no IN (
SELECT DISTINCT (o.NO) AS order_no
FROM t_order o,
vw_customer_with_ship_to_info wwc
WHERE wwc.customer_price_group =
p_customer_price_group
AND wwc.location_code =
p_location_code
AND o.shipment_date = p_shipment_date
AND o.fasdat_status = p_fasdat_status
AND o.order_type = p_order_type
AND wwc.NO = o.customer_no))
LOOP
v_query_items := ',''' || cur_loop.item_no || '''' || v_query_items;
v_pivot_items :=
','''
|| cur_loop.item_no
|| ''' as "ad_'
|| cur_loop.description
|| '"'
|| v_pivot_items;
END LOOP;
v_query_items := LTRIM (v_query_items, ',');
v_pivot_items := LTRIM (v_pivot_items, ',');
v_sql :=
'select * from
(SELECT wwc.ship_to_customer_no||''-''|| wwc.ship_to_customer_name as "Müst. Adi ('
|| p_order_type
|| ')", ol.item_no,ol.amount
FROM t_order o, t_order_line ol,vw_customer_with_ship_to_info wwc
WHERE o.NO = ol.order_no
and wwc.no = o.customer_no
and ol.order_no in (
(SELECT DISTINCT (o.NO) AS order_no
FROM t_order o,
vw_customer_with_ship_to_info wwc
WHERE wwc.customer_price_group ='''
|| p_customer_price_group
|| '''
AND wwc.location_code =
'''
|| p_location_code
|| '''
AND o.shipment_date = '''
|| p_shipment_date
|| '''
AND o.fasdat_status = '
|| p_fasdat_status
|| '
AND o.order_type = '''
|| p_order_type
|| '''
AND wwc.NO = o.customer_no)
)
and OL.ITEM_NO in ('
|| v_query_items
|| ')
)
pivot
( SUM(amount) FOR item_no IN ('
|| v_query_items --v_pivot_items
|| '))';
--DBMS_OUTPUT.put_line ('TSQL ' || v_sql);
-- OPEN cur_pivot_item_by_ship_to FOR
-- SELECT v_sql
-- FROM DUAL;
BEGIN
OPEN cur_pivot_item_by_ship_to FOR v_sql;
EXCEPTION
WHEN NO_DATA_FOUND
THEN
NULL;
WHEN OTHERS
THEN
IF SQLCODE = -936
THEN
NULL;
ELSE
pck_helper.pr_log_error
(SQLCODE,
'p_shipment_date:'
|| p_shipment_date
|| ','
|| 'cur_pivot_item_by_ship_to err. :'
|| SQLERRM,
'pr_pivot_item_by_ship_to'
);
END IF;
END;
EXCEPTION
WHEN NO_DATA_FOUND
THEN
NULL;
WHEN OTHERS
THEN
pck_helper.pr_log_error (SQLCODE,
'p_shipment_date:'
|| p_shipment_date
|| ','
|| SQLERRM,
'pr_pivot_item_by_ship_to'
);
END pr_pivot_item_by_ship_to;
END pkg_report;
I don't have an Oracle 11 instance available to me now (PIVOT is new in Oracle 11) so I can only speculate. My guess is that the -- has ended up commenting out everything after the pivot, and Oracle has given you a 'missing expression' error because it was expecting to find something after pivot. Have you tried commenting out xml by surrounding it with /* and */ instead of putting -- before it?
If you were running this query from Java, say, then I would expect you to get a 'missing expression' error if you attempted to run the SQL in the following string:
String sql =
"select * from (SELECT o.ship_to_customer_no, ol.item_no,ol.amount " +
"FROM t_order o, t_order_line ol " +
"WHERE o.NO = ol.order_no and ol.item_no in (select distinct(item_no) from t_order_line)) " +
"pivot --xml " +
"( SUM(amount) FOR item_no IN ( select distinct(item_no) as item_no_ from t_order_line))";
Because this SQL string gets concatenated into one line, the -- in front of xml will cause Oracle to ignore everything in the query after it.
EDIT: In the procedure you added, what concerns me is this part (abbreviated):
v_sql := 'begin
select * from ...;
end;';
open DENEME for
select v_sql from dual;
I don't understand why you're using BEGIN and END within v_sql. Try removing them.
Also, take care not to leave a trailing semicolon in your SQL statement string. The following will give an ORA-00911: invalid character error if you try to run it using EXECUTE IMMEDIATE or suchlike:
v_sql := 'select * from dual;'; /* note the semicolon inside the string */
but the following will be OK:
v_sql := 'select * from dual';
Finally, it appears you want to return the results of this SQL query, not the query itself as a string. Replacing your open DENEME for ... statement with the following should do what you want:
open DENEME for v_sql;
EDIT 2: In your procedure, there is a commented-out call to DBMS_OUTPUT.PUT_LINE. Have you verified that the SQL generated here is correct? In particular, are you sure that none of the values used to form v_query_items and v_pivot_items have ' characters in them?
It may be that there is a problem using PIVOT with dynamic SQL. I don't know, and can't help much more because I don't have access to Oracle 11 here. Do you still get errors if you simplify the SQL to a much smaller query, but one that still has PIVOT in it? In other words, find a simple PIVOT query that works (e.g. you can run it successfully in SQL Developer or suchlike), write a procedure such as the following, and see whether you get any data back from it:
CREATE OR REPLACE PROCEDURE dynamic_pivot_test(
results OUT SYS_REFCURSOR
)
AS
BEGIN
OPEN results FOR 'SELECT ...'; /* put the PIVOT query here. */
END;
/
the procedure edited at 17.01.2011 is dirty but working, looping cursors should change, better err. handling for the dynamic sql ( currently second to none), will try to improve it when got time. Thank you all for your help.