Search code examples
mysqlsqlsyntaxinsertvarchar

How to do a SQL insert inside of varchar in mysql


How I can do a insert of sql syntax inside of varchar in mysql?

example:

INSERT INTO vartemp (SQLSyntax)
VALUES ('UPDATE RegExample SET Field1 = 'abc', Field2 = 123 WHERE whereCond1 = 'abc'')

where:

Vartemp is my table of SQL Syntax.

RegExample is a example of table.


Solution

  • Is this what you want?

    INSERT INTO vartemp (SQLSyntax)
        VALUES ('UPDATE RegExample SET Field1 = ''abc'', Field2 = 123 WHERE whereCond1 = ''abc''')
    

    If so, all you need to do is properly escape the embedded single quotes in the string. The escape method for a single quote is two single quotes.