PLSQL Substitue argument of a function with SELECT statement - plsql

I would like to display the contents of a BLOB using the functions provided by Anton Scheffer using this query in sql developer: [1.] select * from table( as_read_xlsx.read( v_blob ) ); --"v_blob" is a variable containing blob with xlsx but I have BLOB in some other table. To get BLOB i have to use this: [2.]select bb from at_table_temp where id_c=4
So, the question is how to put [2.] as an argument to [1.]:
select * from table( as_read_xlsx.read([2.]) );
I already tried the following but it didn't work:
select * from table( as_read_xlsx.read( $"[2.]" ) );
select * from table( as_read_xlsx.read( #"[2.]" ) );
select * from table( as_read_xlsx.read( $([2.]) ) );
Looking forward to your answers!

Try this:
select * from at_table_temp t cross join table(as_read_xlsx.read(t.bb));

Related

DELETE from same table used in the WHERE

I have the following query that is supposed to delete from a table with a circular FK requirement;
DELETE FROM question
WHERE defaultGotoQuestionId IN (
SELECT id from question WHERE facilityId IN (
SELECT id FROM facility WHERE customerId NOT IN (1,76)
)
);
But I get the following error;
Table is specified twice, both as a target for 'DELETE' and as a separate source for data
I understand the error message - that I can't DELETE from a TABLE that I am specifying in the WHERE - I just can't seem to work out how to solve it, for myself.
I saw a few examples of using a join - but perhaps I am having an off day - because I have been iterating through copy/paste examples - but still can't manage it.
It works as a SELECT : In that the result gives me the records I want to delete;
SELECT id FROM question
WHERE defaultGotoQuestionId IN (
SELECT id from question WHERE facilityId IN (
SELECT id FROM facility WHERE customerId NOT IN (1,76)
)
);
Thanks for any help!
What version of MariaDB are you using?
DELETE :: Same Source and Target Table
Until MariaDB 10.3.1, deleting from a table with the same source and target was not possible. From MariaDB 10.3.1, this is now possible.
See dbfiddle.
In older versions of MariaDB (or in MySQL, for example), one option you can use is:
DELETE
`question`
FROM
`question`
INNER JOIN (
SELECT
`id`
FROM
`question`
WHERE
`defaultGotoQuestionId` IN (
SELECT
`id`
FROM
`question`
WHERE
`facilityId` IN (
SELECT
`id`
FROM
`facility`
WHERE
`customerId` NOT IN (1, 5)
)
)
) `der` ON
`question`.`id` = `der`.`id`;
See dbfiddle.

CREATE TABLE with CTE statement

Is it possible to create a table from a query with a CTE statement?
Something like:
CREATE TABLE db1.test1 AS
(
WITH cte1(v1) as
( SEL v1 FROM db1.table1 )
SEL * FROM cte1
)
This is how the CTE's look like:
WITH employees(id, name, boss, senior_boss) AS
(
SEL
empls.id,
empls.name,
supervisors.name as boss,
senior_bosses.name as senior_boss
FROM empl_cte AS empls
LEFT JOIN empl_cte AS supervisors ON empls.boss_id = supervisors.id
LEFT JOIN empl_cte AS senior_bosses ON supervisors.boss_id = senior_bosses.id
),
WITH empl_cte(....) AS
(
SEL
id,
name
boss_id
FROM all_employees
WHERE <some_filters>
)
SEL
*
FROM products
LEFT JOIN employees ON products.sales_rep_id = employees.id
Both
converting the CTEs into views
and
converting employees as a sub-query (empl_cte as a VIEW) in the left join
leads to a massive loss of performance (run time blowing up from a couple of minutes to days of work). I can't figure out how Teradata optimizer works.
EXPLAIN on the new refactored queries seem indicate that the LEFT JOIN becomes a product join draining countless of time.
This will work in V16 (and possibly earlier versions).
CREATE TABLE myTable AS (
SELECT * FROM (
WITH x AS (
SELECT ...
FROM ...
WHERE ...
)
SELECT ...
FROM x ...
WHERE ...
) D
) WITH DATA PRIMARY INDEX (PK)
;
Basically you need to wrap the whole query, including the CTE, in a SELECT with an alias.

subquery that starts with "with" in teradata is not working

I have a query (that I cannot modify) that starts like this
with CodeSet (
code_context_c
, bom_index_c
, src_qs_c
, src_code_set_c
, src_code_set_x
, src_code_value_c
, src_code_value_x
, tgt_code_set_c
, tgt_code_value_c
) as (
SELECT ...
and then goes on. Now I need to use it as a subquery and do something like
select * from (with CodeSet (
code_context_c
, bom_index_c
, src_qs_c
, src_code_set_c
, src_code_set_x
, src_code_value_c
, src_code_value_x
, tgt_code_set_c
, tgt_code_value_c
) as (
SELECT ...
but Teradata does not like it... Anyone has seen this before? Changing the query would require some time and I would prefer not to. Anyone can help me out here?
Error message is:
SELECT Failed. [3707] Syntax error, expected something like a name or a Unicode delimited identifier or '(' between the 'from' keyword and the 'as' keyword.
Thanks in advance,
Umberto
Not sure if you are still looking for the answer, but you need to run the SELECT after your WITH statement. WITH doesn't actually materialize a table until you SELECT from it. So something like this:
;WITH CodeSet (
blah
, blah
, ...
) AS (
SELECT blah
, blah
, ...
)
;
SELECT * FROM CodeSet;

Oracle sql : expression is of wrong type

I'm trying to write a PL/SQL to convert comma separated string into an array and iterate through that.
for that I created a datatype as follows:
"CODE_TABLE_TYPE" AS TABLE OF VARCHAR2(500)
crated a function - STR_TO_CODE_TABLE to convert the comma separated string in to the table of CODE_TABLE_TYPE.
and the PL/SQL looks like this:
FOR DEP IN ( SELECT * FROM TABLE ( CAST( STR_TO_CODE_TABLE( IN_DES_AIRPORTS ) AS CODE_TABLE_TYPE ) ) ) LOOP
SELECT * INTO RESULTS FROM MY_TABLE
WHERE IN_ID = MY_TABLE.ID
AND ( SELECT 1 FROM TEMP_TABLE WHERE DEPARTURE LIKE '%' || DEP || '%' )= 1;
END LOOP;
But it gives an error saying "expression is of wrong type". But the datatype is varchar2.
Can anyone please suggest what's the possible cause for this. What should I do to avoid this issue?

Where-Condition as IN(Subquery) with Doctrine2 in Symfony2.3.1 doesnt work

---- Done with Symfony2.3.1 and Doctrine2 ----
Sorry, i hope i was not too stupid to find a suitable solution for my problem. I try to build a Query for hours.
SELECT * FROM product
WHERE product_id in
(
SELECT product_id from (
SELECT count(*) as result_amount, product_id FROM product_x_attribut
JOIN productattribut on productattribut_id = productattribut.id
WHERE (
productkey = "price" and
stringType = "premium"
) or (
productkey = "size" and
stringType = "S"
)
GROUP BY product_id
HAVING result_amount = 2
) as temp
)
GROUP BY product_id
ORDER BY p0_.name ASC
This is the SQL which works fine in phpmyAdmin.
This can be seen like
Select * from abc where abc.x in ( Select * from ( select * from ) as abcd )
So there is one core query, i call it subSubQuery, the second query around the core will be called subQuery and the outer Query is just the outer Query, no a Subquery.
I could build the subSubQuery with Doctrine2.
But i cannot built the subQuery like this
Select product_id from ( $subSubQuery->getQuery()->getDQL() )
I want to do the subQuery like this
$subQuery = $repositoryProduct->createQueryBuilder('product');
$subQuery->add('select', 'product_id');
$subQuery->add('from',$subSubQuery->getDQL() );
// However to set an alias is a miracle for me, this didnt work off course
$subQuery->add('as','tmp' );
This is the subQuery.
I also cannot build the outer Query
Select * from abc where abc.x in ( $subQuery->getQuery()->getDQL() )
I want to do this like this
$query->where(
$query->expr()->in('product.id', $subQuery->getDQL() )
);
But i try to build this with Doctrine2 like this:
I am so down, i tried ->getSQL(), ->getDQL(), i tried as much as i was able to detect as a suitable tiny step to a solution for this problem and i has tried as much keyword in google as my finger were able to write... I hope someone could help me to find a solution...
Thanks a lot to each helpful advise.
I know that statements like this work:
$qbGames->andWhere($qbGames->expr()->in('game.id',$qbGameId->getDQL()));
Your question is kind of hard to follow. Consider using pastebin to show all your mappings as they currently exist. And then maybe presenting a simplieid query?
My $qbGameId query was built with:
$qbGameId = $em->createQueryBuilder();
$qbGameId->addSelect('distinct gameGameId.id');
$qbGameId->from('ZaysoCoreBundle:Event','gameGameId');
// Assorted joins and where conditions

Resources