MYSQL new event if syntax in phpMyAdmin - mysql

I want to make an event that updates a fields in a table (start)
I created the event in phpMyAdmin and it gives me an error
BEGIN
IF ((select sum(putNextDay) from machineTypes where no=1) = 1) THEN
update prgs set start=CURDATE() where machineNo=1 and start=CURDATE() + INTERVAL 1 DAY and isDone=0;
END IF
END
it gives syntax error near END

on line 2 where you checked the if condition ,
it take your "if(xyz where=1)" condition as "if (xyz=1) " on your code,
it seem that you have assigned a value "1" in "select sum(putNextDay) from machineTypes where no" .
try to apply the same in ''.
hope this will helpful to you .
thanks

Related

mysql update with filter by json selection

This sql statement works
SELECT * FROM form_submission WHERE JSON_EXTRACT(json_data, "$.startup") = "test company"
but when use for update
UPDATE form_submission SET status='submit' WHERE JSON_EXTRACT(json_data, "$.startup") = "test company"
will return error of
"Syntax error in JSON text in argument 1 to function 'json_extract' at position 617"
please help
Some functions, including json_extract, result in an error when encountering bad data only when used in a data modification statement such as UPDATE. You can prevent it in the case by checking if json_data is valid before extracting:
UPDATE form_submission SET status='submit' WHERE JSON_VALID(json_data) AND JSON_EXTRACT(json_data, "$.startup") = "test company";
Fiddle

Before Insert trigger in Derby DB

I am building a javafx project using netbeans and am using derby database for that.
I have two tables, BOOK and ISSUE.
I want to create a BEFORE INSERT trigger ON ISSUE table which checks for the value of a boolean field called "available" in the BOOK table.
A button called BookIssue when pressed, should check the following trigger.
If the value of "available" is false, a warning message should pop up, else the insert operation must be executed.
I am not able to get the trigger command right.
String trigger = "CREATE TRIGGER toIssue NO CASCADE BEFORE INSERT ON ISSUE"
+ " FOR EACH ROW"
+ " BEGIN"
+ " SELECT isAvail from BOOK WHERE id = '"+ bookId + "'"
+ " IF isAvail = false"
+ " THEN RAISE_APPLICATION_ERROR(-20001,'Books Out of stock')"
+ " END IF"
+ " END";
databasehandler.execQuery(trigger);
I am getting following exception in my log:
Exception at execQuery:dataHandlerSyntax error: Encountered "BEGIN" at line 1, column 71.
Could someone help me with this! I couldn't find any other place which had a similar issue with derby.
I think you want: ExecuteNonQuery().
In addition, this line is suspect:
SELECT isAvail from BOOK WHERE id = '"+ bookId + "'"
The trigger body should look more like this:
DECLARE v_avail boolean;
SELECT b.isAvail INTO v_avail
FROM BOOK b
WHERE b.id = NEW.ID;
IF NOT v_isAvail THEN
RAISE_APPLICATION_ERROR(-20001,'Books Out of stock')"
END IF;

Sql Syntax giving unknown syntax error

