1

I want to be able to see the sql text from SHOW CREATE TABLE for any table for debugging purposes. Can I do that with prepared escaping?

// doesn't work because table name is escaped as string
 SHOW CREATE TABLE ?

//doesn't work, syntax is not allowed    
SHOW CREATE TABLE (SELECT table_name FROM `INFORMATION_SCHEMA.tables`   WHERE  `TABLE_NAME`=?)
2
  • Just use the select from information_schema without SHOW CREATE TABLE Commented Apr 7, 2016 at 19:57
  • "Parameter markers can be used only where data values should appear, not for SQL keywords, identifiers, and so forth." -- dev.mysql.com/doc/refman/5.7/en/prepare.html Commented Apr 7, 2016 at 20:12

1 Answer 1

1

You could use prepared statement:

-- here goes your table name, could be parametrized to call from app
SET @tab_name = '`tab`';   

SET @query = REPLACE('SHOW CREATE TABLE @tab_name', '@tab_name', @tab_name);

-- SELECT @query;

PREPARE stmt1 FROM @query;
EXECUTE stmt1;
DEALLOCATE PREPARE stmt1;

SqlFiddleDemo

Output:

╔════════╦═════════════════════════════════════════════════════════════════════════════════════════════════════════════════════╗
║ Table  ║                                                    Create Table                                                     ║
╠════════╬═════════════════════════════════════════════════════════════════════════════════════════════════════════════════════╣
║ tab    ║ CREATE TABLE `tab` ( `col1` int(11) DEFAULT NULL, `col2` datetime DEFAULT NULL ) ENGINE=InnoDB DEFAULT CHARSET=utf8 ║
╚════════╩═════════════════════════════════════════════════════════════════════════════════════════════════════════════════════╝

If you need it for every table you could wrap it with loop or cursor.

Warning:

You should quote @tab_name in application layer to avoid SQL Injection.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.