Teradata stored procedure result output - teradata-sql-assistant

How I can print var2?
REPLACE PROCEDURE Name(out var2, in var1)
---some code goes here
BEGIN
TRANSACTION;
SET var2= var1+ 5;
end;
REPLACE PROCEDURE Name(out var2, 5)

Related

How to check output of function or variable for NULL

I am bit confused with Mysql syntax. I want to check for NULL the value of ExtractValue(xml, '//order[1]/quantity[$#i]') function. It can be assign to variable or this action can be skipped. I tried this and there is syntax error:
DELIMITER $$
DROP PROCEDURE IF EXISTS `sp_test_for_null`$$
CREATE PROCEDURE `sp_test_for_null`()
BEGIN
DECLARE xml VARCHAR(1000);
SET xml = '';
DECLARE test VARCHAR(1000);
SET test = (SELECT ExtractValue(xml, '//order[1]/quantity[$#i]');
IF (test IS NULL) THEN SELECT 1; END IF;
END$$
DELIMITER ;
CALL sp_test_for_null;
I'd try this (note that all DECLAREs are at the beginning:
DELIMITER $$
DROP PROCEDURE IF EXISTS `sp_test_for_null`$$
CREATE PROCEDURE `sp_test_for_null`()
BEGIN
DECLARE xml VARCHAR(1000);
DECLARE test VARCHAR(1000);
SET xml = '';
SET test = (SELECT ExtractValue(xml, '//order[1]/quantity[$#i]');
SELECT ISNULL(test, 1, 0);
END$$
DELIMITER ;
CALL sp_test_for_null;

An error in MySQL procedure

Why the stored procedure can't be created?
delimiter //
CREATE PROCEDURE p()
BEGIN
DECLARE j INT;
SET j = 1;
SELECT j:=j+1, request.* FROM request;
END//
The problem is in line:
SET j:=j+1, ...
You need to add a # to the variable name:
delimiter //
CREATE PROCEDURE p()
BEGIN
SET #j = 1;
SELECT #j:=#j+1, request.* FROM request;
END//
Here's an explanation: MySQL: #variable vs. variable. Whats the difference?

multiple mysql stored procedure in a single call

I have a stored procedure like this and it's working fine:
$drop = $mysqli->query("DROP PROCEDURE IF EXISTS changegroup");
$initiate = $mysqli->query("
Create Procedure changegroup(IN param1 int(10),IN param2 int(10))
BEGIN
UPDATE t_parts SET part_group_id = param2 WHERE part_id = param1;
END;
");
$result = $mysqli->query("CALL changegroup($p1,$p2);");
My question is, can we put two SQL statements in a single procedure and execute the second procedure based on first like this:
BEGIN
SELECT * FROM ........
/**fetch the result of this mysql statment and if matches certain conditions,then execute the update statment***/
UPDATE t_parts SET part_group_id = param2 WHERE part_id = param1;
END;
In your stored procedure write
if <condition> then
your code
end if;
So your code should be like this
Create Procedure changegroup(IN param1 int(10),IN param2 int(10))
BEGIN
DECLARE totalcount Integer default 0;
SELECT count(*) into totalcount FROM yourtable where <condition>;
/**fetch the result of this mysql statment and if matches certain conditions,then execute the update statment***/
if(totalcount > 0) then
UPDATE t_parts SET part_group_id = param2 WHERE part_id = param1;
end if;
END;

How to return a resultset from StoredProcedure using MySql?

I am writing a stored procedure using MySql which returns multiple rows using select statement.
My code is as below
drop procedure if exists GetAccounts;
DELIMITER //
CREATE PROCEDURE GetAccounts()
BEGIN
DECLARE rowcount int;
SET #resultset = (SELECT * from requests where STATUS = "FAILURE" ;
END //
DELIMITER
Any examples of how to return a resultSet in storedProcedure?
Thanks
Gendaful
DROP PROCEDURE IF EXISTS GetAccounts;
DELIMITER //
CREATE PROCEDURE GetAccounts()
BEGIN
DECLARE rowcount int;
SELECT * from requests where STATUS = "FAILURE" ;
END //
DELIMITER ;

MySQL Stored Procedure in PHPMYADMIN -- nothing happens

I created this test procedure :
DELIMITER //
CREATE PROCEDURE `str` (IN var1 INT)
BEGIN
WHILE var1 < 5 DO
SELECT var1;
SET var1 = var1 + 1;
END WHILE;
END //
DELIMITER ;
when I run it via phpmyadmin, nothing happens. no error, no confirm message. If I CALL str(1), I get a message saying the procedure doesn't exist. What's wrong here?
If you want to get the iterated value of var1 you need the SELECT outside of the WHILE loop. The following works fine in my MySQL install:
DROP PROCEDURE IF EXISTS `str`;
DELIMITER //
CREATE PROCEDURE `str` (IN var1 INT)
BEGIN
WHILE var1 < 5 DO
SET var1 = var1 + 1;
END WHILE;
SELECT var1;
END //
DELIMITER ;
Executing CALL str(1) returns 5