How to extract part of SQL query from a table - sql

Is it possible to to extract SQL queries that are saved in a table?
For example
select * from saved_queries
name | statement
queryname | 'select * from mytable where myfield = 'somevalue'
I would like to be able to do something like
select * from ( extractsomehow( 'select Statement from saved_queries where name = 'queryname') ).
Unfortunately I cannot use Java so I am restricted to SQL and XML there.
I am using Oracle 11g

If you can write a stored procedure, you could use execute immediate, something like this:
select statement into v_statement from saved_queries where ... ;
execute immediate v_statement;
Before you use dynamic SQL, think carefully about whether or not you actually need it.

Related

postgres sql / dbweaver - Using a variable for tablename?

One of the SQL Queries use the same table name again and again -
Example -
select
rr.jdoc as child_node, json_agg(parent_rr.jdoc)::jsonb as parent_node , array_length(array_agg(parent_rr.jdoc)::jsonb[], 1) as count
from MYTABLE rr, MYTABLE parent_rr
where
parent_rr.jdoc #> (rr.jdoc->'somefield')::jsonb
group by rr.jdoc
UNION
select rr.jdoc, NULL as parent_id, null as pcount
from MYTABLE rr where
not (rr.jdoc ?? 'somefiled')
and ((rr.jdoc->'crazyfiled'->>'doublecrazyfiled')<>'gotyou')
You can see the same MYTABLE is used 3 times. The thing is, I have to run the same query for different tables i.e the MYTABLE substituted.
So I am trying to find - if anything like below is possible -
SET TABLENAME=CUS_DELTA --//then use the above like -
select * from $TABLENAME;
I am using postgres 13.5 with DBeaver for running the queries.
If not SQL, does DBeaver provide any such means.
If SQL can not support it, I am ok to use Dynamic SQL / PL SQL (though i have not idea about these) - if its not going to be too complicate for the above case. But I am not aware about the code, so request to share the same through an answer.
Read this article about Dynamic SQL carefully. Example from the docs:
EXEC SQL BEGIN DECLARE SECTION;
const char *stmt = "INSERT INTO test1 VALUES(?, ?);";
EXEC SQL END DECLARE SECTION;
EXEC SQL PREPARE mystmt FROM :stmt;
...
EXEC SQL EXECUTE mystmt USING 42, 'foobar';
Basically this way you can simply pass values. Now, your values are dynamic as well, but no worries. Declare your tablename as varchar and SELECT tbname INTO yourvariable FROM sometable. Then you can pass this variable to your dynamic query.
EDIT:
Minimalistic example:
Setting a variable to hard-coded value
SET TABLENAME = 'CUS_DELTA';
Setting a variable to a dynamic table name
SELECT tbname
INTO TABLENAME
FROM yourtable
And then let's use our TABLENAME variable like this:
EXEC SQL BEGIN DECLARE SECTION;
const char *stmt = "SELECT * FROM ?";
EXEC SQL END DECLARE SECTION;
EXEC SQL PREPARE mystmt FROM :stmt;
EXEC SQL EXECUTE mystmt USING TABLENAME;
The above is untested.

How to create a reference value in Oracle SQl Developer

I use basic sql querying for my day to day work, but I regularly find myself needing to run queries in different tables using the same where clauses.
What I would ideally like to do is locally set a value to a name, for example:
traderef = ABCD1234. It's kind of like Defining a name in excel.
and then use 'traderef' in my queries,
select * from table1 where tranid = traderef
select * from table2 where tranid = traderef and otherattr = 'xyz1'
I only have query access to the dbs that i use, i have tried to google results, and found some info re SET
TIA
Declare a bind variable using the SQL/Plus and SQL Developer command VARIABLE name data_type and assign it a value using EXECUTE (or a PL/SQL anonymous block) and then use it in your queries:
VARIABLE traderef VARCHAR2(20)
EXEC :traderef := 'ABCD1234';
SELECT * FROM table1 WHERE tranid = :traderef;
SELECT * FROM table2 HWERE tranid = :traderef AND otherattr = 'xyz1';

SQL DB2 - Execute a Query using a results from another table or query

I am running the following query:
select TABLE_SCHEMA ||'.'||TABLE_NAME AS Schema_Table
from qsys2.systables
where table_schema = 'PTLDBA'
AND TABLE_NAME LIKE'DAILY%'
AND DATE (LAST_ALTERED_TIMESTAMP) = CURRENT DATE;
The result set is schema.table name. I need to use the result set to query the actual table
The final query would look like this:
Select * from Schema_Table limit 10;
The table name changes depending on the day. I believe a SQL Procedure would work best but I am not sure how to write it.

db2 use query result in variable

i want to use the result of a query as an input in another query.
What might make it difficult: The variable is the schema in the database.
CREATE or replace VARIABLE myschema varchar(15) ;
set myschema = (select owner from syscat.tables where tabname = 'xyz');
select count(name) as result from myschema.USR02 where USTYP = 'A';
DROP VARIABLE myschema;
This is my last try, after i failed using declare.
But i get an error, because "myschema" is used as a string, and of course there is no schema with name "myschema". The result of the first query is not used.
If I just run the first two lines, i get the schemaname as result. Do i have to mark the variable in a special way? The goal is just the result of the query in line 3 by using the dynamic value of "myschema".
Unfortunately, you have to use dynamic SQL (forming a custom SQL query through string manipulation) if you want to deal with table, schema, or column names dynamically:
This is the basic idea:
execute immediate 'select * from ' || myschema || '.USR02';
However, you can't just run a bare select in dynamic SQL; you have to put the result in something. And the whole thing must be in a compound SQL block. So the full example would look something like this (simplified query for space).
This query assumes that a table called "result" exists to store the result you are returning.
begin
declare myschema varchar(100) default '';
set myschema = (select owner from syscat.tables where tabname = 'xyz');
execute immediate 'insert into result select count(*) from ' || myschema || '.USR02';
end
select * from result;
Note that within the block, you can simply declare a variable (as shown in my example). So you don't have to declare a global variable for this purpose, unless you want it to persist beyond this one statement.

PLSQL Execute Immediate with Dynamic Using

I am dynamically building a search query with bind variables with at least 1 and at most 7 different potential criteria. I know I can do this -
EXECUTE IMMEDIATE sql USING bind_var1, bind_var2 or
EXECUTE IMMEDIATE sql USING bind_var3, bind_var5, bind_var7.
Is it possible to include the bind variables within the sql?
sql = 'SELECT * FROM table WHERE id = :bind_var1 AND name = :bind_var2 USING bind_var1, bind_var2'
and do
EXECUTE IMMEDIATE sql?
I want and need to dynamically build the USING piece instead of writing a lot of IF THEN statements.
According to your tags, I assume this will be used inside some kind of PL/SQL block. So, maybe are you looking for the open for statement.
This allows you to get a cursor on an dynamic query:
sql := 'SELECT * FROM table WHERE id = :bind_var1 AND name = :bind_var2';
open my_cursor for sql using bind_var1, bind_var2';
-- do whatever you need with your cursor
your USING bind_var1, bind_var2 pice of code should be out side os your sql string and come at the end of execute immediate statement and also for select senarios try to use dynamic sql for select with a cursor unless you want to select into a variable