I want to count the rows of a number of tables. But the table name should be used dynamically. I want to do that within one SQL statement.
I tried it with
BEGIN ATOMIC
FOR tmp AS (
SELECT tabschema || '.' || tabname tbl
FROM syscat.tables WHERE tabname LIKE '%CD') DO
(SELECT COUNT(*) FROM tmp.tbl);
END FOR;
END
but I receive the error
DB21034E The command was processed as an SQL statement because it was not a
valid Command Line Processor command. During SQL processing it returned:
SQL0204N "TMP.TBL" is an undefined name. LINE NUMBER=1. SQLSTATE=42704
and found no other working solution...
Is there a solution for that?
Thanks in advance.
I assume that your SELECT COUNT(*) FROM tmp.tbl should translate in multiple statements like
select count(*) from TABLECD
select count(*) from TABLE2CD
...
However, your query will try to do a count of the table TBL in the schema TMP.
You'll have to prepare the complete SQL statement, store it in a variable and pass it to the PREPARE statement (documentation ).
A rather complete stored procedure which somewhat fits your requirements can be found here . The result of the counts will be stored in a table COUNTERS which you can query afterwards.
//edit: this is the example from the topic, adapt to work (not tested since I have no DB2 instance to test atm):
CREATE PROCEDURE tableCount()
LANGUAGE SQL
BEGIN
DECLARE SQLCODE INTEGER DEFAULT 0;
DECLARE SQLSTATE CHAR(5);
DECLARE vTableName VARCHAR(20);
DECLARE vTableCount INTEGER;
DECLARE stmt varchar(2000);
DECLARE not_found CONDITION FOR SQLSTATE '02000';
DECLARE c1 CURSOR FOR
SELECT tabname from syscat.tables where tabschema='DB2ADMIN';
DECLARE C2 CURSOR FOR S2
DECLARE CONTINUE HANDLER FOR not_found
SET stmt = '';
Delete from COUNTERS;
OPEN c1;
getRows:
LOOP
FETCH c1 INTO vTableName;
IF SQLCODE = 0 THEN
SET stmt ='SELECT Count(*) FROM ' || vTableName;
PREPARE S2 FROM stmt;
OPEN C2;
SET vTableCount = 0;
FETCH C2 INTO vTableCount;
INSERT INTO COUNTERS (tableName, tableCount)
VALUES (vTableName, vTableCount);
CLOSE C2;
ELSE
LEAVE getRows;
END IF;
END LOOP getRows;
CLOSE c1;
END
Related
I try to produce a stored procedure that allows me to update many tables in my database but when I try to execute it with a columnstore engine, i get an error of procedure syntax not supported. I have looked for it in the web but do not manage to find where is the issue. If you have an idea, I let you the procedure here.
DELIMITER $$
CREATE PROCEDURE update_sp_aggregated()
BEGIN
DECLARE finished INTEGER DEFAULT 0;
DECLARE tableName varchar(255) DEFAULT "";
DECLARE cursor_update
CURSOR FOR
SELECT table_name FROM INFORMATION_SCHEMA.TABLES WHERE table_name like 'sp_aggr%' ;
OPEN cursor_update;
updateAggregated: LOOP
FETCH cursor_update into tableName;
IF finished = 1 THEN
LEAVE updateAggregated;
END IF;
SET #sql = CONCAT('ALTER TABLE ', tableName, ' ADD COLUMN col2 varchar(5)');
PREPARE stmt FROM #sql;
EXECUTE stmt;
DEALLOCATE PREPARE stmt;
SET #sql = CONCAT('UPDATE ', tableName, ' SET col2= LEFT(col1, 1)');
PREPARE stmt FROM #sql;
EXECUTE stmt;
DEALLOCATE PREPARE stmt;
END LOOP updateAggregated;
CLOSE cursor_update;
END $$
-- declare NOT FOUND handler
DECLARE CONTINUE HANDLER
FOR NOT FOUND SET finished = 1;
DELIMITER ;
CALL update_sp_aggregated();
Finally found, so as tell in my comment, the issue was the update command was not allowed.
to solve it, you need to change the variable infinidb_vtable_mode by setting it to 0.
All is described here
and here
CREATE OR REPLACE PROCEDURE USP_TEST_ROW_COUNT(
OUT vROW_COUNT BIGINT
)
RESULT SETS 1
MODIFIES SQL DATA
LANGUAGE SQL
P1: BEGIN ATOMIC
BEGIN
DECLARE C1 CURSOR WITH RETURN FOR
SELECT * FROM TABLE_NAME;
OPEN C1;
SET vROW_COUNT = CURSOR_ROWCOUNT(C1);
END;
END P1
Above is my code but it is showing Below Error
DB2 SQL error: SQLCODE: -206, SQLSTATE: 42703, SQLERRMC: C1
Message: "C1" is not valid in the context where it is used.
Line: 12
Please Help.
You may insert the results into some DGTT if you want to return all rows in the result set and return the number of output rows simultaneously:
CREATE OR REPLACE PROCEDURE USP_TEST_ROW_COUNT(OUT vROW_COUNT BIGINT)
RESULT SETS 1
MODIFIES SQL DATA
BEGIN ATOMIC
DECLARE v_stab VARCHAR(100) DEFAULT 'SESSION.USP_TEST_ROW_COUNT';
DECLARE v_stmt VARCHAR(100) DEFAULT 'SELECT * FROM TABLE_NAME';
DECLARE C1 CURSOR WITH RETURN FOR S1;
EXECUTE IMMEDIATE
'DECLARE GLOBAL TEMPORARY TABLE '||v_stab||' AS ('
||v_stmt
||' ) DEFINITION ONLY WITH REPLACE ON COMMIT PRESERVE ROWS NOT LOGGED';
EXECUTE IMMEDIATE 'INSERT INTO '||v_stab||' '||v_stmt;
GET DIAGNOSTICS vROW_COUNT=ROW_COUNT;
PREPARE S1 FROM 'SELECT * FROM '||v_stab;
OPEN C1;
END#
CURSOR_ROWCOUNT can only return the number of rows fetched (by the caller of the stored procedure). This is different from the number of rows in the result set. So if your syntax was accepted the value would be zero initially as nothing as yet been fetched.
You can see an example here, which shows the cursor variable, the cursor being opened and fetched, and the resulting value returned by CURSOR_ROWCOUNT.
To find the number of rows in the result-set either consume the cursor (fetch until no more rows), or do a second query that counts the rows in the same unit of work, or append the count to each row and fetch only 1 row.
I am trying to create a stored procedure, but I get this error
Expected tokens may include: "". LINE NUMBER=17. SQLSTATE=42601
My code:
CREATE OR REPLACE PROCEDURE FETCH_EMP_SP(IN V_EMP_NAME VARCHAR(100),IN V_EMP_DEPT VARCHAR(100))
DYNAMIC RESULT SETS 1
LANGUAGE SQL
BEGIN
DECLARE p_query_string VARCHAR(100);
IF ((V_EMP_NAME IS NOT NULL) AND (V_EMP_DEPT IS NOT NULL)) THEN
SET p_query_string = 'emp_name ='||V_EMP_NAME||' AND emp_dept='||V_EMP_DEPT||' WITH UR';
ELSEIF(V_EMP_DEPT IS NOT NULL) THEN
SET p_query_string = ' AND emp_dept='||V_EMP_DEPT||' WITH UR';
ELSE
SET p_query_string = ' WITH UR';
END IF;
DECLARE C1 CURSOR WITH RETURN TO CLIENT FOR SELECT emp_name,emp_no,emp_dept,emp_location from employee where status=1 p_query_string;
OPEN C1;
END#
should be executed successfully
Declarations and statements can’t follow in arbitrary order in a Compound SQL (compiled) statement
Cursor declarations must follow the variables declarations and must be followed by the SQL procedure statements.
So, place the cursor declaration after the variable declaration.
Moreover, there is a number of other errors in your code. Should be something like this:
CREATE OR REPLACE PROCEDURE FETCH_EMP_SP(IN V_EMP_NAME VARCHAR(100),IN V_EMP_DEPT VARCHAR(100))
DYNAMIC RESULT SETS 1
LANGUAGE SQL
BEGIN
DECLARE p_query_string VARCHAR(256);
DECLARE C1 CURSOR WITH RETURN TO CLIENT FOR S1;
IF ((V_EMP_NAME IS NOT NULL) AND (V_EMP_DEPT IS NOT NULL)) THEN
SET p_query_string = ' AND emp_name ='''||V_EMP_NAME||''' AND emp_dept='''||V_EMP_DEPT||''' WITH UR';
ELSEIF(V_EMP_DEPT IS NOT NULL) THEN
SET p_query_string = ' AND emp_dept='''||V_EMP_DEPT||''' WITH UR';
ELSE
SET p_query_string = ' WITH UR';
END IF;
SET p_query_string='SELECT emp_name,emp_no,emp_dept,emp_location from employee where status=1 '||p_query_string;
PREPARE S1 FROM p_query_string;
OPEN C1;
END#
String constants in the query text must be wrapped in single quotes. Don't do this if emp_dept is a numeric column.
I am building a procedure, where I`m first creating a select statement and store it in an VARCAHR variable.
I now want to execute that query and store the whole result set in an variable to loop through it or use directly in a for loop.
I only find examples where the Select is hard written in the for loop definition.
How do i exchange the Select statement with my variable that holds my select statement?
for r IN (SELECT ... FROM ...)
loop
--do sth;
end loop;
how i want to use it :
statement := 'SELECT .... FROM ...';
for r IN (statement) -- HOW TO DO THIS
loop
--do sth;
end loop;
For a dynamic ref cursor, you need to define everything explicitly:
declare
sqlstring long := 'select 123 as id, ''demo'' as somevalue from dual where dummy = :b1';
resultset sys_refcursor;
type demo_rectype is record
( id integer
, somevalue varchar2(30) );
demorec demo_rectype;
begin
open resultset for sqlstring using 'X';
loop
fetch resultset into demorec;
exit when resultset%notfound;
dbms_output.put_line('id=' || demorec.id || ' somevalue=' || demorec.somevalue);
end loop;
close resultset;
end;
You can parse the cursor and figure out the column names and datatypes with DBMS_SQL. Example here: www.williamrobertson.net/documents/refcursor-to-csv.shtml
I Have below cursor in the code.
CURSOR cur1
IS
SELECT a, b, c, d,
FROM EMP;
BEGIN
--Stored procedure logic
END
This curosr is getting information from EMP table.
But I need to change is as per below
There is a table (Table1) with Key Value pairs.
If the Table1 value is TRUE then the cursor should be created with STUDENT table
If the table1 value is FALSE then the cursor should be created with EMP table.
I can check the Value in the Table1 as below
select t.value into variable1 from Table1 t where s.key='xxxxx';
And I want write something like
IF variable1 := 'true'
curosr created with STUDENT
ELSE
curosr created with EMP
END IF
BEGIN
--Stored procedure logic
END
How to do it?
In another way you can just keep two CURSORS for those two scenarios and OPEN them on the condition. Declaring two CURSORS will not affect to the performance; you should be careful when OPEN a CURSOR and FETCHING from it.
PROCEDURE Get_Details_On_Condition ( name_ OUT VARCHAR2, isEmp IN BOOLEAN )
IS
CURSOR get_emp IS
SELECT name
FROM EMP;
CURSOR get_std IS
SELECT name
FROM STUDENT;
BEGIN
IF isEmp THEN
OPEN get_emp ;
FETCH get_emp INTO name_ ;
CLOSE get_emp ;
ELSE
OPEN get_std ;
FETCH get_std INTO name_ ;
CLOSE get_std ;
END IF;
RETURN name_;
END Get_Details_On_Condition;
Using if .. else construct is not proper (neither supported). You can use REF cursor to achieve the same like below.
DECLARE type cur1 REF CURSOR;
c1 cur1;
BEGIN
IF (variable1 := 'true') THEN
OPEN c1 FOR 'SELECT * FROM STUDENT';
ELSE
OPEN c1 FOR 'SELECT * FORM EMP';
END IF ;
END;
Idea taken from Oracle Community Forum Post
NOTE: I didn't included the entire code block (I mean cursor processing, closing etc) cause the main concern here is "How he will declare/define conditional cursor". So, pointed that particular in my code snippet. Since, rest of the part like processing the cursor and closing can be directly be found in Oracle specification.
For a complete code block, you can refer the answer given by Harsh
I would prefer to solve this without using dynamic SQL. If the code to process the results is the same for both tables, then it is reasonable to assume that the columns are the same (or equivalent) as well. My inclination would be to solve this using UNION and sub-queries:
DECLARE
CURSOR cur1 IS
SELECT a, b, c, d
FROM emp
WHERE NOT EXISTS
(SELECT *
FROM table1
WHERE s.key = 'xxxxx' AND t.VALUE = 'true')
UNION ALL
SELECT a, b, c, d
FROM student
WHERE EXISTS
(SELECT *
FROM table1
WHERE s.key = 'xxxxx' AND t.VALUE = 'true');
BEGIN
--Stored procedure logic
END;
The link provided by Rahul indicates the correct way to solve the problem. From the Oracle community forum post posted by Rahul, I have taken the code snippet through which the code could run successfully.
Rahul: Please do not take this as a redundant answer as I could not comment on your answer to help shyam to take the code snippet in the link posted by you.
Declare
TYPE cv_typ IS REF CURSOR;
cv cv_typ;
Begin
If(condition1 is TRUE) then
open cv FOR
'Select * from table_name1';
EXIT WHEN cv%NOTFOUND;
ELSE
open cv FOR
'Select * from table_name2';
EXIT WHEN cv%NOTFOUND;
End If;
CLOSE cv;
END;
Thanks & Regards,
Harsh
You can move the OPEN outside IF statement:
DECLARE type cur1 REF CURSOR;
c1 cur1;
vSQL VARCHAR2(1000);
BEGIN
IF (variable1 = 'true') THEN
vSQL := 'SELECT * FROM STUDENT';
ELSE
vSQL := 'SELECT * FORM EMP';
END IF;
OPEN c1 FOR vSQL;
--procedure logic
CLOSE c1;
END;