Now we can call the displayevennumbers function to get the even numbers between 15 and 22 using the following query statement. Then we will use a for loop ranging from the “first” variable to the “last” variable to whom the value of the second parameter is assigned, and the step value will be 2 as all even numbers will have a difference of two between consecutive even values. If the first parameter is even, we will assign that value to the “first” variable else, we will add one to the first parameter value and then assign that value to the “first” variable. ![]() We will first check whether the starting number first is an even or odd number by using the modulus operator and if statement. The above function would give the following output if the function created successfully.Įxplanation: Here, the first parameter will be starting number and the second will be the ending number between which we want even numbers. RAISE NOTICE 'Even numbers : %', sampleCounter END LOOP Our function will then be like this:ĬREATE OR REPLACE FUNCTION displayEvenNumbers(int,int) RETURNS void AS $$ DECLAREįOR sampleCounter IN first.last BY 2 LOOP We have to retrieve all the even numbers between the two numbers that are passed to our function and print them. Let us take one example where we will take a for loop and use if statement in it. Now, let us try with a value less than 19, take 15. Now, we want to check the eligibility for a 21-year-old guy then we can call our function in the following way:Įxplanation: This displays the notice along with our message, which means the condition inside the if-statement evaluated to true and if the block’s body was executed. The output after copying and pasting the above statements on the psql command prompt terminal is as follows. We can call this function whenever we want to pass the value of the age for which you want to check eligibility. RAISE NOTICE 'You are eligible to vote as your age is %!', age END IF Įxplanation: This will create a function named iseligible. Let us write a function and try to call them by passing the age variable instead of statically declaring and initializing in the above example:ĬREATE OR REPLACE FUNCTION iseligible(int) RETURNS void AS $$ DECLARE Then the output is seen somewhat like the following :Įxplanation: No notice is raised, and the statements in the if block is simply skipped for execution. Now, let us initialize our variable to a value less than 19, for example, 15 and see that is the output. Then we begin or execution part where we will place our if statement in which we will check whether age is greater than 18 if so, the raise a notice saying, “You are eligible to vote!”. ![]() Then comes the declaration part, where we declare our variable named age and initialize it to 23 integer value. RAISE NOTICE 'You are eligible to vote!' Įxplanation: The DO statement specifies that Postgres needs to execute the following statements below it. Let us see how we can perform this execution with the help of do statement. If the age is greater than 18, then we will raise a message in the form of a notice saying that “he/she is eligible to vote”. Let us consider a simple example where we will declare one variable named age. ![]() Examples to Implement PostgreSQL IF Statement using expressions with declared variables, numbers, string comparisons, query results, and any functions that return boolean value like isnull, exists and many other. ![]() Usage: We can use this if statement in functions, stored procedures, the sequence of statements inside DO statement or with loops such as simple LOOP statement, for loop, while loop, etc. This is the syntax of if statement in psql. If the condition doesn’t evaluate to true, then the statements in the if block is simply skipped. The body of the if block starts from the THEN keyword till the END IF statement. This condition can be any expression ranging from variable comparison to query result or isNull statement or exists statement that returns a boolean value if this condition evaluates to true, then the code or statements you want to execute are specified after THEN keyword is executed. Explanation: The condition is the boolean expression that evaluates to true or false.
0 Comments
Leave a Reply. |