MySQL: How to tell if a cell value has changed? - mysqli

I am pulling data from table Supervisors. Through each iteration, I want to know when Supervisor.SupNum changes. If the value changes, do this. If it stays the same, do the other.
How would I accomplish this?

try this.
if (Supervisor.SupNum = chk) then
flag =true; else
chk := Supervisor.SupNum; flag =false end if;
If you want the first iteration to be true then have another iterator i.
if (i=0) then Supervisor.SupNum := chk; end if; i++;

Related

How to return boolean in if else statement and the return value will link to anothe if else statement in Matlab?

I have the following if else statement that created by myself in order to link to the if else statement given in second part:
m=4
if m==3
disp(true)
else
disp(false)
Second part ( this code is fix cannot be change):
if (true)
A=Hello World
else
A=Bye
If using the first part code, my output will be
A=Hello World
but my desire output is
A=Bye
Anyone one have idea to edit the first part, because now my return value in first part not able to link to my second part.
If you can't change the second part's code, I'm afraid your desire cannot be fulfilled. Or rather, I'm afraid your code won't run at all, because your perenthesis, quotes, end-statement (and arguably semicolons) are not in place.
if true
A = 'Hello World';
else
A = 'Bye';
end
This code will return A = 'Hello World', no matter what, since true is always true. If-else conditions work like this:
if (*what's in here evealuates to true*)
%do stuff
else (*if what's up there does not evaluate to true*)
%do other stuff
Clearly, true will always evaluate to true. So the above if-else condition will always return A = 'Hello World'.
You don't need two if statements in order to accomplish this task. One is more than enough to perform all what you need:
m = 4;
if (m == 3)
A = 'Hello World';
else
A = 'Bye';
end
disp(A);
A few comments concerning your code:
if statements need to be closed with an end
if (true) will always pass into the statement
the disp function doesn't assign a value, its only goal is to display it in the Command Window
in order to work with text, you have to enclose it within single quotes ' (char array) or double quotes " (string), more info here
If you posted only small excerpts of your code and you need to perform those two checks sequentially, in different parts of your script, then:
m = 4;
if (m == 3)
m_equals_3 = true;
disp('M == 3');
else
m_equals_3 = false;
disp('M ~= 3');
end
% then, somewhere else...
if (m_equals_3)
A = 'Hello World';
else
A = 'Bye';
end
% ...
I guess this is a homework exercise. You should disclose that if it’s the case.
The exercise requieres you to change the workspace such that the second bit of code evaluated the else case. This can be accomplished by changing the meaning of true. In your first bit of code, make it so that
true = flase;
Or equivalently,
true = 0;
Note that this is really bad form, if you ever do something like this outside of a homework exercise that explicitly asks you to do so, you’ll get fired or maybe even shot. You’ve been warned!
By the way, I assume that the missing quote characters around the strings and the missing ends are typos?

PL/SQL How to implement conditional select

I have a small problem which I believed I could solve simply, it turns out I'm not able to figure out.
I have following query:
SELECT custom_field
INTO v_start_of_invoice
FROM BILL
WHERE BIMA_TRACKING_ID = v_previous_BIMAtrackingID
AND BSCO_CODE_ID = 'PRPAYMENT'
AND PREP_SEQ_NUM = 0
AND ITEM_CAT_CODE_ID = 1
AND PARTITION_KEY = v_prev_partition
AND SUBPARTITION_KEY = v_prev_subpartition;
What I would like to achieve here is to give to variable v_start_of_invoice the value "0" if one or all the where condition are not met.
In simple word I don't want the script to fail but I want the variable either to be set with some value if all the where conditions are matched, otherwise I want to assign the value 0.
I'm sure there are quite a few ways but I need to check what could be the best way to achieve that.
Many Thks in advance
M.
You seem to have a misunderstanding of what the exception block actually does. First off you cannot avoid the exception. If you use "select into" the query is successful only when exactly 1 row is returned. Otherwise PLSQL will internally raise the NO_DATA_FOUND when the query returns 0 rows, and TOO_MANY_ROWS when it returns more then 1 row. The exception block tells PLSQL what to do when an error is generated. Basically the exception block tells what action to take on specific errors and whether to continue error processing or discard the error. (Check the RAISE and RAISE_APPLICATION_ERROR statements.)
Keep in mind that blocks can be nested. With this in mind and in context of a outer block the solution offered by #are is exactly what you want. The structure becomes:
Begin
.
.
.
begin
place #are's code here.
end ;
-- Continue your code here: The Error has been handled and execution continues as though the exception never happened.
.
.
.
end;
As far as the "NVL(v_start_of_invoice, 0);" it's a function that will return 0 if v_start_of_invoice is NULL and v_start_of_invoice otherwise. Note the value of INTO variables is not it the select generates an error unless you set it in the exception block.
begin
SELECT custom_field
INTO v_start_of_invoice
FROM BILL
WHERE BIMA_TRACKING_ID = v_previous_BIMAtrackingID
AND BSCO_CODE_ID = 'PRPAYMENT'
AND PREP_SEQ_NUM = 0
AND ITEM_CAT_CODE_ID = 1
AND PARTITION_KEY = v_prev_partition
AND SUBPARTITION_KEY = v_prev_subpartition;
exception WHEN NO_DATA_FOUND THEN
v_start_of_invoice := 0;
end;

SAS hash objects: multidata merge

I'm new to hash objects, but I'd like to learn more about them. I'm trying to find ways to substitute all possible proc sql and regular merges with hash whenever possible. While playing around with SASHELP datasets, I ran into the following issue:
Let's say I have a dataset of 10 unique observations (car manufacturer) and I want to match it up with another table that contains various models of these cars, so the car make repeats in that table. The other important aspect to note is that not all car makes are present in the table I'm looking up, but I still would like to retain those in my table.
Consider the code below:
proc sql noprint;
create table x as select distinct make
from sashelp.cars;
quit;
data x;
set x (obs = 10);
if make = "GMC" then make = "XYZ";
run;
data hx (drop = rc);
if 0 then set sashelp.cars(keep = make model);
if _n_ = 1 then do;
declare hash hhh(dataset: 'sashelp.cars(keep = make model)', multidata:'y');
hhh.DefineKey('make');
hhh.DefineData('model');
hhh.DefineDone();
end;
set x;
rc = hhh.find();
do while(rc = 0);
output;
rc = hhh.find_next();
end;
if rc ne 0 then do;
call missing(model);
output;
end;
run;
If all makes in table X were also in table cars, then removing output command after call missing(model) would do exactly what I want. But I also want to make sure that make "XYZ" will remain in the table.
The existing code, however, produces a blank after it find all matching models, like so:
make model
==========
Acura MDX
Acura RSX Type S 2dr
Acura TSX 4dr
... (skipping a few rows)
Acura NSX coupe 2dr manual S
Acura
Audi A4 1.8T 4dr
As you can see, in the above table, there is a missing model in the second to last row. This pattern appears in the end of every make.
Any suggestions on how to fix this would be highly appreciated!
Many thanks
The direct answer: you need to consider this section.
rc = hhh.find();
do while(rc = 0);
output;
rc = hhh.find_next();
end;
if rc ne 0 then do;
call missing(model);
output;
end;
What's happening here is you are repeatedly trying to find next, fine, until you fail. Okay. Now you're in rc ne 0 condition, though, even though you really mean that last step to only be used if you didn't even find one.
You can handle this a couple of ways. You can do this:
rc = hhh.find();
if rc ne 0 then do;
call missing(model);
output;
end;
else
do while(rc = 0);
output;
rc = hhh.find_next();
end;
Or, you can add a counter to the do while loop, and then execute the call missing/output if that counter stores a 0. The above is probably easier.
Further, you probably should consider whether a hash is the right solution for this problem. While it is possible to solve this with multidata hashes, keyed set is usually more efficient for something like this, and much easier to code.

Sybase. Break or continue inside of a for loop

I'm writing Sybase procedure and ran into a problem: how to break inside of a for loop. It just gives me syntax error. Is it possible to skip the current row and go to the next one in for loop?
for TmpUserEvents as TmpUserCursorEvents dynamic scroll cursor for
select
*
from
test
do
if flag = 1 then
continue; --break;
end if;
end for;
To answer my own question: it is possible to continue for loop while executing.
for TmpUserEvents as TmpUserCursorEvents dynamic scroll cursor for
select
*
from
test
do
lbl:
loop
if condition = 1 then
leave lbl;
end if;
end loop lbl;
end for;

With Maple how can i prompt the user to answer a boolean expression?

The exact question would be "is the equation you want to use x=f(Xo)". This is in an if statement already so if true then continue if not then prompt user to enter a different function.
Your bit about its already being inside an if statement isn't very workable because that doesn't allow for an alternative value to be assigned to something in the case that the initial response is negative.
You should be able to work with something like this. Call p(), and assign its result to a ans, say, and then work with that value (and/or test it for some properties).
restart:
p := proc()
local answer, oldprompt, res1, res2;
oldprompt := interface(':-prompt'=``);
try
printf("Is the equation you want to use x=f(Xo)? (y/n)\n");
res1 := readline(-1);
if member(res1,{"y;","y","yes;","yes"}) then
answer := x=f(Xo);
elif member(res1,{"n;","n","no;","no"}) then
printf("Enter your equation.\n");
res2 := readline(-1);
answer := parse(res2);
else
printf("Response not recognized\n");
end if;
catch:
finally
interface(':-prompt'=oldprompt);
end try;
if answer='answer' then NULL else answer end if;
end proc:
ans := p();
[edited below]
It is possible to get it a little closer to your original. With procedure p as below the returned result will be one of true/false/FAIL and could be used in a conditional. In the case that the return values if false (because of the response to the initial query) then a second query is made about the choice of another expression.
This version of p takes two arguments, the first is the suggested initial equation. The second is a name which can be assigned any alternative.
restart:
p := proc(candidate, resultvar)
local result, oldprompt, res1, res2;
oldprompt := interface(':-prompt'=``);
try
printf(sprintf("Is the equation you want to use %a? (y/n)\n",
candidate));
res1 := readline(-1);
if member(res1,{"y;","y","yes;","yes"}) then
result := true;
assign(resultvar,candidate);
elif member(res1,{"n;","n","no;","no"}) then
result := false;
printf("Enter your equation.\n");
res2 := readline(-1);
assign(resultvar,parse(res2));
else
printf("Response not recognized\n");
result := FAIL;
end if;
catch:
finally
interface(':-prompt'=oldprompt);
end try;
return result;
end proc:
Now we can test it out.
p(x=f(X0), 'ans');
ans;
We could also use the call to p inside an if statement. Eg,
if p(x=f(X0), 'ans') then
"accepted";
else
"new choice made";
end if;
ans;
Here, answering "n" to the first query will make the conditional test see a false value, but the named argument ans will get assigned to as a side-effect.