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!
Related
Is it possible to use a procedure inside a function? For example, I would like to gather all my rows related to an id but I would also like to count the rows and use it in a select statement. This is not working:
drop procedure if exists relatives;
create procedure relatives(in parent int(11),out counted int(11))
begin
set counted=(select count(*) from category where related=parent);
end;
drop function if exists relatives_count;
create function relatives_count(parent parent(11)) returns int(11)
begin
declare count int(11);
call relatives(parent,counted);
return counted;
end;
So that I can use the count
select relatives_count(id) from category
This is just for curiosity purposes. It may look senseless since I can just call a single select query and get the same results but I want to know how I can use my procedure out variable in a function.
Yes, a MySQL FUNCTION can call a MySQL PROCEDURE.
But... the operations the procedure performs will be limited to the operations allowed by a function. (We can't use a procedure to workaround the limitations placed on a function.)
"is not working" is so nebulously vague as to be practically useless in debugging the issue. What exact behavior is being observed?
My suspicion is that the SQL statements shown are failing, because there is no override for the default statement delimiter.
Also, parent(11) is not a valid datatype.
Be aware that when an identifier for a column in a SQL statement in a MySQL stored program matches an identifier used for an argument or local variable, MySQL follows a rule about which (the column name or the variable) that is being referenced.
Best practice is to adopt a naming convention for arguments and local variables that do not match column names, and to qualify all column references with a table name or table alias.
Personally, I use a prefix for arguments and local variables (a for argument, l for local, followed by a datatype i for integer, d for date/datetime, n for decimal, ...
DELIMITER $$
DROP PROCEDURE IF EXISTS relatives$$
CREATE PROCEDURE relatives(IN ai_parent INT(11),OUT ai_counted INT(11))
BEGIN
SELECT COUNT(*)
INTO ai_counted
FROM category c
WHERE c.related = ai_parent
;
END$$
DROP FUNCTION IF EXISTS relatives_count$$
CREATE FUNCTION relatives_count(ai_parent INT(11))
RETURNS INT(11)
BEGIN
DECLARE li_counted INT(11);
CALL relatives(ai_parent,li_counted);
RETURN li_counted;
END$$
DELIMITER ;
Please identify the exact behavior you observe. Error message when creating the procedure? Error message when executing the function? Unexpected behavior. That's much more precise and informative than telling us something "is not working".
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.
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);
In this query using to_number() in oracle. How to write compatibility query for oracle and mysql databases.
SELECT col1 FROM table WHERE condition ORDER BY TO_NUMBER(col2);
Here col2 is varchar2 datatype. Suppose i was used ORDER BY command in this query must use converting function i.e to_number(col2),this function not available in mysql.so please give correct solution for above problem
Create a custom function in your mysql db with name to_number which takes same parameter and returns integer .
You can then use cast function inside your custom function
DELIMITER $$
DROP FUNCTION IF EXISTS to_number$$
CREATE FUNCTION to_number (number VARCHAR(10)) RETURNS INT (11)
BEGIN
RETURN (CAST(number AS SIGNED));
END$$
DELIMITER ;
This will create a custom/userdefined function with to_number as name
Then you can use your query both in oracle and mysql
You can do it with CAST
select id,num type, details,CAST(num AS SIGNED) as T
from demo order by T
SQL Fiddle link
For more info : http://dev.mysql.com/doc/refman/5.0/en/cast-functions.html
A similar question about sql-server has been asked here. I'm wondering if its possible in MySql.
edit:
I want to use result set returned from procedure in view.
If you want to get result-set and use routine in FROM clause - NO. Stored routines (procedures or functions) in MySQL cannot return tables as result value.
But you can use functions as simple values, for example -
DELIMITER $$
CREATE FUNCTION mul10(Param1 INT)
RETURNS INT(11)
BEGIN
RETURN Param1 * 10;
END
$$
DELIMITER ;
CREATE OR REPLACE VIEW view1
AS
SELECT mul10(2) AS column1;
SELECT column1 FROM view1;
----------
20