MySQL Forums
Forum List  »  Stored Procedures

Re: StoreProc to dynamically set table name
Posted by: Peter Brawley
Date: January 03, 2012 12:49PM

Dynamic SQL needs PREPARE. Here is a version of your sproc that runs, though it returns an empty set, so an input param or the logic is wrong.

DROP PROCEDURE IF EXISTS getTCs_cts;
DELIMITER //
CREATE PROCEDURE getTCs_cts( IN prj VARCHAR(100),IN pcts VARCHAR(100) )
BEGIN
  DECLARE tpTBName, tcTBName VARCHAR(20);
  SET @tpID=6;    -- PREPAREd stmt needs a user var
  Select TestPlanTbName from Project where ProjectName = prj into tpTBName;
  Select TestCaseTbName from Project where ProjectName = prj into tcTBName;
  -- PREPARE 
  SET @sql = Concat( 'Select TP_ID from  ', tpTBName, ' where CTS=', Char(39), pcts, Char(39), ' into @tpID ' );
  SELECT @sql;
  PREPARE stmt FROM @sql;
  EXECUTE stmt;
  DROP PREPARE stmt;
  SET @sql = Concat( 'Select * from ', tcTBName, ' where tp_id=', @tpID );
  SELECT @sql;
  PREPARE stmt FROM @sql;
  EXECUTE stmt;
  DROP PREPARE stmt;
END //
DELIMITER ;

PB

Options: ReplyQuote


Subject
Views
Written By
Posted
Re: StoreProc to dynamically set table name
1387
January 03, 2012 12:49PM


Sorry, you can't reply to this topic. It has been closed.

Content reproduced on this site is the property of the respective copyright holders. It is not reviewed in advance by Oracle and does not necessarily represent the opinion of Oracle or any other party.