this is my code :
CREATE TRIGGER Zmiana_kategorii
ON Hotele
AFTER UPDATE
AS
BEGIN
DECLARE #stara smallint, #nowa smallint
IF COL_LENGTH('deleted', 'IloscGwiazdek')
BEGIN
SET #stara=(SELECT IloscGwiazdek FROM deleted)
SET #nowa=(SELECT IloscGwiazdek FROM inserted)
IF(#stara<#nowa)
BEGIN
print 'Powiadom następujących klientów o zmianie klasy hotelu'
declare #data date
SET #data=(CONVERT (date, GETDATE()))
SELECT KlientID FROM Rezerwacje Where #data<DataPrzyjazdu
END
END
END
could someone tell me what is wrong in syntax? I am 1st time using MYSQL i have no clue whats is wrong with this...
this is error : ON Hotele AFTER UPDATE AS BEGIN DECLARE #stara smallint, #nowa smallint ' at line 2" this is error
At least one problem is that you don't include the THEN after your IF conditions. For example:
IF (#stara<#nowa) THEN
BEGIN
-- ...
END
Another problem is that you have the order of the CREATE TRIGGER elements wrong. It should be:
CREATE TRIGGER Zmiana_kategorii
AFTER UPDATE
ON Hotele
Note that the AFTER UPDATE goes before the ON. This seems to solve your specific problem.
Finally, MySQL doesn't have a PRINT command and all variables have to be declared at the beginning of a BEGIN block, before any other statements.
Please read this entire page and understand what's going on: http://dev.mysql.com/doc/refman/5.0/en/create-trigger.html
If you understand the syntax, then you can debug it yourself. These types of simple problems shouldn't require community assistance (though sometimes an extra pair of eyes can catch an obvious error).
Related
I'm new to the php mysql developpement, I want to make a trigger to be launched after I insert a row in the evolution table. The trigger must take a value (prixMisDaccord) from another table (inscription) and reduce it value from the evolution column prixAPaye.
Here is what I tried and what I found on Stack Overflow:
DELIMITER $$
CREATE TRIGGER trg_rap
BEFORE INSERT ON evolution FOR EACH ROW
BEGIN
DECLARE pmd float;
-- Check BookingRequest table
SELECT prixMisDaccord
INTO #pmd
FROM inscription
WHERE inscription.idETD= 1;
SET NEW.resteAPaye = #pmd-NEW.prixPaye
WHERE idETD = 1;
END;
$$
DELIMITER `;
'i have a probleme from this line SELECT' - Is not the error I get, I do get an error on the set statement because you cannot apply a where clause to a set..There are other problems with your code and you don't seem to know the difference between user defined variables and declared variables see - How to declare a variable in MySQL? and temporary tables..so #pmd-NEW.prixPaye is just nonsense.
If you want more help read https://stackoverflow.com/help/how-to-ask and provide table definitions,sample data and desired outcome all as text in the question.
Sarcastically enough the problem is that I am not getting any errors. The trigger is the following one:
DELIMITER ##
CREATE TRIGGER autohome1
AFTER INSERT ON prueba
FOR EACH ROW
BEGIN
DECLARE cmd VARCHAR(255);
DECLARE result int(10);
SET cmd=CONCAT('sh /home/pi/Desktop/Py_Script_Auto_Home/autohome.sh');
SET result = sys_exec(cmd);
END;
##
DELIMITER ;
If I am not wrong (I dont know much about this topic), whenever I insert into the table named "prueba", the command should be executed. Or at least that is what I understand this should do, However this is not happening. I am not clear about the functionality of the line "SET result = sys_exec(cmd);"
I know that the command I want to execute works so I do not know from where should I start tackling my problem.
I'm wondering if there is a way, after pragmatically giving a Varchar variable a value, to use that variable in the From Statement? We have archive tables that will cycle out and back in. So, the table can go missing for several days and when they cycle back in they change the table name to end w/ the current year (Ex. 012015 when before it was 012014.) Because of this my query can throw up object errors and I'm the only guy on my team that understands the simple error and how to quickly fix the stupid thing. below is the solution I'm trying to get to work, but I keep getting the error "Must declare the table variable #Jan." I totally understand what a table variable is and how to use it and that is obviously not what I want to do here. Is there any way to use a varchar variable (or other similar variable type) in the From Statement? Code below:
Declare #Jan Varchar(40)
IF OBJECT_ID('ColTelephonyArchive.dbo.ACDSkill201401') IS NOT NULL
Begin
Set #Jan = 'ColTelephonyArchive.dbo.ACDSkill201401'
END
IF OBJECT_ID('ColTelephonyArchive.dbo.ACDSkill201501') IS NOT NULL
Begin
Set #Jan = 'ColTelephonyArchive.dbo.ACDSkill201501'
END
IF #Jan IS NULL
Begin
Set #Jan = 'Does.Not.Exist'
END
Select WorkDte, SwitchNbr, SkillNbr,StaffTimeInSec, AvailableTimeInSec, ACDCallTotCt
,AbandonCallTotCt, AbandonCall1Ct, AnswerTimeInSec, ACDTalkTimeInSec,TotAcwTimeInSec, HoldTimeInSec
FROM #Jan
If I'm getting your problem right, you need to build the dynamic query. So in your case it would be
DECLARE #qry VARCHAR(511)
SET #qry = 'Select WorkDte, SwitchNbr, SkillNbr,StaffTimeInSec, AvailableTimeInSec, ACDCallTotCt
,AbandonCallTotCt, AbandonCall1Ct, AnswerTimeInSec, ACDTalkTimeInSec,TotAcwTimeInSec, HoldTimeInSec
FROM ' + #Jan
EXEC (#qry)
I'm having a rather strange problem with MySQL. Trying to create a procedure to update some fields in the database (the code is below).
The problem is with the line that is currently commented. It seems that if no SELECT statements get executed during the procedure MySQL query browser will return an error code of "-1, error executing SQL query".
I tried the same thing in HeidiSQL and the error was "cannot return result set". So I suppose the question is do I always have to select something in the procedure, or is there some other thing I missed.
The query works fine when the comment is removed.
DELIMITER /
DROP PROCEDURE IF EXISTS updateFavourites /
CREATE PROCEDURE updateFavourites(quota INT)
BEGIN
DECLARE done INT DEFAULT 0;
DECLARE artist_id,releases INT;
DECLARE c_artist Cursor FOR
SELECT Artist.id_number,COUNT(Artist.id_number) FROM Artist
JOIN CD ON CD.is_fronted_by = Artist.id_number
GROUP BY Artist.id_number;
DECLARE CONTINUE HANDLER FOR SQLSTATE '02000'
SET done=1;
IF quota > 0 THEN
OPEN c_artist;
REPEAT
FETCH c_artist INTO artist_id,releases;
IF NOT done THEN
IF releases >= quota THEN
UPDATE CD SET CD.rating='favourite' WHERE CD.is_fronted_by = artist_id;
END IF;
END IF;
UNTIL done END REPEAT;
CLOSE c_artist;
-- SELECT 'Great success';
ELSE
SELECT CONCAT('\'quota\' must be greater than 0.',' Got (',quota,')');
END IF;
END /
DELIMITER ;
Here's the sql to create the tables and some data:
DROP TABLE IF EXISTS CD;
DROP TABLE IF EXISTS Artist;
CREATE TABLE Artist (
id_number INT UNSIGNED AUTO_INCREMENT PRIMARY KEY,
name VARCHAR(50),
);
CREATE TABLE CD (
catalog_no INTEGER UNSIGNED AUTO_INCREMENT PRIMARY KEY,
is_fronted_by INT UNSIGNED,
rating ENUM ('favourite','top draw','good','so-so','poor','rubbish'),
CONSTRAINT fk_CD_Artist FOREIGN KEY (is_fronted_by) REFERENCES Artist(id_number) ON UPDATE CASCADE
);
INSERT INTO Artist VALUES(11,'Artist 1');
INSERT INTO Artist VALUES(10,'Artist 2');
INSERT INTO CD VALUES (7,11, 'top draw');
INSERT INTO CD VALUES (650,11,'good');
INSERT INTO CD VALUES (651,11,'good');
INSERT INTO CD VALUES (11,10,'favourite');
Query Browser is not for running scripts, just single query.
I tried your code by moving cursor into each query (except DELIMITER) and pressing Ctrl+Enter.
It created that stored procedure without problem. (just refresh schema on the left).
If you wish creating procedure, use menu "Script"->"Create stored procedure/function".
But better forget about QueryBrowser it is not supported at all (and actunally not useful).
If you have decent hardware and plenty resources, try Workbench 5.2 otherwise use SQLyog
Googling around, there are several reports of the same error, but little information to solve the problem. There's even a bug logged at mysql.com but it appears to have been abandoned without being resolved.
There's another StackOverflow question on the same error, but it's also unresolved.
All it means is that there is no result set from the query. Looking at the source code, it appears that sometimes an error status of MYX_SQL_ERROR is set when the query has no result set. Perhaps this is not an appropriate consequence?
I notice that when I use the mysql command-line client, it yields no error for calling a proc that returns no result set.
update: I tried to revive that MySQL bug report, and provide a good test case for them. They changed the bug from "no feedback" to "verified" -- so at least they acknowledge it's a bug in Query Browser:
[11 Dec 9:18] Sveta Smirnova
Bill,
thank you for the feedback. Verified
as described.
Although most likely this only be
fixed when MySQL Query Browser
functionality is part of MySQL
workbench.
I guess the workaround is to ignore the -1 error, or to test your stored procedures in the command-line mysql client, where the error does not occur.
The comment supposes the issue will disappear as the Query Browser functionality becomes part of MySQL Workbench. This is supposed to happen in MySQL Workbench 5.2. I'll download this beta and give it a try.
MySQL Workbench 5.2 is in Beta, but I would assume MySQL engineering can't predict when the Beta will become GA. Those kinds of predictions are hard enough under standard conditions, but there's a lot of extra uncertainty of MySQL's fate due to the unresolved Oracle acquisition.
update: Okay, I have tried MySQL Workbench 5.2.10 beta. I executed a stored procedure like this:
CREATE PROCEDURE FooProc(doquery SMALLINT)
BEGIN
IF doquery THEN
SELECT * FROM Foo;
END IF;
END
When I CALL FooProc(0) the response is no result set, and the status is simply "OK".
When I CALL FooProc(1) the response is the result of SELECT * FROM Foo as expected.
However, there's another bug related to calling procedures. Procedures may have multiple result sets, so it's hard to know when to close the statement when you execute a CALL query. The consequence is that MySQL Workbench 5.2 doesn't close the statement, and if you try to do another query (either CALL or SELECT) it gives you an error:
Commands out of sync; you can't run this command now.
MySQL doesn't support multiple concurrent open queries. So the last one must be closed before you can start a new one. But it isn't closing the CALL query. This bug is also logged at the MySQL site.
The bug about commands out of sync has been resolved. They say it's fixed in MySQL Workbench 5.2.11.
Try putting BEGIN and END blocks around the multiple statements in the IF block as such:
IF quota > 0 THEN
BEGIN
OPEN c_artist;
REPEAT
FETCH c_artist INTO artist_id,releases;
IF NOT done THEN
IF releases >= quota THEN
UPDATE CD SET CD.rating='favourite' WHERE CD.is_fronted_by = artist_id;
END IF;
END IF;
UNTIL done END REPEAT;
CLOSE c_artist;
END;
ELSE
SELECT CONCAT('\'quota\' must be greater than 0.',' Got (',quota,')');
END IF;
Over the last couple of days I have tried to write an Stored procedure in MySQL and I have some truble getting it to work. Hope someone here can give me some input :)
The example I post is for asp.Net Membership provider to create a new user. I expect to send email and password to the DB and get an int return to verify that the userdeatils was written to the DB.
I use a MySQL DB 5.1 (I think) and write the SQL to a webinterface.
I got 2 sidequestions, can someone explain that too :):
1) I use a DELIMITER, but do not know what it does.
2) I am not sure if I have to do other things then to set autocommit = 0 to get transactions to work, or if I even have to do that.
I know that I could have used a IF / ELSE statement instead of a transaction, but would like to do it with one to find out how it works. (I expect to use it alot later)
The code I can not get to work:
DELIMITER //
CREATE DEFINER=`websharp_dk`#`%` PROCEDURE `CreateUser`(
IN _username VARCHAR(100),
IN _Password VARCHAR(100))
RETURNS INT
BEGIN
SET autocommit = 0;
DECLARE return_value INT;
BEGIN TRY
START TRANSACTION
INSERT INTO User
(Email
,Password
,Failed_Password_Count
,Creation_Date)
VALUES
(_username
,_Password
,0
,Datetime.Now())
SET return_value = 1;
COMMIT;
END TRY
BEGIN CATCH
ROLLBACK
SET return_value = 0;
END CATCH
BEGIN FINALLY
RETURN return_value;
END FINALLY
END//
DELIMITER ;
Edit:
The error message I get is:
1064 - 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 'INT BEGIN SET autocommit = 0; DECLARE return_value INT; ' at line 4
To get support for transactions, make sure you are using the InnoDB storage engine rather than the default MyISAM.
As far as that code itself, my first question would be, why are you wrapping that single query in a transaction? Also, what errors are you seeing?
The delimiter redefines what sequence of characters you use to end an sql statement. The entire create procedure is one big statement and you need to tell MySQL where it ends with something (would normally be ';'). But since you have a bunch of other statements in the "body" (between BEGIN and END) of the create procedure statement that all need to be ended too you need to redefine the delimiter so you don't end the create procedure statement at the first ';'.
Without redefining the delimiter, MySQL would think that the create procedure statement looked like this and then begin a new statement:
CREATE DEFINER=`websharp_dk`#`%` PROCEDURE `CreateUser`(
IN _username VARCHAR(100),
IN _Password VARCHAR(100))
RETURNS INT
BEGIN
SET autocommit = 0;
Using DELIMITER ; at the end of the script changes the delimiter back to ';' and is not needed although it's good practice to do so.