EXEC SQL DROP STATEMENT foo; gives syntax error - sybase-ase

The following ESQL/C code follows exactly what the Sybase manual say about the usage:
...
strcpy(anweisung, "UPDATE image_test SET katkey = ? WHERE CURRENT OF hc_image_test");
EXEC SQL PREPARE updt FROM :anweisung ;
EXEC SQL EXECUTE updt USING :key;
EXEC SQL DROP STATEMENT updt;
EXEC SQL CLOSE hc_image_test;
EXEC SQL COMMIT TRANSACTION;
...
The UPDATE of the row goes fine but the DROP STATEMENT (to free the resources) gives:
** SQLCODE=(-102)
** ASE Error
** Incorrect syntax near 'STATEMENT'.
I have no idea what could be wrong with this.

Related

LOAD DATA INFILE syntax error if executed from php or shell script

I have the following SQL command inside test.php file to be executed by cron:
$sql = "
DELETE FROM `module_currency_rates_rate`;
LOAD DATA INFILE '/export/home/mysql/test.txt' INTO TABLE `module_currency_rates_rate` FIELDS TERMINATED BY ',';
UPDATE `module_currency_rates_rate` SET `date_add` = now();
";
if(!mysql_query($sql)) exit(mysql_error());
When I execute this script via command line (all paths are absolute just to prove that there are no errors here) in shell:
/www2/bin/php /export/home/mysql/bin/test.php
I always get the following 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 'LOAD DATA INFILE '/export/home/mysql/test.txt'
However, when I run the same script WITHIN mysql command line (mysql-uuser -ppass), the files loads correctly and there are no syntax errors! I'm desperately searching for a solution to run query via php script. Just to add: all necessary permissions are given to the files and for unix users.

No output for multiple queries in mysql

We dynamically generate a sql file which contains one big insert query. This file will be run periodically from a PHP app with the following command:
mysql --force -u foo -pbar demo < demo-file.sql
There was no output from the command except when an error happened as the content of the file was only one insert command. Now we decided to change the file that it will contain multiple insert queries instead of one big query. Since then the output of the command is
0
0
0
Our PHP app fails because it assumes now that there is an error happening because the output is not empty. So my question is, can I write an sql file that there is no output generated with multiple queries? I try not to touch the PHP app.
I know there are better designs but the code is historically grown :-)
[UPDATE 1]
Basically the app does
$response = shell_exec('mysql --force -u foo -pbar demo < demo-file.sql');
if (empty($response)) {
echo 'OK';
} else {
echo 'error: '.$response;
}
[UPDATE 2]
The sql file contains something like
insert into;
select sleep(0);
insert into;
select sleep(0);
insert into;
select sleep(0);
Those zeros come from the sleep lines. Get rid of them, you don't need them.
Also, for performance sake, if you are using a schema that supports transactions, you should add:
START TRANSACTION
INSERT...
INSERT...
INSERT...
...
COMMIT

How to change mysql delimiter through php

I created an event with a set of sql statements in mysql terminal, It is working fine. But I want to create this event from php script. My query is below.
DELIMITER |
CREATE EVENT e_cart
ON SCHEDULE AT CURRENT_TIMESTAMP + INTERVAL 3 MINUTE
DO
BEGIN
SET #var_orderid = '';
SET #var_orderid = (SELECT orderid FROM order WHERE id=308);
IF #var_orderid = NULL THEN
UPDATE order SET status=1 WHERE id=308;
END IF;
END |
DELIMITER ;
This results in :-
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 'DELIMITER |
CREATE EVENT e_cart
ON SCHEDULE AT CUR' at line 1
How do I execute this event from php script?
DELIMITER isn't MySQL server command but MySQL CLI/Query Browser/Workbench etc. It only says when to send command to MySQL server.
Omit DELIMITER and send whole CREATE EVENT statement as one command, it should work.

in php5, how to use mysql_execute in script that has delimiter

Have a script that I can copy and run in mysql shell just fine, but explodes when attempting the same script in php5 mysql_query. Part of the script:
-- sync shadow with users
drop trigger users_post_insert;
delimiter $$
create trigger users_post_insert after insert on users
for each row
begin
insert into shadow( usr_id ) values( new.usr_id );
end$$
delimiter ;
Raises 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
'delimiter $$\ncreate trigger users_post_insert after insert
on users\n\tfor each ro' at line 8
Again, have done similar executing script files with PostgresQL and Oracle scripts, so this took me off guard.
Do MySQL scripts need to be passed through a regex before being run, or what?
These are scripts that have been debugged and then applied via php to new schemas.
Might you need to define delimiter first before executing a command, and make sure you dropped an already created trigger (or just use if exists clause) try this
-- sync shadow with users
delimiter $$
drop trigger if exists users_post_insert;$$
create trigger users_post_insert after insert on users
for each row
begin
insert into shadow( usr_id ) values( new.usr_id );
end;$$
delimiter ;
Testing shows mysql_execute cannot accept the DELIMITER command.
To make it work break up your file into chunks, leaving out the DELIMITER lines altogether. For example,
$procs = file_get_contents( '../sql/security.sql' );
$procArr = preg_split( "/delimiter .*\n/", $procs );
Also, remove the set delimiter "$$" from the remaining chunks.
Feed each non-empty chunk into mysql_execute - it's ugly, but it works.

DBD::Oracle and DBI error in perl

I am having a strange issue here with perl and DBI module. I can get the query successfully sometimes, but sometimes, when I add a line of code which is remotely related to database access or anything like that, I got an error saying:
DBD::Oracle::st fetchrow_array failed: ERROR no statement executing (perhaps you need to call execute first) [for Statement "select * from (...)"] at script.pl line 18.
I verified using sqlplus that my select command has no problem here (of course, that is why I said the script worked sometimes!)
If I added a semicolon after the select command in the perl script, I got another error saying:
DBD::Oracle::db prepare failed: ORA-00911: invalid character (DBD ERROR: error possibly near <*> indicator at char 970 in 'select * from (...)<*>;') [for Statement "select * from (...);"] at script.pl line 13.
Can anyone please suggest to me what is going on here? Is it because the sql command is too long (~900 chars)?
That error means you've prepared a statement but not executed it. You may also get it if you prepared a statement, executed it and fetched all the rows then call fetch again but I'm less sure about that. Don't put semi-colons on the end of your SQL in this case as it is not required.
See https://metacpan.org/pod/DBI#Executed for th executed attribute.
I had the same issue, and I notice that I had a fetchrow_array after a fetch, that was the problem.
while ($sth->fetch) {
sth->fetchrow_array;
$myvar = some logic here
$myvar2 = some other logic here
}
I removed the sth->fetchrow_array; and now everything is working :)

Resources