Is there another way to check if variables are equal? When I use this code I didn't have any result - plsqldeveloper

When I check if variables are equal, I don't have any result. When I run the procedure, is there any way to check the value of variables?
procedure c is
i_n number:=0;
o_n number:=0;
c_t number;
cursor c_values is
SELECT
a.file_type AS file_type,a.input AS a_input,b.input AS b_input,a.output AS a_output,b.output AS b_output,
CASE WHEN a.input = b.input then
i_n+1
WHEN a.output = b.output THEN
o_n+1
End case
FROM
test1 a, test4 b
where a.file_type=b.file_type
;
begin
select count(*) into c_t from test1;
if i_n=c_t and o_n=c_t then
dbms_output.put_line('done');
end if;
end;`
end;

Related

MySql stored procedure rows issue

I have this stored procedure:
CREATE DEFINER=`admin`#`%` PROCEDURE `GetTickets4Card`(
IN p_TicketID int,
OUT p_returnvalue int
)
BEGIN
SELECT idbookingstickets
INTO #p_returnvalue
FROM bookingstickets
WHERE TicketId = p_TicketID;
/* Return value accordingly */
IF mysqll_affected_rows = 0 THEN SET p_returnvalue = 0;
/*
ELSE
SELECT * FROM BookingsTicketsCollected WHERE p_returnalue = idtickets;
if mysqll_affected_rows = 0 THEN SET p_returnvalue = -1;
END IF;
*/
END IF;
END
It gives me the following error: "Result consisted of more than one row". It may have something to do with mysql_affected_rows , but I have no idea, I want to know if the sql statement returns 1 row or not, any ideas?
Call code:
set #p_returnvalue = 0;
call yourTICKETbox_LIVE_DB.GetTickets4Card("aabb188e-6adc-11e5-9770-061de6653ea3", #p_returnvalue);
select #p_returnvalue;
When you use SELECT ... INTO variable, the query must return at most one row. If you only care whether there are any matching rows, you can use the EXISTS() function.
SET p_returnvalue = EXISTS(
SELECT 1
FROM bookingstickets
WHERE TicketId = p_TicketID);
BTW, the MySQL equivalent to the PHP function mysqli_affected_rows() is ROW_COUNT().

Single Conditional Check Affecting Multiple (or All) Columns Inside a Single Row

What I'd like to accomplish, is affect multiple columns of all rows where e.g. column1 > 5 to have their values altered or replaced.
Is there any elegant way of performing this conditional check once (assuming it is a complex statement) and using its outcome for the columns using CASE / WHEN?
I understand that I can perform standard filtering using WHERE column1 > 5 and UNION of sorts, but I'd like to learn if there are any other options (maybe even MySQL specific) available.
Edit:
As an example:
SELECT
CASE
WHEN Country = 'UK' THEN CustomerId
ELSE '???'
END AS "CustomerId",
CASE
WHEN Country = 'UK' THEN City
ELSE '???'
END AS "City",
CASE
WHEN Country = 'UK' THEN Country
ELSE '???'
END AS "Country"
FROM Customers
As you can see, I'm using CASE WHEN Country = 'UK' multiple times here which I'd like to avoid.
You can use union all:
select c.customerid, c.city, c.country
from customers c
where country = 'UK'
union all
select '???', '???', '???'
from customers c
where country is null or country <> 'UK'
I think the elegant solution here is to have every column affected by column1 it's own transformation function;
DELIMITER $$
CREATE FUNCTION fn_transformation_for_column2(var_column1 INT)
RETURNS INT
BEGIN
IF (fn_condition(var_column1)) THEN
RETURN 1;
ELSE
RETURN 5;
END IF;
END;
DELIMITER ;
DELIMITER $$
CREATE FUNCTION fn_transformation_for_column3(var_column1 INT)
RETURNS INT
BEGIN
IF (fn_condition(var_column1)) THEN
RETURN 14;
ELSE
RETURN 51;
END IF;
END;
DELIMITER ;
based on the shared condition (which again can be a function):
DELIMITER $$
CREATE FUNCTION fn_condition(var_column1 INT)
RETURNS BOOLEAN
BEGIN
IF (var_column1 > 5) THEN
RETURN TRUE;
ELSE
RETURN FALSE;
END IF;
END;
DELIMITER ;
Finally:
SELECT
fn_transformation_for_column2(t.column1) NewColumn2,
fn_transformation_for_column3(t.column1) NewColumn3,
...
FROM table t
;
Another would be to have the same transformation function for all the columns, and have the transormation handle it based on the given columntype parameter:
DELIMITER $$
CREATE FUNCTION fn_transformation_for_columns(var_column1 INT, var_column_no INT)
RETURNS INT
BEGIN
IF (var_column1 > 5) THEN
CASE var_column_no
WHEN 2 THEN
RETURN 1;
WHEN 3 THEN
RETURN 14;
ELSE
RETURN 0;
END CASE;
ELSE
CASE var_column_no
WHEN 2 THEN
RETURN 5;
WHEN 3 THEN
RETURN 51;
ELSE
RETURN 0;
END CASE;
END IF;
END;
DELIMITER ;
and then:
SELECT
fn_transformation_for_columns(t.column1, 2) NewColumn2,
fn_transformation_for_columns(t.column1, 3) NewColumn3,
...
FROM table t
;

How come this codes error? SQL Error code 1109: Unknown table 'o' in field list

When i run this code, it gives me error. From this code, there's several task to do:
(1) update tble customer by setting the address to '90 TYT' if c_id= 1
(2) view order_no,status,c_id,item_total remarks.
(3) if item_total 0, then update table order_status by setting remarks = 'UNAVAILABLE', else select order_no,status,item_total,remarks where status = 'waiting'.
Please help me fix the error. I'm new to SQL.
#drop procedure if exists usp_GetAnything;
delimiter //
create procedure usp_GetAnything()
begin
select c_id,lname,address,city
from customer;
update customer
set address = '90 TYT'
where c_id = 1;
select o.order_no,o.o_status,c.c_id,o.item_total,o.remarks
from customer c, order_status o
where c.c_id=o.c_id;
if (o.item_total > 0) then
update order_status o
set remarks = 'UNAVAILABLE'
where order_no > '123';
else
select order_no,o_status,item_total,remarks
from order_status
where o_status = 'waiting';
end if;
end
It's failing on the line:
if (o.item_total > 0)
o is unidentified outside of the previous select clause including all the selected variables.
In order to use the results that returned from the previous select you should select ... INTO...
(select the result arguments into declared local variables).
You can find here the following example:
DELIMITER //
CREATE PROCEDURE `proc_WHILE` (IN param1 INT)
BEGIN
DECLARE variable1, variable2 INT;
SET variable1 = 0;
WHILE variable1 < param1 DO
INSERT INTO table1 VALUES (param1);
SELECT COUNT(*) INTO variable2 FROM table1;
SET variable1 = variable1 + 1;
END WHILE;
END //
You can see that variable1 and variable2 are declared in the beginning of the procedure and later on used with select ... INTO ....

IF and Case statements in MySQL

I would like to select * if 5 is greater than 2, if false select a particular column. Where am I going wrong?
SELECT IF(5>2, *, column_x),
CASE whereheard_name WHEN 'Newspaper' THEN 'a'
WHEN 'TV' THEN 'b'
WHEN 'Internet' THEN 'c'
ELSE '-'
END
AS result
FROM whereheard;
Thanks for the answers to the above. Here is the following example stored procedure I am using:
DELIMITER $$
USE `registration`$$
DROP PROCEDURE IF EXISTS `test2`$$
CREATE DEFINER=`root`#`localhost` PROCEDURE `test2`()
BEGIN
IF(5>2) THEN
SELECT * FROM whereheard;
ELSE
SELECT whereheard_name FROM whereheard;
END IF;
END$$
DELIMITER ;
This is how I have called it:
CALL test2(),
CASE whereheard_name WHEN 'Newspaper' THEN 'a'
WHEN 'TV' THEN 'b'
WHEN 'Internet' THEN 'c'
ELSE '-'
END
AS result
FROM whereheard;
Where am I going wrong with this?
You cannot do it in SQL (I mean particular statement cannot have dynamic number of columns). You can write stored procedure that does the job though :
CREATE PROCEDURE FOO .....
.....
IF (5>2) THEN
SELECT * FROM ...
ELSE
SELECT column1 FROM ....
END IF;

How can I get this stored procedure to execute a CALL when the condition is not equal?

I have a quick question about MySql. I have this simple store procedure, that I would like to make a CALL to two store procedures when the condition is met.
The variable #SDOuser is assigned the User ID when the access level is 10 and the timestamp evaluates to the login time of between now and 2 seconds ago. Once I have this set, I then want to use it as the condition for my IF block. Here lies the issue, it works fine when the first block stays TRUE and nothing follows. But, I need the Users who have access level that is less than 10 and it does not seem to like the negating the condition to get to that.
Here is the code:
DROP PROCEDURE `getPositionGrid`//
CREATE DEFINER=<some name> PROCEDURE `getPositionGrid`()
BEGIN
set #SDOuser = (SELECT DISTINCT(uu.uu_id) FROM tbl_logins ul
INNER JOIN tbl_users uu
ON ul.ul_username = uu.uu_email
inner join tbl_access_level al
on al.al_id = uu.al_id
inner join tbl_positions up
on up.up_id = uu.up_id
inner join tbl_departments ud
on up.ud_id = ud.ud_id
WHERE ul.ul_created BETWEEN NOW() - INTERVAL 2 SECOND AND NOW());
set #CurrUser = (select uu_id from tbl_users where al_id = 10 AND uu_id = #SDOuser);
set #CurrUser1 = (select uu_id from tbl_users where al_id < 10 AND uu_id != #SDOuser);
IF(#CurrUser) THEN
BEGIN
CALL getPositionGridSDO();
END;
END IF;
IF(#CurrUser1) THEN
BEGIN
CALL getPositionGridALL();
END;
END IF;
END
//
Try this:
IF (#CurrUser IS NOT NULL) THEN
BEGIN
CALL getPositionGridSDO();
END;
END IF;
IF(#CurrUser1 IS NOT NULL) THEN
BEGIN
CALL getPositionGridALL();
END;
END IF;
Alternatively:
IF (#CurrUser > 0) THEN
...
END IF;
IF(#CurrUser1 > 0) THEN
...
END IF;
Hope it helps!