MySQL mylfunctions - mysql

I have created this code using MySQL within a "mylibrary" database but when I turned it in the professor said that the function had nothing to do with the database and it seemed to be missing some statements. Could some of you take a look at this code and tell me what I am missing in order for the information to work properly?
delimiter $$
adds number of titles to the qty count
create function titleCount_5(number INT,qty INT)
RETURNS INT
BEGIN
RETURN number+number;
END $$
delimiter ;
select titleCount_5(5,8);

Related

Temporary Procedure define variable issue

I'm trying out MySQL procedures for the first time, however I can't figure out how to define the variable #index_ids for the life of me. It really doesn't like the SET.
CREATE PROCEDURE #indextemp
BEGIN
SET #index_ids = (SELECT DISTINCT index_id FROM visibility_index_processing_queue WHERE process_id IS NOT NULL);
SELECT #index_ids;
END
Problem is in CREATE PROCEDURE syntax, not in setting variable. You just have to add parentheses after procedure name. Here's working sample
delimiter $
CREATE PROCEDURE indextemp()
BEGIN
SET #index_ids = (SELECT DISTINCT index_id FROM visibility_index_processing_queue WHERE process_id IS NOT NULL);
SELECT #index_ids;
END$
delimiter ;
Sometimes use of delimiter character in procedure body can cause problems too. That's why I set delimiter to $ before creating procedure and revert it to default ; after I'm done.
Also notice that I have removed # from your procedure name. In sql # is used to insert comments. If for some reason you really want to use it in your name you have to do it like that
CREATE PROCEDURE `#indextemp`()

MySQL TRIGGER to INSERT BEFORE INSERT using SELECT, IF, etc

I am trying to write a trigger that combines an insert & select, I've found numerous topics online, but, none seem to relate to my exact problem, maybe I am missing something with my structure?
The aim of this is that on the event of a cancellation in our audit log, then I define a cancellation reason based on a series of business logic in another table, this logic is drawn together in a SELECT using CASE & subqueries.
I want to expand the following trigger that currently works and replace the SET cancellation_point='test' element with the SELECT query I just mentioned.
DELIMITER $$
CREATE
TRIGGER `cancellation_stage` BEFORE INSERT ON `log`
FOR EACH ROW
BEGIN
IF (NEW.status='cancel' AND NEW.type='0') THEN
INSERT INTO cancellation_stage
SET
id=NEW.id,
property_id=NEW.entity_id,
cancellation_date=NOW(),
cancellation_point='test';
END IF;
END;
$$
DELIMITER ;
I did try to construct this myself using various guidance from here, but, its just not working. I got this code to physically save as a trigger, but, it did not populate the data in the database (I have replaced my SELECT with a basic query example below):
DELIMITER $$
CREATE
TRIGGER `cancellation_stage` BEFORE INSERT ON `log`
FOR EACH ROW
BEGIN
DECLARE cancellation_point VARCHAR(255);
SET cancellation_point = ( SELECT * FROM x);
IF (NEW.transition='cancel' AND NEW.entity_type='property') THEN
INSERT INTO cancellation_stage
SET
id=NEW.id,
property_id=NEW.entity_id,
cancellation_date=NOW(),
cancellation_point=NEW.cancellation_point;
END IF;
END;
$$
DELIMITER ;
Any help would be greatly appreciated :)

Truncated incorrect DOUBLE error in MySQL

I wrote a simple mysql procedure as follows:
DELIMITER $$
create procedure trialdbjs.spDeleteEmployees(in IDs nvarchar(1000))
Begin
Delete from employees_js Where ID IN (IDs);
end$$
it works for single values of IDs but when I call the procedure with a comma separated value such as: '2,4'
for example:
call trialdbjs.spDeleteEmployees('2,4');
This returns an error of: Truncated incorrect DOUBLE value: '2,4'
I hope you can help me!
Thanks in advance
You cannot write the stored procedure this way. The problem is IN. It is treating '2,4' as a string value and then trying to convert it to a number (because id is a number).
Here is one way to solve this:
delimiter $$
create procedure trialdbjs.spDeleteEmployees(
in in_IDs nvarchar(1000)
)
Begin
Delete from employees_js Where find_in_set(id, in_IDs) > 0;
end$$
There are other ways to solve this problem (dynamic SQL would be faster because it can make use of an index).

mysql where clause doesn't work

I am writing a stored procedure in mysql which simply returns the row with ID provided or return all table when no ID is provided.
CREATE DEFINER=`root`#`localhost` PROCEDURE `SLICE_GET`(`slice_id` int)
BEGIN
SELECT *
FROM `thesis_db`.`SLICE_INFO`
WHERE (SLICE_ID = `slice_id` OR `slice_id` IS NULL);
END
I have used the same idea in ms-sql for years yet it doesn't seem to work for mysql since no matter which ID is passed, the procedure returns entire table.
What am I missing here ?
This is a way to write procedures in mysql
DELIMITER $$
CREATE PROCEDURE `name of procedure` (x CHAR(1), D1 DATE, D2 DATE)
BEGIN
SELECT name of columns you want to display
FROM table name
WHERE SLICE_ID= x
OR SLICE_ID IS NULL;
END
$$
Note: Moreover mysql is not case sensitive means all caps or all small will not effect it.
delimiter is used to:
If you use the mysql client program to define a stored program containing semicolon characters, a problem arises.
By default, mysql itself recognizes the semicolon as a statement delimiter, so you must redefine the delimiter temporarily to cause mysql to pass the entire stored program definition to the server.

MySQL -> create alias for DATE_FORMAT function with given pattern

I just wanted to know if it was possible to do something like:
CREATE ALIAS SOLR_DATE_FORMAT FOR DATE_FORMAT(date_val,'%Y-%m-%dT%TZ')
DATE_FORMAT(date_val,'%Y-%m-%dT%TZ') exists in MySQL, but I'd like to give it another name and make it a one argument function, because in my unit tests I use another DB (H2) on which I defined such a SOLR_DATE_FORMAT function
You can create your own function:
DELIMITER $$
create function SOLR_DATE_FORMAT( date_val )
returns char(20)
begin
return DATE_FORMAT(date_val,'%Y-%m-%dT%TZ');
end$$
DELIMITER ;
EDITED Fixed returned type. Thanks eggyal!