SELECT NULL in OpenEdge Query - openedge

I would like to be able to SELECT a NULL value in my SELECT list so I can use it later in a UNION (for example). When I try SELECT NULL AS my_null_value FROM some_table it gives me a syntax error. I can work around it by doing the following.
SELECT CASE WHEN 1=1 THEN NULL END AS my_null_value
FROM some_table
Is there a cleaner way to accomplish this? I feel like I have done this in less characters and in a simpler manner in the past than using a CASE statement.

I ending up finding it.
A simpler (or more shorthand) way to accomplish this is to use the following:
SELECT NULLIF(0,0) AS my_null_value
FROM some_table

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.

Can I loop over the results of an SQL query to use each value in another query all at once in SQL Developer?

What I want to achieve (if it is possible from SQL Developer) is that when I execute the script it do the following:
Run a SELECT statement that will return a list of IDs. Approx 270 records.
I need to use each of those IDs individually in another SELECT statement (in the WHERE clause) which will return some records. A few of this could result in over 17,000 records and some can be one.
Then each result from the second SELECT I want it to be exported to an excel or csv file into a folder at my pc.
I have both 'Select' ready but I don't know how to loop over the results of the first to grab each ID and use it in the second one. Also I don't know how to export automatically from the code.
You can use GROUP BY clause.
read more here:
http://docs.oracle.com/javadb/10.6.2.1/ref/rrefsqlj32654.html
If the first SELECT returns you IDs only, and that's all you need for your second SELECT, just use IN clause providing your first SELECT query for the second one.
Example:
-- Your second select
SELECT
col1
,col2
,col3
,col4
FROM
second_table
WHERE
some_foreign_id IN (
-- Your first select
SELECT
id
FROM
first_table
WHERE
some_conditions...
)
In my opinion don't use PLSQL for this. In PLSQL the only way you can get an output of this by using a REFCURSOR (unless you dont use UTIL File package to do it). A simple SELECT WITH JOIN condition will suffice your requirement.
Test illustration
SELECT A.* FROM TABLE_A A, TABLE_B
WHERE A.COMMON_COLUMN = B.COMMON_COLUMN;
Hope this helped you in same way

SQlite3 bound params and LIKE

Sqlite3 provides the sqlite3_bind_* functions which allow one to do parameter substitution into a SQL query. My question is: what is the right way to combine this with LIKE queries? For example, I might want to do:
SELECT * FROM thing WHERE name LIKE '%?'
but that doesn't work at all. Is the best way really just:
SELECT * FROM thing WHERE name LIKE ?
and then put the pattern characters into the actual string value to be substituted?
To concatenate strings, use the || operator:
SELECT * FROM thing WHERE name LIKE '%' || ?

preg_replace for changing sql query string

I have problems with sqlite3 and "num_rows". I cannot ask for num_rows of a result like mysql.Now i wan't to automate my SELECT query to ask for the rowcount.
Is there a way to replace all between two patterns?
Like
SELECT date,name,etc FROM myTable WHERE gender=1
// to
SELECT count(*) FROM myTable WHERE gender=1
I must replace all between SELECT and FROM with count(*).
Can i do this this preg_replace?
For your example provided it's simple, but I guess it won't work for all query syntaxes. But have it a try:
PHP code (don't know language you do use):
$count_sql = preg_replace('/^(SELECT)\\s+.*?\\s+(FROM.*)$/s', '$1 count(*) $2', $select_sql);
But I guess, there is a more elegant way to find the num_rows thing. You could use a subquery, like:
SELECT COUNT(*) FROM ( *** original query here *** )
It's not very efficient, but you don't have to deal with the query syntax.

Resources