That provides the following output after execution – Now, we will retrieve the name of the developer and the total months that they have worked in the company by using the following query statement in which we have to give a call to the getMonths() function – SELECT name, getMonths(joining_date) as NumberOfMonths FROM developers Let us select records by firing the same above SELECT query statement that gives us the following output – select * from developers The execution of the above function gives the following output – Our function will be as follows – DELIMITER $$ĬREATE FUNCTION getMonths(sampledate date) RETURNS int DETERMINISTIC Let us consider another example that involves returning the months between the current and supplied dates. Hence, if the block wasn’t executed and execution went to the else block, returning the value “No” as the guy was not eligible to vote. Let us check the eligibility of a 10-year-old guy by executing the following command – SELECT is eligible(10) We can see that the output of isEligible(20) function execution returns the “Yes” as output because it is eligible to vote. Then we can call our function in the following way – SELECT isEligible(20) Now, we want to check the eligibility for a 20-year-old guy. The output after copying and pasting the above statements on the MySQL command prompt terminal is as follows – We can call this function whenever we want to pass the age value you wish to check eligibility. This will create a function named is eligible. Let us write a function and try to call them bypassing the age variable instead of statically declaring and initializing in the above example – DELIMITER $$ This part of the function can also include calls to other functions. By default, MySQL considers the function NONDETERMINISTIC when none of the function types is mentioned.Ĭode of statements to be executed – We can write our program or code in this section of function that can contain conditional, looping statements, initializing and assigning the value of variables, and preparing and executing the database queries. However, if the function returns a different value for the same values of functions, then we can call that function to be nondeterministic. We consider a function deterministic when it returns the same value for the same parameter values. DETERMINISTIC – The function can be either deterministic or nondeterministic, which must be specified here. Once MySQL finds the RETURN statement while executing the function, the execution of the function is terminated, and the value is returned. The type of value that will be returned needs to be specified after the RETURN clause.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |