execute immediate with $$ - sql

I have a long query that returns 1 column and 1 row:
Query 1:
select test_query from (
SELECT
LISTAGG('...') ... AS xx,
LISTAGG('"' || c.COLUMN_NAME || '"', ', ') WITHIN GROUP(ORDER BY c.COLUMN_NAME) AS column_list
.....
FROM INFORMATION_SCHEMA.COLUMNS c
WHERE TABLE_NAME = xx);
The output looks like this. It has a query that I want to run. Note that it consists of quotation marks. The original query 1 also has quotation marks in the LISTAGG.
output:
test_query
select "col1", "col2" from stg.new
I wanted to extend the Query A such that I can select and run the test_query and return the final output of the test_query instead of queryA. I tried using execute immediate with $$:
execute immediate
$$select test_query from (
SELECT
LISTAGG('...') ... AS xx,
LISTAGG('"' || c.COLUMN_NAME || '"', ', ') WITHIN GROUP(ORDER BY c.COLUMN_NAME) AS column_list
.....
FROM INFORMATION_SCHEMA.COLUMNS c
WHERE TABLE_NAME = xx)$$;
but instead of running the test_query it outputs the same thing as Query1.
What else can I try?

Here's an attempted conversion of your SQL template. You can extend it to add the entire SQL statement:
execute immediate
$$
declare
rs resultset default (
select test_query from (
SELECT
LISTAGG('...') ... AS xx,
LISTAGG('"' || c.COLUMN_NAME || '"', ', ') WITHIN GROUP(ORDER BY c.COLUMN_NAME) AS column_list
.....
FROM INFORMATION_SCHEMA.COLUMNS c
WHERE TABLE_NAME = xx);
);
c cursor for rs;
sqlStatement string;
rsFinal resultset;
begin
for rowContents in c do
sqlStatement := rowContents.SQL_STATEMENT;
end for;
rsFinal := (execute immediate :sqlStatement);
return table(rsFinal);
end;
$$
;
In case that's not working because of context, etc., here is a completely self-contained sample that's working:
create or replace transient table T1 as
select 'Hello world' as s;
create or replace transient table SQL_TO_RUN as
select 'select S from T1 SQL_STATEMENT' SQL_STATEMENT
;
execute immediate
$$
declare
rs resultset default (select SQL_STATEMENT from SQL_TO_RUN);
c cursor for rs;
sqlStatement string;
rsFinal resultset;
begin
for rowContents in c do
sqlStatement := rowContents.SQL_STATEMENT;
end for;
rsFinal := (execute immediate :sqlStatement);
return table(rsFinal);
end;
$$
;
Output:
S
Hello world

Related

cursor for loop & dynamic SQL - Snowflake

