How to add table name into dynamic query in execute immediate? - sql

I'm confused, because I don't know how to use variable as string in execute immediate clause.
declare
variable1 varchar2(30):
cur sys_refcursor;
begin
open cur for
select tablename from table1;
loop
fetch cur into variable1;
exit when cur %notfound;
execute immediate 'select count(*)
into variable1
from user_tables
where table_name =''' || variable1 || '''';
end loop;
close cur;
end;
Here variable1 is a table name. There should be a string value. How to do that? Still got an error.

The execute immediate statement should look like this:
execute immediate 'select count(*) from user_tables where table_name ='''||variable1||'''' into variable1;
with the into xxx after the query.

Related

Iterate through schemas

I have several schemas in my database. Most of the schemas have a table called orders. How do I iterate through the schemas that have that table and get a count of the number of records in each of those schemas for that table?
select * from information_schema.schemata;
see https://www.postgresql.org/docs/11/infoschema-schemata.html
do
$$
declare
tableName varchar := 'xyz';
schemaName varchar;
query text;
counter integer;
begin
for schemaName in select table_schema from information_schema.tables where table_name = tableName
loop
query := 'select count(*) from ' || schemaName || '.' || tableName;
execute query into counter;
raise notice E'% %', schemaName, counter;
end loop ;
end $$

Oracle databases loop tables

I have an oracle database and I am running the below query
select table_name
from db_test.test
where table_name like '%20170128'
This returns me a column with all the tables with the specific date at the end.
How can I take this list and query them?
You'd need dynamic SQL. If you're running a simple query against each table (I'm just doing a count(*) in this example), something like this would work
declare
l_cnt integer;
l_sql varchar2(1000);
begin
for t in (select table_name
from db_test.test
where table_name like '%20170128')
loop
l_sql := 'select count(*) from ' || t.table_name;
execute immediate l_sql
into l_cnt;
dbms_output.put_line( t.table_name || ' has ' || l_cnt || ' rows.' );
end loop;
end;

Create Master SP in Teradata to call other SP dynamically

REPLACE PROCEDURE PD_CCP_FN_DB.master_proc_TEST()
SQL SECURITY INVOKER
BEGIN
DECLARE STMT VARCHAR(4000);
FOR rec AS Cur CURSOR FOR
select SP_NAME from WRK_QA_CCP_ATOMIC_DB.SP_CALL
DO
SET STMT= ' CALL ' || rec.SP_NAME || '(''PII_SUPP_SMRY_TXN'') ';
EXECUTE IMMEDIATE STMT;
END FOR;
END;

Dynamic loop in PL/SQL

I am currently looping through values in PL/SQL with the following:
for c in (select * from example_table where name is not null) loop
-- logic
end loop;
I would like to replace the SQL statement with a dynamic one, for example:
l_sql := 'select * from example_table where || l_col || is not null';
for c in (l_sql) loop
-- logic
end loop;
Is this possible?
Best regards
That's not possible with an implicit cursor loop ( select inside for loop ). You may use the conventional OPEN .. FETCH .. LOOP through a REFCURSOR with a record variable of tablename%ROWTYPE
DECLARE
t_rec example_table%ROWTYPE;
l_sql VARCHAR2(1000);
v_cur SYS_REFCURSOR;
l_col varchar2(32) := 'MY_COLUMN';
BEGIN
l_sql := 'select * from example_table where '|| l_col || ' is not null';
OPEN v_cur FOR l_sql;
LOOP
FETCH v_cur INTO t_rec; --fetch a row
EXIT WHEN v_cur%NOTFOUND;
-- your logic using t_rec columns.
END LOOP;
CLOSE v_cur;
END;
/

In SQL Developer, how to find distinct values of all columns of all tables in a database?

This was what i was trying
SET SERVEROUTPUT ON;
DECLARE
sql_query VARCHAR2(32767);
BEGIN
FOR t IN (SELECT table_name, column_name FROM user_tab_columns)
LOOP
EXECUTE IMMEDIATE sql_query := 'SELECT * FROM ' || t.table_name ;
END LOOP;
END;
This query gives you the number of distinct values per column (assuming that statistics are up to date).
select owner, table_name, column_name, num_distinct
from all_tab_col_statistics
Maybe this would be enough.
If you need to have the distinct values, you have to modify the sql_query param in your script as follows:
EXECUTE IMMEDIATE sql_query := 'SELECT distinct '|| t.column_name ||
' FROM ' || t.table_name ;