I have created one procedure which take 2 parameter and it check the count.
But when I pass "" blank value It still return 1 count.
Did not get it why it is working like this.
Thanks for the help below is my procedure
DELIMITER $$
CREATE DEFINER=`dadclient`#`123.63.249.169` PROCEDURE `checkInOut`(IN grid varchar(50),OUT count INT)
begin
select count(GRIDID) into count from GRIDID where GRIDID=grid;
select count;
END
when I call
checkInOut("",#aaa);
select #aaa;
When I call this,it return me 1 which is wrong.
But when I pass "" blank value It still return 1 count.
Because when you say it is blank by providing an empty string it is a value. Empty string is also treated as a value in database and hence you get count as 1
The MySQL docs says:
COUNT(expr)
Returns a count of the number of non-NULL values of expr in the rows
retrieved by a SELECT statement. The result is a BIGINT value.
So if you want the count to return 0 then instead of making the string as empty "" you need to make the value as NULL.
Yo mate, try this:
DELIMITER $$
CREATE
DEFINER=`dadclient`#`123.63.249.169`
PROCEDURE `checkInOut`(
IN `grid` VARCHAR(50),
OUT `count` INT
)
BEGIN
-- insert value into variable
SET `count` = (
select count(GRIDID)
from GRIDID
where GRIDID=grid;
);
END$$
DELIMITER ;
So in your application, e.g. php you can CALL checkInOut($input, $output);. No need for the final SELECT... part in your initial code, afaik, because you already declared count as an OUT in the procedure parameter
Note:
Is the name GRIDID the name of your table? because as I see, you also used it in your WHERE statement as a key filter
Regarding your query, supposedly it is expected to contain one row of result. Kindly check that also mate
Related
I have a mysql table of employees with columns surname and workhours for each employee . I am trying to create a stored function that takes a letter as input and returns the sum of workhours of all employees whose surname starts from the input letter .This is basically an easy task but I am a complete beginner in sql and I need your help .
What I tried :
CREATE FUNCTION sumOfTotalSickHours(letter CHAR(1))
RETURNS DOUBLE DETERMINISTIC
BEGIN
DECLARE total DOUBLE DEFAULT 0
SELECT SUM(sickleavehours) INTO total
FROM employee where lastName LIKE '#letter%'
RETURN total
END
With my above code I get a syntax error since I do not know how to write the query correctly .
First of all, each statement needs to end with a semicolon (;).
Be sure you read https://dev.mysql.com/doc/refman/8.0/en/stored-programs-defining.html to understand how to use DELIMITER when defining stored programs.
Also to reference an input variable, don't put inside quotes.
And don't use #letter because variables with the # sigil are not the same variable as without the sigil.
So for example:
CREATE FUNCTION sumOfTotalSickHours(letter CHAR(1))
RETURNS DOUBLE DETERMINISTIC
BEGIN
DECLARE total DOUBLE DEFAULT 0;
SELECT SUM(sickleavehours) INTO total
FROM employee where lastName LIKE CONCAT(letter, '%');
RETURN total;
END
Much appreciated if you can have a quick look on the below.
I've created the below stored function in order to format the datetime from a table to mysql format;
(%Y-%m-%d; %h:%i:%s);
Delimiter $$
create function formatdata(dataparameter varchar(20), hourparametru
varchar(20))
begin
DECLARE x time;
declare y date;
set x = (select STR_TO_DATE(Datăcolumn,'%h:%i:%s')from tabelname);
set y = (select STR_TO_DATE(oracolumn,'%Y-%m-%d')from tabelname);
select x,y;
if current_time > time(hourcolumn) then
select STR_TO_DATE(Datăcolumn,'%Y-%m-%d')into x from tabelname;
select STR_TO_DATE(oracolumn,'%h:%i:%s') into y from tabelname;
return(x,y);
end if;
end $$
Delimiter ;
What am I doing wrong here?
The error is You have an error in your sql syntax near first select_Str_to_date.
but I am kind of confused how local variables, IN parameters work together - so I am assuming that here is where I do wrong..
If you set a variable to a result of a scalar SELECT query, you put the query inside parentheses.
This:
set x = select STR_TO_DATE('07:47:20','%h:%i:%s');
Should be:
set x = (select STR_TO_DATE('07:47:20','%h:%i:%s'));
That explains the syntax error.
You also have a few other problems in this function.
Re your comment:
The line:
select x,y;
Would return a result set, but that's not allowed in a stored function (it is allowed in a stored procedure). I think you can just remove this line.
A stored function can only return a single scalar value. You should have a line RETURNS <datatype> before your BEGIN, and you should RETURN a single value somewhere in your function.
Another problem: your assignments are not valid if the table has more than one row. Setting a variable to a scalar SELECT query result is valid only if the query returns a single column and single row.
Another problem:
if current_time > time(hourcolumn) then
What is hourcolumn? It's not a function parameter or a local variable. But if it is a column of a table, there's no syntax to say which table you mean.
Another problem:
return(x,y);
You can't return a pair of variables from a stored function. Only one variable.
Another problem: The only RETURN is inside an IF block. What should the function return if the IF condition isn't true?
I am trying to create an sql trigger statement using phpmyadmin trigger interface.
Trying to do something for table 1 as shown below :
BEGIN
declare #valid_number int ;
select id into #valid_number from table 2 ;
if 10 does not exist in #valid_number then
{do something here}
end if;
END
how to achieve it?
First: a variable in a stored routine can't store multiple values, just a single one. Your statement
select id into #valid_number from table 2 ;
will only work, if the query returns exactly one row. An error will occur, if the query returns multiple rows, a warning, if the query returns no row at all, see the manual page to SELECT ... INTO:
The INTO clause can name a list of one or more variables, which can be
user-defined variables, stored procedure or function parameters, or
stored program local variables. [...]
The selected values are assigned to the variables. The number of
variables must match the number of columns. The query should return a
single row. If the query returns no rows, a warning with error code
1329 occurs (No data), and the variable values remain unchanged. If
the query returns multiple rows, error 1172 occurs (Result consisted
of more than one row).
Solution:
It's not difficult to create a statement that gives you the desired answer in exact one row, i.e.
SELECT COUNT(*) into valid_number FROM example WHERE id = 10;
This query will return 0, if the id 10 does not exists in column id and the count of occurences else. Of course there are several ways to achieve this, this is just one of them. You could rewrite your stored routine to:
BEGIN
-- prefer local variables, don't use user defined, if not needed.
DECLARE valid_number int;
SELECT COUNT(*) into valid_number FROM example WHERE id = 10;
IF valid_number = 0 THEN
-- do something here
END IF;
SELECT result;
END
Note
You could use a cursor to traverse the result of a query, but most times one wants to avoid a cursor. To use a cursor under similar conditions as of this question would not be the SQL way to do it and most times very inefficient.
I noticed that a select statement inside my stored procedure is always returning the same value, no matter what parameter I'm calling my stored procedure with. Here's the code:
DELIMITER $$
CREATE PROCEDURE TEST(IN id INT)
BEGIN
DECLARE x INT DEFAULT 0;
select id;
SELECT paper_id
INTO x
FROM Paper
WHERE ID = id
limit 1;
select x;
END$$
x always returns the same value no matter what id I call test with. I noticed the value of x is always the value of paper_id in the first row of my Paper table.
However, if I run the same query outside of the stored procedure, I get the expected value.
What is going wrong inside the stored procedure which skews that value?
I'm not that familiar with MySQL's stored procedures, but could it be that the expression WHERE ID = id is evaluated as "all rows from Paper where the value in the column ID equals the value in the column ID" and simply ignores your parameter?
Try to rename your parameter to something that is different from the column name in your query.
I have a user defined function.
In that function I declared a variable of type datetime.
I am assigning the result of a query into this variable. And I am returning this assigned value. It looks like
delimiter$$
drop function if exists getQEDate$$
create function getQEDate() returns datetime
begin
declare qedate datetime;
select date into qedate from qenddates where ....;
return qedate;
end$$
delimiter ;
When accessing this function I am getting an exception like "returns more than a row...".
So I am thinking this error occurred while returning the result. That means the variable qedate can hold more than one row.
Is the above analysis makes sense ?
It's probably that your query is returning more than 1 row and it can't store that into a variable. If you're only expecting 1 row you should check your where clause or add LIMIT 1 to the end of the query in the function.
I'm guessing since I can't see your data or your where clause :)