I'm attempting to write a procedure that takes in a list of tables and date_column to create some row_counts by calendar dates for reconciliation purposes.
SELECT t.*
FROM (
VALUES ('tbl1', 'created_date')
, ('tbl2', 'modify_date')
, ('tbl3', 'last_seen_date')
) t(tbl, dt)
+----+--------------+
|TBL |DT |
+----+--------------+
|tbl1|created_date |
|tbl2|modify_date |
|tbl3|last_seen_date|
+----+--------------+
I'm connected to Snowflake via a JDBC connection using Datagrip - so I assume I need to follow the classic SnowSQL part of the documentation:
https://docs.snowflake.com/en/developer-guide/snowflake-scripting/loops.html#cursor-based-for-loops
EXECUTE IMMEDIATE $$
DECLARE
dt text
, tbl text;
c1 CURSOR FOR SELECT dt, tbl from t;
BEGIN
FOR record in c1 DO
dt := record.dt
tbl := record.tbl
stmt =: 'SELECT COUNT(*)' ||
CONCAT(', DAYOFMONTH(', $dt, ')') ||
CONCAT('\n FROM ', $tbl) ||
CONCAT('\n WHERE YEAR(', $dt, ')', ' = YEAR(CURRENT_DATE)') ||
CONCAT('\n AND MONTH(', $dt, ')', ' = MONTH(CURRENT_DATE)') ||
'\n GROUP BY' ||
CONCAT('\n DAYOFMONTH(', $dt, ')')
EXECUTE IMMEDIATE stmt -- will adapt this to be an update statement eventually.
END FOR
end;
$$
This returns a SQL Compilation error, I've tried a few different variations of this but I'm none the wiser on how to proceed.
Instead of concatenating the query string which makes it almost unreadable it could be rewritten using bind variables:
DECLARE
dt text;
tbl text;
stmt text;
c1 CURSOR FOR SELECT dt, tbl from t;
BEGIN
FOR record in c1 DO
dt := record.dt;
tbl := record.tbl;
stmt := 'INSERT INTO result(cnt, day_of_month)
SELECT COUNT(*), DAYOFMONTH(IDENTIFIER(?)) AS day_of_month
FROM TABLE(?)
WHERE YEAR(IDENTIFIER(?)) = YEAR(CURRENT_DATE)
AND MONTH(IDENTIFIER(?)) = MONTH(CURRENT_DATE)
GROUP BY day_of_month';
EXECUTE IMMEDIATE :stmt USING (dt, tbl, dt, dt);
RETURN stmt;
END FOR;
END;
If column or table is parameter it should be wrapped with IDENTIFIER/TABLE funtion.
For sample data:
CREATE OR REPLACE TABLE t AS
SELECT 'col1' AS dt, 'tab1' AS tbl UNION ALL
SELECT 'col2' AS dt, 'tab1' ;
CREATE TABLE tab1(col1 DATE, col2 DATE) AS
SELECT CURRENT_DATE(), CURRENT_DATE()-40;
CREATE TABLE result(cnt INT, day_of_month INT);
SELECT * FROM result;
There are lots of minor issues like missing semicolons etc. Here is the fixed script:
DECLARE
dt text;
tbl text;
stmt text;
c1 CURSOR FOR SELECT dt, tbl from t;
BEGIN
FOR record in c1 DO
dt := record.dt;
tbl := record.tbl;
stmt := 'SELECT COUNT(*)' ||
CONCAT(', DAYOFMONTH(', dt, ')') ||
CONCAT('\n FROM ', tbl) ||
CONCAT('\n WHERE YEAR(', dt, ')', ' = YEAR(CURRENT_DATE)') ||
CONCAT('\n AND MONTH(', dt, ')', ' = MONTH(CURRENT_DATE)') ||
'\n GROUP BY' ||
CONCAT('\n DAYOFMONTH(', dt, ')');
-- EXECUTE IMMEDIATE :stmt;
RETURN stmt;
END FOR;
END;

How to declare a number variable where I can save th count of table in my loop

