Select from dual as one row? - plsql

To select many strings from dual as one column. I have tried the solution below and i can live with it, its relatively easy with PL/SQL macros to add "select from dual". I'm wondering is there any other way to achive this?
select 'AAA' as code
from dual
union all
select 'ABQ'
from dual
union all
select 'ACA'
from dual
union all
from dual

For a single column query there is. You need a database type that is a table of VARCHAR2, and some always exist in oracle including:
SYS.KU$_VCNT
SYS.DBMS_DEBUG_VC2COLL
(and of course you could create your own if you prefer).
Then you can query like this:
select * from table (SYS.KU$_VCNT ('AAA','ABQ','ACA'));
For a query with more than one column, you would need to create a type specifically for that query, which isn't such a useful option. But just for completeness this is how you could do it:
create type my_obj_t as object(n number, d date, c varchar2(100));
create type my_tab_t as table of my_obj_t;
select * from table (my_tab_t(my_obj_t(1,sysdate,'aaa'),
my_obj_t(2,date '2014-12-31','bbb'),
my_obj_t(3,sysdate+2,'bbb')));

Related

PL/SQL. Is it possible to use SELECT INTO from table dual?

Is it possible to use SELECT INTO from the dual table like this?
SELECT * INTO myTable
FROM ('val1' COLUMN1, 'val2' COLUMN2 FROM dual) dualTable
I would use this, because I need to insert values and get them back in a same statement.
There's really not much to it other than just making two variables.
Below is the code
SELECT
'val1' COLUMN1, 'val2' COLUMN2
INTO
variable1, variable2
FROM
DUAL
Regarding the insertion part, I don't think it's possible to be done in the same statement. You can always write a procedure in the back to handle this.

Peoplesoft pass in array of strings as parameter

I'd like to ask how to pass an array of strings as parameter in Peoplesoft.
In Oracle query, I can do that like the following
SELECT EMP_ID, EMP_NAME
from EMPLOYEE
where EMP_ID in (select regexp_substr(:empid,'[^,]+', 1, level) from dual connect by regexp_substr(:empid, '[^,]+', 1, level) is not null)
And pass in parameter value as
E001,E002,E003,...
But in Peoplesoft Query Manager, when I create the in criteria to a subquery, it does not allow from clause in it.
Another alternative that I try is using Prompt and then pass the value to it, so the query becomes like:
SELECT EMP_ID, EMP_NAME
from EMPLOYEE
where EMP_ID in (:1)
But this also does not work.
So how do I do this?
I can't tell if you are asking about doing this in ps query manager or in peoplecode.
Maybe use older methods, like populating a table first, and joining to that table. Maybe use a "With" clause.
Please share what you ended up using, to accomplish your goal.

Does SQLScript for SAP HANA support the use of INSERT with CTEs (Common Table Expressions)?

I know this isn't a specific bit of code or problem, but I am having trouble with a very similar issue to the person asking this (except theirs is for SQL Server): Combining INSERT INTO and WITH/CTE ...and I can't seem to find it out there on any SAP HANA help forums etc. so thought there may be an expert on here who can just give me a simple yes or no answer.
The SQL statement I am using contains multiple CTEs, but when I try to insert it tells me there is a Syntax error around the word INSERT. It is definitely laid out exactly the same as in the question I've linked above (spent hours checking), and I can post code samples if necessary but I simply want to know whether it is supported first! Thanks
Short answer:
No, CTEs are not supported for INSERT/UPDATE statements.
Longer answer:
SQLScript's INSERT/UPDATE commands are actually "borrowed" SQL commands as the documentation explains.
Checking the documentation for SQL INSERT we find that it supports a subquery as a source of values.
The subquery term is defined as part of the SQL SELECT statement. Checking the documentation for SELECT shows that <subquery> and <with_clause> are different, non-overlapping terms.
This means, that CTEs cannot be used in subqueries and therefore not be part of the subqueries used in INSERT/UPDATE commands.
You can, however, use SQLScript table variables in INSERT statements in your SQLScript blocks, which is very similar to CTEs:
DO BEGIN
te_a := SELECT 10, 'xyz' as VAL from dummy;
te_b := SELECT 20, 'abc' as VAL from dummy;
te_all := SELECT * from :te_a
UNION ALL SELECT * from :te_b;
INSERT INTO VALS
(SELECT * from :te_all);
END;
You can convert the CTE into a Sub-Select statement in many cases
You can use following
insert into city (city, countryid, citycode)
select
city, countryid, citycode
from (
-- CTE Expression as subselect
select * from city
-- end (CTE)
) cte
Instead of using following valid CTE command combined with INSERT (on SQL Server)
with cte as (
select * from city
)
insert into city (city, countryid, citycode)
select
city, countryid, citycode
from cte
SAP HANA includes this posibility, the order of the code is different than SQL Server:
INSERT INTO EXAMPLE (ID)
WITH cte1 AS (SELECT 1 AS ID FROM DUMMY)
SELECT ID FROM cte1;

How to select distinct records from two tables, combine into one column, and eliminate all records that exist in another table using SQLite?

Here is my existing query which successfully selects distinct records from two tables and combines them into one column:
SELECT index_text AS unique_text FROM words
UNION
SELECT c1index_text FROM words_content
ORDER BY unique_text
Now I want to eliminate all records WHERE body NOT IN (SELECT body FROM sms) (or NOT EXISTS, whatever works is fine). The problem is that no matter what I try, either I get a syntax error whenever I attempt to use parentheses, or it will not recognize sms.body (even if I precede every column by its parent table). I'm thinking some SQLite limitations may be making this harder than it needs to be, but there has to be a workaround. Below are queries I have tried unsuccessfully (I have also tried numerous variations of these queries to no avail):
SELECT index_text AS unique_text FROM words
UNION
SELECT c1index_text FROM words_content
WHERE body NOT IN (SELECT body FROM sms)
ORDER BY unique_text
Results in error: No such column: body
SELECT words.index_text AS unique_text FROM words
UNION
SELECT words_content.c1index_text FROM words_content
LEFT JOIN sms
ON sms.body=unique_text
ORDER BY unique_text
Results in error: No such column: unique_text
How do I join to an alias column and show only records that do not exist in sms.body? Thanks,
If you use where or join clauses in UNIONs you have to apply them to both select statements.
SELECT index_text AS unique_text
FROM words
where index_text NOT IN (SELECT body FROM sms)
UNION
SELECT c1index_text
FROM words_content
WHERE c1index_text NOT IN (SELECT body FROM sms)
ORDER BY unique_text

How can I get a MS Access Union query make a table?

I've finally gotten my hands on MS Access 2010, and I'm unioning some tables.
I'd like to output that union to a (static) table, but if I select the Make Table option it wipes the union query you've just written, and vice versa.
This is trivial to do in SQL (Select * into A from( Select tbl1.col1 from tbl1 UNION Select tbl2.col2 from tbl2 or somethign similar)
But is it possible to do just by pressing buttons?
No, it is not possible with pressing buttons. Use sql view of the query design window.

Resources