MySQL Forums
Forum List  »  Knowledge Base

Problem with creating a statement based on variables
Posted by: Hans Karlsson
Date: October 24, 2009 08:42AM

I want to insert a string (YYYY-MM) in a table. The field is of type CHAR. The value in the INSERT-string is based on variables.

CREATE PROCEDURE `Test`.`TestPart2`(dteFromdat DATE)
.....
#Create the YearMonth variable
IF MONTH(dteFromDat)<10 THEN
SET txtYearMonth=CONCAT(YEAR(dteFromDat), '-0', MONTH(dteFromDat));
ELSE
SET txtYearMonth=CONCAT(YEAR(dteFromDat), '-', MONTH(dteFromDat));
END IF;

#Create the statement
SET @QUERY = CONCAT('INSERT INTO tblKorrlogg SET KorrDate=CURDATE(), YearMonth=', txtYearMonth);
PREPARE stmt FROM @QUERY;
EXECUTE stmt;
DEALLOCATE PREPARE stmt;
.......

The statement seem now to looks like this "INSERT INTO tblKorrlogg SET KorrDate=CURDATE(), YearMonth=2009-05"

My problem is how ho add the apostrophes round 2009-05 so it looks like '2009-05' and MySQL will recognize the value as a string.


/Hans Karlsson

Options: ReplyQuote


Subject
Views
Written By
Posted
Problem with creating a statement based on variables
2620
October 24, 2009 08:42AM


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.