OpenEdge ABL / Progress 4GL Query - progress-4gl

I need help writing this progress query:
find first a no-lock
where a.a = variable
and a.b = variable2
and a.c = variable3
and ((a.d <> variable4
and a.e <> variable5
and a.f <> variable6)
/* this "or in" is just sudecode for what I want it to do */
or in (first b no-lock where b.a = variable
and (b.b = variable7 or b.b = variable8 no-error)))
no-error.`
The "or in" is the thing I'm having trouble with.

Given the kind of statement you want to create, I suggest the following two-step process:
find first b no-lock where b.a = variable
and (b.b = variable7 or b.b = variable8) no-error.
find first a no-lock where a.a = variable
and a.b = variable2
and a.c = variable3
and ((a.d <> variable4 and a.e <> variable5 and a.f <> variable6)
or available b)
no-error.

Don't think too much it like MS SQL query or the general scripting query.
like
for each test1 no-lock where test1.a = var1 and test1.b = var2 no-error.
same as
find first test1 no-lock where test1.a = var1 and (test1.b = var2 or test1.c = var3) no-error. // it gives you only one row at a time.

I believe the statement you are looking for is 'can-find'.
... or can-find(first b where b.a = a.a ...

Related

How to perform WHERE in with multiple columns in postgres

I wants to do something like this,
SELECT * FROM product p
JOIN product_version pv ON p.id = pv.product_id
where (p.code, pv.product_version) in (("FF6",1), ("FF12", 1));
But this is giving error at in clause.
Can someone provide the correct syntax.
You are not providing any information about the actual error neither about column types.
But, by the way, it really looks like that those double quotes are wrong because in Postgres strings are quoted using simple quotes ('), not double (").
Try:
SELECT *
FROM product p
JOIN product_version pv ON (p.id = pv.product_id)
where
(p.code, pv.product_version) in (('FF6',1), ('FF12', 1))
;
Despite that, your query looks syntactically "correct" unless some kind of type mismatching we cannot foresee without more information.
You probably can't go with IN, depending on your goal you need to do something like:
where (p.code = "FF6" and pv.product_version = 1) or
(p.code = "FF12" and pv.product_version = 1)
or, if the logic above was not what you meant, maybe:
where p.code IN ("FF6", "FF12} AND pv.product_version IN (1)
or
where p.code IN ("FF6", "FF12} OR pv.product_version IN (1)
This code should work for you
SELECT * FROM product p
JOIN product_version pv ON p.id = pv.product_id
where p.code in("FF6","FF12") and pv.product_version = 1

How should I use the COALESCE function in PostgreSQL?

I've written the following code:
UPDATE prueba t2
SET num = coalesce(t1.n_locnae, 0)
FROM prueba2 t1
WHERE t2.utm = t1.utm and t2.sem = t1.sem;
but it still includes null values in the column num.
How can it be solved?
You have values of utm and sem in prueba that you don't have in prueba2, so get filtered out by your UPDATE statement.
You probably need something like (untested):
UPDATE prueba pa
SET num = coalesce(p2.n_locnae, 0)
FROM prueba pb
LEFT JOIN prueba2 p2
ON (pb.utm,pb.sem) = (p2.utm,p2.sem)
WHERE (pa.utm,pa.sem) = (pb.utm,pb.sem);
This should ensure that all num values are updated, whether they their key is matched against the join or not.

Column is not updating in postgresql

I tried to update my table like below:
$query = "select *
FROM sites s, companies c, tests t
WHERE t.test_siteid = s.site_id
AND c.company_id = s.site_companyid
AND t.test_typeid = '20' AND s.site_id = '1337'";
$queryrow = $db->query($query);
$results = $queryrow->as_array();
foreach($results as $key=>$val){
$update = "update tests set test_typeid = ? , test_testtype = ? where test_siteid = ?";
$queryrow = $db->query($update,array('10','Meter Calibration Semi Annual',$val->site_id));
}
The above code is working good. But in update query , The column test_typeid is not updated with '10'. Column test_typeid is updating with empty value. Other columns are updating good. I dont know why this column test_typeid is not updating? And the column test_typeid type is integer only. I am using postgreSql
And My table definition is:
What i did wrong with the code. Kindly advice me on this.
Thanks in advance.
First, learn to use proper JOIN syntax. Never use commas in the FROM clause; always use proper explicit JOIN syntax.
You can write the query in one statement:
update tests t
set test_typeid = '10',
test_testtype = 'Meter Calibration Semi Annual'
from sites s join
companies c
on c.company_id = s.site_companyid
where t.test_siteid = s.site_id and
t.test_typeid = 20 and s.site_id = 1337;
I assume the ids are numbers, so there is no need to use single quotes for the comparisons.

How to search all tables and all fields for a string?

I want to search all fields in all tables of a database for a user supplied value and display records which contain that input keyword. Something like this:
FOR EACH _file WHERE (NOT _file-name BEGINS "_" AND NOT _file-name BEGINS "sys")
NO-LOCK:
FOR EACH _field OF _file
NO-LOCK:
ASSIGN
ttable = _file._file-name
tfield = _field._field-name .
FOR EACH &ttable WHERE ttable.tfield MATCHES "urpon frisbee "
NO-LOCK :
MESSAGE "hai"
VIEW-AS ALERT-BOX INFO BUTTONS OK.
DISPLAY _file._file-name .
END.
END.
END.
You want to study "dynamic queries".
procedure x:
define input parameter tbl as character no-undo.
define input parameter fld as character no-undo.
define input parameter xyz as character no-undo.
define variable qh as handle no-undo.
define variable bh as handle no-undo.
define variable fh as handle no-undo.
create buffer bh for table tbl.
create query qh.
qh:set-buffers( bh ).
qh:query-prepare( "for each " + tbl ).
qh:query-open.
qh:get-first( no-lock ).
do while qh:query-off-end = no:
fh = bh:buffer-field( fld ).
if fh:buffer-value = xyz then /* needs special handing if there are array fields in the db ... */
do:
display tbl fld bh:recid fh:buffer-value.
pause.
end.
qh:get-next( no-lock ).
end.
delete object bh.
delete object qh.
return.
end.
for each _file no-lock where not _hidden:
for each _field no-lock of _file:
if _data-type <> "character" then next. /* skip non-char fields */
run x ( _file-name, _field-name, "urpon frisbee" ).
end.
end.

Conditional / Optional where statement

I have a stored procedures and a few boolean variables (Bit).
I want to put a WHERE statement if a certain variables is False.
I know I could do something like this :
IF (#myBoolean = 1)
BEGIN
SELECT * FROM myTable
END
ELSE
SELECT * FROM myTable WHERE myTable.Foo = 'Bar'
Is there a way to make the WHERE statement optionnal ? Because I have so many boolean variable I don't want to have a different query for each possibilities.
This is what I have in mind (I know it does not work) :
SELECT * FROM myTable
CASE WHEN #myBoolean = 0
THEN WHERE myTable.Foo = 'Bar'
ELSE --Do nothing
SELECT * FROM myTable
where ( #myboolean = 1 and foo = 'bar' ) or #myboolean = 0