I am working for a little film database with php and mysql. And i got a problem when I want to create a stored procedure, that the local variable don't work in the case i want to use it.
delimiter //
### showFilm###
create procedure showFilm( in id INTEGER )
BEGIN
DECLARE tmp_Value varchar(50);
IF ( id = 0) THEN SET tmp_Value = "";
ELSE SET tmp_Value = concat('where = ', id);
END IF;
select f.Titel, f.Jahr, f.Bewertung, f.altersfreigabe, f.Beschreibung, f.Trailer, f.Dateipfad, f.Titelbild, f.Speichermedium, r.Vorname, r.Nachname, r.Land, r.Bild
from film as f
inner join regisseur as r
on r.idRegisseur = f.idFilm tmp_Value;
END;
//
Related
I'm trying to run this code on MySQL WorkBench:
DELIMITER $$
CREATE PROCEDURE lavoratore_superficie(IN _superficie CHAR(5),
OUT codiciFiscali VARCHAR(255))
BEGIN
DECLARE finito INTEGER DEFAULT 0;
DECLARE codiceFiscale VARCHAR(255) DEFAULT '';
DECLARE cursoreCodici CURSOR FOR
SELECT distinct(TL.Capocantiere)
FROM superficie SU INNER JOIN lottomateriale LM on SU.Codice = LM.Superficie INNER JOIN lavoro LA ON LM.Lavoro = LA.Codice INNER JOIN divisionelavoro DT ON DT.Lavoro = LA.Codice
INNER JOIN turno TU ON TU.Codice = DT.Turno INNER JOIN turnolavoro TL ON TL.Turno = DT.Turno
WHERE SU.Codice = _superficie;
DECLARE CONTINUE HANDLER
FOR NOT FOUND SET finito = 1;
OPEN cursoreCodici;
preleva: LOOP
FETCH cursoreCodici INTO codiceFiscale;
IF finito = 1 THEN
LEAVE preleva;
END IF;
SET codiciFiscali = CONCAT(codiceFiscale, ';', codiciFiscali);
END LOOP preleva;
CLOSE cursoreCodici;
END $$
DELIMITER ;
Then I try to visualize the result set of my query into a local variable with the CONCAT function:
SET #codiciFiscali = '';
CALL lavoratore_superficie('SU001', #codiciFiscali);
SELECT #codiciFiscali;
But after the CALL if I select #codiciFiscali the result set is NULL.
Since codiciFiscali is declared as an OUT parameter, the initial value of #codiciFiscali is not being passed to the procedure. So the variable defaults to NULL.
Either add
SET codiciFiscali = '';
before the loop, or change the parameter to INOUT so it will concatenate to the caller's value.
I was getting an error of "Delimiter" is not valid at this position, expecting CREATE" as I was writing a stored procedure and couldn't figure out the cause. I think it might be an issue with MySQL workbench possibly, because the following code gives the same error but was copied straight off of this website.
DELIMITER $$
CREATE PROCEDURE GetTotalOrder()
BEGIN
DECLARE totalOrder INT DEFAULT 0;
SELECT COUNT(*)
INTO totalOrder
FROM orders;
SELECT totalOrder;
END$$
DELIMITER ;
Edit: My real stored procedure is:
DELIMITER //
CREATE PROCEDURE GetSimilar(inputdate char(10))
BEGIN
Declare id(tinyint) DEFAULT 0;
Set id := (select t.IdTimelineinfo
From timelineinfo t
WHERE t.Date = inputdate);
SELECT t.Date From timelineinfo t where t.date = inputdate;
SELECT o.Name, o.Race, o.Sex, o.IdOfficer
FROM timelineinfo
JOIN timelineinfo_officer ON timelineinfo.IdTimelineinfo = timelineinfo_officer.IdTimelineinfo
JOIN officers o ON timelineinfo_officer.IdOfficer = o.IdOfficer
WHERE timelineinfo.IdTimelineinfo = id
UNION
SELECT s.IdSubject, s.Name, s.Race, s.Sex
FROM timelineinfo
JOIN timelineinfo_subject ON timelineinfo.IdTimelineinfo = timelineinfo_subject.IdTimelineinfo
JOIN subjects s ON timelineinfo_subject.IdSubject = s.IdSubject
WHERE timelineinfo.IdTimelineinfo = id;
UNION
Select *
From media m
Where (m.IdTimelineinfo = id);
END //
DELIMITER ;
Watch out where you edit the procedure SQL code. There's a dedicated routine object editor (just like there are for tables, triggers, views etc.), which only accept SQL code for their associated object type. Hence they don't need a delimiter and even signal an error if you use one.
On the other hand you can always directly edit SQL code in the SQL IDE code editors, where no such special handling is implemented. In this case you need the delimiter.
i've been using pl/sql for a while.. And i'm bit confused about the variables in mysql procedure thing.
I'm trying to create a procedure, and I can't get it to compile :(
Error i got:
Heres the full code
CREATE DEFINER = CURRENT_USER PROCEDURE `NewProc`()
BEGIN
SET #city = FLOOR(RAND() * 5);
SET #uid = 3948;
SET #alive = (select alive from users where id = #uid);
SET #thread_id = 2950;
set #post_content = "AAAAOOOAAA! Jeg er i ";
if (#alive = 1) THEN
# Move NPC over to a random city
UPDATE users set city = #city where id = #uid;
# post a post on the forum thread
INSERT INTO forum_posts (
author,
thread,
creationdate,
content,
) values (
#uid,
#thread_id,
UNIX_TIMESTAMP(),
#post_content
);
END IF;
END;;
Anyone of you geniusens that see what I've done wrong :)?
I'm fairly new to MySQL but I'd like to create a function to validate a JSON objects that are stored in my database tables.
I looked up information on creating a function, but must be missing something as I can't seem to get it to work. It doesn't seem like it would be overly complicated but perhaps I'm not using the appropriate syntax.
Here is my code:
DELIMITER //
CREATE FUNCTION CHECKJSON( DB_NAME varchar(255), TABLE_NAME varchar(255), JSON_COLUMN varchar(255))
RETURNS varchar(300)
BEGIN
DECLARE notNullCount int;
DECLARE validJSONCount int;
DECLARE result varchar(300);
SET notNullCount = (SELECT count(*) FROM DB_NAME.TABLE_NAME WHERE JSON_COLUMN IS NOT NULL);
set validJSONCount = (SELECT count(*) FROM DB_NAME.TABLE_NAME WHERE JSON_VALID(JSON_COLUMN) > 0);
CASE
WHEN (validJSONCount = notNullCount) THEN
SET result = CONCAT('VALID JSON COUNT: ', validJSONCount)
ELSE
SET result = CONCAT('INVALID JSON COUNT: ', (notNullCount - validJSONCount))
END;
RETURN result;
END //
DELIMITER ;
When I try to run this code, I get the following error message:
"Error Code: 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 'ELSE SET result = CONCAT('INVALID JSON COUNT: ', (notNullCount - validJSONC' at line 14"
Any thoughts on how I might improve this code? Thanks!
Since MySQL 5.7 you have a pretty and simple function for this:
JSON_VALID(value)
Returns 0 or 1 to indicate whether a value is valid JSON. Returns NULL if the argument is NULL.
https://dev.mysql.com/doc/refman/5.7/en/json-attribute-functions.html#function_json-valid
You're missing a couple of ; and to end the case it should be END CASE.
DELIMITER //
CREATE FUNCTION CHECKJSON( DB_NAME varchar(255), TABLE_NAME varchar(255), JSON_COLUMN varchar(255))
RETURNS varchar(300)
BEGIN
DECLARE notNullCount int;
DECLARE validJSONCount int;
DECLARE result varchar(300);
SET notNullCount = (SELECT count(*) FROM DB_NAME.TABLE_NAME WHERE JSON_COLUMN IS NOT NULL);
set validJSONCount = (SELECT count(*) FROM DB_NAME.TABLE_NAME WHERE JSON_VALID(JSON_COLUMN) > 0);
CASE
WHEN (validJSONCount = notNullCount) THEN
SET result = CONCAT('VALID JSON COUNT: ', validJSONCount) ;
ELSE
SET result = CONCAT('INVALID JSON COUNT: ', (notNullCount - validJSONCount)) ;
END CASE;
RETURN result;
END //
DELIMITER ;
I'm new to SQL programming and I decided to make a script. This one might be quite riddled with errors but I'm getting an error that I'm unable to resolve.
DELIMITER $
DROP FUNCTION IF EXISTS crossref$
CREATE FUNCTION crossref()
BEGIN
DECLARE i INT;
DECLARE names VARCHAR(70);
SET i = 1;
myloop: LOOP
SET i=i+1;
IF i = 6 then
LEAVE myloop;
END IF;
SET names = (SELECT NAME FROM cbase_excel_table WHERE ID = i);
INSERT INTO cbase_master(NAME, PERMALINK, HOMEPAGE_URL, CATEGORY_LIST, MARKET, FUNDING, 'STATUS', COUNTRY, REGION, CITY)
SELECT NAME, PERMALINK, HOMEPAGE_URL, CATEGORY_LIST, MARKET, FUNDING, 'STATUS', COUNTRY, REGION, CITY FROM cbase_excel_table WHERE ID = i;
UPDATE cbase_master
SET DESCRIPTION = (SELECT DESCRIPTION FROM cbase_json_table WHERE NAME = names)
SET DOMAIN = (SELECT DOMAIN FROM cbase_json_table WHERE NAME = names)
SET IMAGE_URL = (SELECT IMAGE_URL FROM cbase_json_table WHERE NAME = names)
SET FACEBOOK_URL = (SELECT FACEBOOK_URL FROM cbase_json_table WHERE NAME = names)
SET TWITTER_URL = (SELECT TWITTER_URL FROM cbase_json_table WHERE NAME = names)
SET LINKEDIN_URL = (SELECT LINKEDIN_URL FROM cbase_json_table WHERE NAME = names)
SET CBASE_UUID = (SELECT CBASE_UUID FROM cbase_json_table WHERE NAME = names);
END LOOP myloop;
END$
DELIMITER;
and I'm getting:
#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 'BEGIN
DECLARE i INT;
DECLARE names VARCHAR(70);
SET i = 1;
Any help?
An example of a function which shows a major difference to your function:
CREATE FUNCTION `fnFindMaximum`(`a` INT, `b` INT)
/* Before the BEGIN statement there are other things going on - the most important being the return type statement */
RETURNS int(11)
LANGUAGE SQL
DETERMINISTIC
CONTAINS SQL
SQL SECURITY DEFINER
COMMENT ''
BEGIN
DECLARE returnval INTEGER;
IF a >= b THEN
SET returnval = a;
ELSE
SET returnval = b;
END IF;
RETURN returnval;
END
Your function then goes on to manipulate sql but does not return a value so, as was pointed out by #arkhil, use a StoredProcedure in preference to a function.