Follows is the code for a stored procedure in it's working state(I think). Well, it works as long as someone has actually received an item, but if not, then @i_MAX winds up being null and the whole procedure fails. I need a way to grab it, and deal with it if it is null, then exit the stored procedure. I'm quite new, so please excuse my odd programming approach.
DELIMITER $$
DROP PROCEDURE IF EXISTS `contacts`.`IsCurrent` $$
CREATE DEFINER=`remote-root`@`` PROCEDURE `IsCurrent`(i_FN varchar(30), i_LN varchar(30),
i_CO varchar(255), i_IT varchar(30) )
BEGIN
DECLARE i_SQL VARCHAR(2000);
-- @i_MAX = IssueID for last issued item
-- @i_VER = Version of last issued item
-- @i_CUR = Current version of an item
SET i_SQL = CONCAT('SELECT MAX(IssueID) INTO @i_MAX FROM `contacts`.`issueditems`
WHERE `First Name`= ''', i_FN, '''',' AND `Last Name` = ''',i_LN,'''',
'AND `Company` = ''', i_CO, '''',
'AND `Item` = ''', i_IT, '''');
SET @sql=i_SQL;
PREPARE s1 FROM @sql;
EXECUTE s1;
DEALLOCATE PREPARE s1;
SET i_SQL = CONCAT('SELECT Version INTO @i_VER FROM `issueditems` WHERE `IssueID`= ''', @i_MAX, '''');
SET @sql=i_SQL;
PREPARE s1 FROM @sql;
EXECUTE s1;
DEALLOCATE PREPARE s1;
SET i_SQL = CONCAT('SELECT CurVer INTO @i_CUR FROM `items` where item = ''',i_IT, '''');
SET @sql=i_SQL;
PREPARE s1 FROM @sql;
EXECUTE s1;
DEALLOCATE PREPARE s1;
IF @i_VER = @i_CUR THEN
SET i_SQL = CONCAT('SELECT ''', '1','''', 'AS IsCurrent, ', '''', @i_VER,'''', 'AS Version');
ELSE
SET i_SQL = CONCAT('SELECT ''', '0','''', 'AS IsCurrent, ', '''', @i_VER,'''', 'AS Version');
END IF;
SET @sql=i_SQL;
PREPARE s1 FROM @sql;
EXECUTE s1;
DEALLOCATE PREPARE s1;
END $$
DELIMITER ;
DELIMITER $$
DROP PROCEDURE IF EXISTS `contacts`.`IsCurrent` $$
CREATE DEFINER=`remote-root`@`` PROCEDURE `IsCurrent`(i_FN varchar(30), i_LN varchar(30),
i_CO varchar(255), i_IT varchar(30) )
BEGIN
DECLARE i_SQL VARCHAR(2000);
-- @i_MAX = IssueID for last issued item
-- @i_VER = Version of last issued item
-- @i_CUR = Current version of an item
SET i_SQL = CONCAT('SELECT MAX(IssueID) INTO @i_MAX FROM `contacts`.`issueditems`
WHERE `First Name`= ''', i_FN, '''',' AND `Last Name` = ''',i_LN,'''',
'AND `Company` = ''', i_CO, '''',
'AND `Item` = ''', i_IT, '''');
SET @sql=i_SQL;
PREPARE s1 FROM @sql;
EXECUTE s1;
DEALLOCATE PREPARE s1;
SET i_SQL = CONCAT('SELECT Version INTO @i_VER FROM `issueditems` WHERE `IssueID`= ''', @i_MAX, '''');
SET @sql=i_SQL;
PREPARE s1 FROM @sql;
EXECUTE s1;
DEALLOCATE PREPARE s1;
SET i_SQL = CONCAT('SELECT CurVer INTO @i_CUR FROM `items` where item = ''',i_IT, '''');
SET @sql=i_SQL;
PREPARE s1 FROM @sql;
EXECUTE s1;
DEALLOCATE PREPARE s1;
IF @i_VER = @i_CUR THEN
SET i_SQL = CONCAT('SELECT ''', '1','''', 'AS IsCurrent, ', '''', @i_VER,'''', 'AS Version');
ELSE
SET i_SQL = CONCAT('SELECT ''', '0','''', 'AS IsCurrent, ', '''', @i_VER,'''', 'AS Version');
END IF;
SET @sql=i_SQL;
PREPARE s1 FROM @sql;
EXECUTE s1;
DEALLOCATE PREPARE s1;
END $$
DELIMITER ;