I work wirh oracle Database. I have a plsql code where i run a query in a loop for multiple tables. so, table name is a variable in my code. I would like to have another variable (a single number) that I can call inside the loop and every time it counts the total rows of each table for me
declare
Cursor C_TABLE is
select trim(table_name) as table_name
from all_tables
where table_name in ('T1', 'T2', 'T3');
V_ROWNUM number;
begin
for m in C_TABLE
loop
for i in ( select column_name
from (
select c.column_name
from all_tab_columns c
where c.table_name = m.table_name
and c.owner = 'owner1'
)
)
loop
--I have this:
execute immediate ' insert into MY-table value (select ' || i.column_name || ' from ' || m.table_name || ')';
--I want this but it does not work of course:
V_ROWNUM := execute immediate 'select count(*) from ' || m.table_name;
execute immediate ' insert into MY-table value (select ' || i.column_name || ', ' || V_ROWNUM || ' from ' || m.table_name || ')';
end loop;
end loop;
end;
/
I count not use the "insert into" because I am not selecting from 1 table but the table I want to select from changes every round.
There are three things wrong with your dynamic SQL.
EXECUTE IMMEDIATE is not a function: the proper syntax is execute immediate '<<query>>' into <<variable>>.
An INSERT statement takes a VALUES clause or a SELECT but not both. SELECT would be very wrong in this case. Also note that it's VALUES not VALUE.
COLUMN_NAME is a string literal in the dynamic SQL so it needs to be in quotes. But because the SQL statement is itself a string, quotes in dynamic strings need to be escaped so it should be `'''||column_name||'''.
So the corrected version will look something like this
declare
Cursor C_TABLE is
select trim(table_name) as table_name
from all_tables
where table_name in ('T1', 'T2', 'T3');
V_ROWNUM number;
begin
for m in C_TABLE
loop
for i in ( select column_name
from (
select c.column_name
from all_tab_columns c
where c.table_name = m.table_name
and c.owner = 'owner1'
)
)
loop
execute immediate 'select count(*) from ' || m.table_name into V_ROWNUM;
execute immediate 'insert into MY_table values ( ''' || i.column_name || ''', ' || V_ROWNUM || ')';
end loop;
end loop;
end;
/
Dynamic SQL is hard because it turns compilation errors into runtime errors. It is good practice to write the statements first as static SQL. Once you have got the basic syntax right you can convert it into dynamic SQL.
you can't assign the result of execute immediate to a variable. it is not a function.
but you can do it by using the into_clause e.g.
execute immediate 'select count(*) from ' || m.table_name into V_ROWNUM ;

Select from all tables inside the schema containing column with name

How can I get select (table_name, table_name.age)?
I need to get values from column 'age' from all tables having this column/
I have this function
CREATE OR REPLACE FUNCTION union_all_tables()
RETURNS TABLE
(
age bigint
) AS
$$
DECLARE
dynamic_query text = '';
r_row record;
BEGIN
FOR r_row IN SELECT table_schema || '.' || table_name qualified_table_name
FROM information_schema.COLUMNS
WHERE column_name = 'age'
LOOP
dynamic_query := dynamic_query || format('UNION SELECT ' ||
'age ' ||
'FROM %s ',r_row.qualified_table_name) || E'\n'; -- adding new line for pretty print, it is not necessary
END LOOP;
dynamic_query := SUBSTRING(dynamic_query, 7) || ';';
RAISE NOTICE 'Union all tables in staging, executing statement: %', dynamic_query;
RETURN QUERY EXECUTE dynamic_query;
END;
$$
LANGUAGE plpgsql;
You don't need to generate a single huge UNION statement. If you use RETURN QUERY the result of that query is appended to the overall result of the function every time you use it.
When dealing with dynamic SQL you should also use format() to properly deal with identifiers.
Your function can be simplified to:
CREATE OR REPLACE FUNCTION union_all_tables()
RETURNS TABLE (table_schema text, table_name text, age bigint)
AS
$$
DECLARE
dynamic_query text = '';
r_row record;
BEGIN
FOR r_row IN SELECT c.table_schema, c.table_name
FROM information_schema.columns c
WHERE c.column_name = 'age'
LOOP
dynamic_query := format(
'select %L as table_schema, %L as table_name, age from %I.%I',
r_row.table_schema, r_row.table_name,
r_row.table_schema, r_row.table_name);
RETURN QUERY EXECUTE dynamic_query;
END LOOP;
END;
$$
LANGUAGE plpgsql;
Note that the whole function will fail if there is (at least) one table where the age column is not a bigint.

Count the number of rows in a schema based on a where condition

I have engine_id column in various tables in the schema. So I want to count the number of rows based on engine_id column in the whole schema where this column exists.
Select count(*)
from table_name
where table_name.engine_id = 8;
Without user-defined PL/SQL, using only built-in Oracle functionality...
Oracle 11g (and maybe even lower) query:
select TC.table_name, X.*
from user_tab_columns TC
cross join xmltable(
'/ROWSET/ROW/CNT'
passing
dbms_xmlgen.getXMLType('
select count(1) as cnt
from '||TC.table_name||'
where &columnName = &columnValueAsLiteral
')
columns
cnt integer
) X
where TC.column_name = '&columnName'
;
Oracle 12c+ query:
select TC.table_name, X.*
from user_tab_columns TC
cross apply xmltable(
'/ROWSET/ROW/CNT'
passing
dbms_xmlgen.getXMLType('
select count(1) as cnt
from '||TC.table_name||'
where &columnName = &columnValueAsLiteral
')
columns
cnt integer
) X
where TC.column_name = '&columnName'
;
In your case supply
&columnName as ENGINE_ID,
&columnValueAsLiteral as 8.
Note: It could also be possible with the with-PLSQL clause of 12c's but I somehow can't make it work, hence I'm not posting that solution here.
These are the steps which you should be following
Get all the tables which have the column.
select table_name from all_tab_columns
where column_name = 'ENGINE_ID';
Create the above as a cursor and run a loop on it
for records in above_cursor
loop
execute immediate 'select count(*) from ' || records.table_name || 'where engine_id = 8' into some_temp_number_var;
some_total_number_var := some_temp_number_var + some_total_number_var;
end loop;
You will need PL code that would query the data dictionary view USER_TABLES to find out which tables have the column you seek and then build a dynamic SQL query as per your filter requirements.
Your function would be something like this:
create or replace function find_count (p_column_name varchar2,
p_column_value number)
return number is
v_sql clob; HERE
v_count number;
begin
for i in (select table_name
from user_tab_cols
where column_name = upper(v_column_name)) loop
v_sql :=
v_sql
|| 'select count(*) as cnt from '
|| i.table_name
|| ' where '
|| p_column_name
|| ' = '
|| p_column_value;
v_sql := v_sql || chr (10) || 'union all ';
end loop;
v_sql := substr (v_sql, 1, length (v_sql) - 11);
v_sql := 'select sum(cnt) from (' || v_sql || ')';
execute immediate v_sql into v_count;
return v_count;
end find_count;
/
You can trigger this function using a query like this:
select find_count('ENTITY_ID', 1012) as engine_count from dual;

postgresql change all sequences with for loop

I have sequences like table_name_sq in postgresql for all tables.
For example;
seqtest-> seqtest_sq
seqtest2-> seqtest2_sq
I need to change all sequences in database.
(I cannot run query for every tables manually)
I can get tables and make sequence string
select table_name || '_sq' as sequence_name from information_schema.tables where table_catalog='test' and table_schema='public'
I can change sequence value for specified table
select setval('seqtest_sq',(select max(id) from seqtest)+1)
But I cannot merge these two.
I think should use for loop but that I could not make that work.
CODE IS:
DO $$
DECLARE
i RECORD;
BEGIN
FOR i IN (select table_name from information_schema.tables) LOOP
EXECUTE 'SELECT count(*) FROM ' || i;
END LOOP;
END$$;
OUTPUT is:
ERROR: syntax error at or near ")"
LINE 1: SELECT count(*) FROM (seqtest)
^
QUERY: SELECT count(*) FROM (seqtest)
CONTEXT: PL/pgSQL function inline_code_block line 6 at EXECUTE
********** Error **********
ERROR: syntax error at or near ")"
SQL state: 42601
Context: PL/pgSQL function inline_code_block line 6 at EXECUTE
Also I printed table names with for loop but table names come with parentheses.
HERE is CODE
DO $$
DECLARE
i RECORD;
BEGIN
FOR i IN (select table_name from information_schema.tables where table_catalog='test' and table_schema='public') LOOP
raise notice 'Value: %',i;
END LOOP;
END$$;
HERE is OUTPUT:
NOTICE: Value: (seqtest)
NOTICE: Value: (seqtest2)
I think it is enough for me to get rid of this parentheses.
Would you help me to build a proper loop or find a easy way to achive this?
Here is the script I use.
DO $$
DECLARE
i TEXT;
BEGIN
FOR i IN (
SELECT 'SELECT SETVAL('
|| quote_literal(quote_ident(PGT.schemaname) || '.' || quote_ident(S.relname))
|| ', COALESCE(MAX(' ||quote_ident(C.attname)|| '), 1) ) FROM '
|| quote_ident(PGT.schemaname)|| '.'||quote_ident(T.relname)|| ';'
FROM pg_class AS S,
pg_depend AS D,
pg_class AS T,
pg_attribute AS C,
pg_tables AS PGT
WHERE S.relkind = 'S'
AND S.oid = D.objid
AND D.refobjid = T.oid
AND D.refobjid = C.attrelid
AND D.refobjsubid = C.attnum
AND T.relname = PGT.tablename
) LOOP
EXECUTE i;
END LOOP;
END $$;
Here is the solution with help of #Nick Barnes and #a_horse_with_no_name
If someone needs a idea of how to fix sequences can use this script.
DO $$
DECLARE
i TEXT;
BEGIN
FOR i IN (select table_name from information_schema.tables where table_catalog='YOUR_DATABASE_NAME' and table_schema='public') LOOP
EXECUTE 'Select setval('''||i||'_sq'', (SELECT max(id) as a FROM ' || i ||')+1);';
END LOOP;
END$$;
I've got some tables that have not got 'id' column and some tables use special names in camelCase, so they are need to be quoted. Hope this javuzs's solutions upgrade will be heplfull for someone.
DO $$
DECLARE
i TEXT;
BEGIN
FOR i IN (SELECT tbls.table_name FROM information_schema.tables AS tbls INNER JOIN information_schema.columns AS cols ON tbls.table_name = cols.table_name WHERE tbls.table_catalog='YOUR_DATABASE_NAME' AND tbls.table_schema='public' AND cols.column_name='id') LOOP
EXECUTE 'SELECT setval(''"' || i || '_id_seq"'', (SELECT MAX(id) FROM ' || quote_ident(i) || '));';
END LOOP;
END $$;