I am having a problem with my mysql statement.
basically the code works perfectly when inserting, but as soon as the addnew variable=false and it switches to update it gives me an error that i cannot solve.
The Code:
procedure Tadddomain.BitBtn2Click(Sender: TObject);
Var
PrevSql:String;
ID:String;
begin
With Datalive.domains Do
Begin
id:=fieldbyname('id').AsString;
Active:=False;
prevsql:=sql.Text;
Sql.Clear;
Params.Clear;
Addparam(Datalive.domains,'client_id',ftinteger,datalive.clients.FieldByName('id').AsString);
Addparam(Datalive.domains,'domain_name',ftString,Edit1.Text);
Addparam(Datalive.domains,'register_date',ftdate,DateTimePicker1.Date);
Addparam(Datalive.domains,'registered_until',ftdate,DateTimePicker2.Date);
if addnew=true then
Sql.Text:='Insert into domains (client_id,domain_name,register_date,registered_until) VALUES (:client_id,:domain_name,:register_date,:registered_until)'
Else if addnew=False then
Sql.Text:='Update domains (domain_name=:domain_name, register_date=:register_date, registered_until=:registered_until) where id='''+id+'''';
Showmessage(sql.text);
execsql;
sleep(100);
sql.Text:=prevsql;
active:=True;
done:=True;
adddomain.Close;
End;
end;
The Error:
Project project1.exe raised exception class EZSQLException with message 'SQL Error: You have an error in your SQL syntax; check the manual that corresponds to your mysql server version for the right syntax to use near '(domain_name='asd',register_date='2014-11-09',registered_until='2015-11-09') w' at line 1'.
Any Assistance would be great, I have searched and searched and cannot find the fault.
Update:
I changed the edit code as suggested below, and now no errors apear what so ever. But also nothing happens. It does not edit the record.
if addnew=true then
Sql.Text:='Insert into domains (client_id,domain_name,register_date,registered_until) VALUES (:client_id,:domain_name,:register_date,:registered_until)'
Else if addnew=False then
Begin
sql.Add('Update domains');
sql.Add('set domain_name=:domain_name,');
sql.Add('register_date=:register_date,');
sql.Add('registered_until=:registered_until');
sql.Add('where id=:id');
End;
Update syntax is like this
Update domains
set domain_name = :domain_name,
register_date = :register_date,
registered_until = :registered_until
where id = :id
You declare the "client_id" parameter not the "id" parameter you use in the update statement.
where id = :id
should be
where id = :client_id
I have found the problem.
If i define the parameters before i use the sql.add() function the parameters are discarded.
When I moved the
Addparam(Datalive.domains,'client_id',ftinteger,datalive.clients.FieldByName('id').AsString);
Addparam(Datalive.domains,'domain_name',ftString,Edit1.Text);
Addparam(Datalive.domains,'register_date',ftdate,DateTimePicker1.Date);
Addparam(Datalive.domains,'registered_until',ftdate,DateTimePicker2.Date);
to just before the execsql; the problem was solved.

Spaces in parameter values when executing sql stored procedures in powershell

I'm having an issue sending a msyqlcommand to execute a store procedure.
This the code that I am using, I have a cmdlet called set-outage I've created. It has parameters set and the only code in it that's important to this is these two lines. Everything else handles what happens after. It errors on the ExecuteNonQuery line.
$query = New-Object Mysql.data.mysqlclient.mysqlcommand("set_outage('$system','$startd','$startt','$endd','$endt','$changes','$user')", (invoke-mysql))
$rdr = $query.ExecuteNonQuery()
all of my parameters are setup as
[string]$variable
The stored procedure is
CREATE DEFINER=`root`#`localhost` PROCEDURE `set_outage`(in system varchar(55), in startd varchar(25), in startt varchar(25), in endd varchar(25), in endt varchar(25), in changes longtext, in user varchar(55))
BEGIN
Insert into to_process (node_name, User_Requested, Start_date, Start_time, End_Date, End_time, Started ) Values (system, user, startd, startt, endd, endt, 'no');
end
MySQL allows text values to be stored in date and time fields so that's not a worry. I know changes isn't being used in this command but it will be after I work this out.
Here are two examples... one that works.. and one that doesn't.
set-outage -system viper12 -startd 2014/12/12 -startt 12:00 -endd 2014/12/17 -endt 17:00 -user 'Daniel asdfsdaf' -changes 'test'
The error returned:
Exception calling "ExecuteNonQuery" with "0" argument(s): "You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near
'set_outage('viper12','2014/12/12','12:00','2014/12/17','17:00','test','Daniel as' at line 1"
At N:\NOC\Powershell Scripts\testingmysql.ps1:194 char:9
+ $rdr = $query.ExecuteNonQuery()
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : NotSpecified: (:) [], MethodInvocationException
+ FullyQualifiedErrorId : MySqlException
If I simply remove the extra words or the spaces like so:
set-outage -system viper14 -startd 2014/12/12 -startt 12:00 -endd 2014/12/17 -endt 17:00 -user 'Daniel' -changes 'aasdfsadfsadfsadfasfsdfsadfsatest'
I no longer get an error. I've put " " around them I've used ' ' around them. I've tried to convert the input into a here_strings, I've used the backtick... nothing I do seems to allow the spaces. I've even tried to use #query.prepare() but that doesn't help either. I've tried to single quote the variables in MySQL as well. No luck.
If I direct the output of $query it looks like this.
LastInsertedId : 0
CommandText : set_outage('viper12','2014/12/12','12:00','2014/12/17','17:00','test','Daniel asdfsdaf')
CommandTimeout : 30
CommandType : Text
IsPrepared : False
Connection : MySql.Data.MySqlClient.MySqlConnection
Parameters : {}
Transaction :
EnableCaching : False
CacheAge : 0
UpdatedRowSource : Both
DesignTimeVisible : False
Site :
Container :
If I take the commandtext and use it directly in MySQL it works perfectly. I can't seem to find the right way to quote the data show it will pass with spaces in the variables. Can you point me in the right direction? This could be a stupid overlook on my part.. but I'm still kind of new to this stuff so be gentle :)
It looks like, from what I can find, that it's the way I'm passing the information.
Could be wrong...but this way works... by setting the parameters this way verses passing them in the command string.
$query = New-Object Mysql.data.mysqlclient.mysqlcommand("set_outage", (invoke-mysql))
$query.CommandType = [System.Data.CommandType]'StoredProcedure'
$query.Parameters.Add("user","$user")
$query.Parameters.Add("system","$system")
$query.Parameters.Add("changes","$changes")
$query.Parameters.Add("startd","$startd")
$query.Parameters.Add("startt","$startt")
$query.Parameters.Add("endd","$endd")
$query.Parameters.Add("endt","$endt")
This appears to only be an issue if I have spaces in my sql command inside the mysqlcommand.
I tested this with the following command:
set-outage -system viper3 -startd 2014/12/12 -startt 12:00 -endd 2014/12/17 -endt 17:00 -changes 'this is a test with lots of spaces' -user 'Daniel Peel'
and I had no issues, it ran and the stored procedure worked as it was supposed to. Any additional input is always welcome.

How to increase the user' error times? unknown whether the original value is null

Before operate Access database,in the SQL express,I can just use this statement and it works very well:
UPDATE Person
SET FErrorTimes = IsNull(FErrorTimes, 0) + 1
WHERE (FUserName = #name)
by now, it reports as error syntax
can someone help me please,and thanks very much.
Access' IsNull is different than SQL Server's IsNull. In Access, IsNull accepts only one argument, and returns True or False to indicate whether that argument evaluates as Null.
Use one of these instead.
UPDATE Person
SET FErrorTimes = Nz(FErrorTimes, 0) + 1
WHERE FUserName = #name
UPDATE Person
SET FErrorTimes = IIf(FErrorTimes Is Null, 0, FErrorTimes) + 1
WHERE FUserName = #name
Note Nz() is only available for a query run within an Access session. If you're running a query from external code which connects to the Access db, use the second example.