title: Creating Dynamic Query in Stored Procedure MySQL
body: <p><strong>I have a Table and Rows in table like below</strong></p>
  
  <pre><code>CREATE TABLE Areas(AreaName VARCHAR(255),
                      PinCode VARCHAR(255))
  
  INSERT INTO Areas(AreaName, PinCode)
        VALUES('Teynampet', '6000018'), 
              ('Ramapuram', '6000089'),
              ('TNagar', '6000017'), 
              ('Mylapore', '6000014'), 
              ('Gopalapuram', '6000087')
  </code></pre>
  
  <p><strong>I Wrote a SQL Procedure as Below</strong></p>
  
  <pre><code>DROP PROCEDURE IF EXISTS mp_test;
  CREATE PROCEDURE mp_test(IN pArea VARCHAR(255))
     BEGIN
        SET @Query = 'SELECT PinCode FROM Areas';
  
        IF pArea != ''
        THEN
           SET @City = CONCAT(' WHERE AreaName = ', pArea);
        END IF;
  
        SET @Query = CONCAT(@Query, @City);
  
   PREPARE stmt FROM @Query;
   EXECUTE stmt;
  END 
  </code></pre>
  
  <p><strong>When I Call the Procedure</strong></p>
  
  <pre><code>CALL mp_test('Teynampet');
  </code></pre>
  
  <p>When i Execute i am Not getting the Desired result i.e is 600018</p>
  
  <p>How can i build query dynamically in SP </p>
  
  <p>Thanks for the Help </p>