views:

40

answers:

2

Why does this work perfectly:

SET @columnQuery = 'INSERT INTO ColumnNames SELECT ORDINAL_POSITION, COLUMN_NAME FROM information_schema.columns WHERE TABLE_NAME=''residents'' ORDER BY ORDINAL_POSITION;';

PREPARE STMT FROM @columnQuery;
EXECUTE STMT;

but this does not:

DECLARE TableName VARCHAR(50);
SET @TableName = 'residents';

SET @columnQuery = 'INSERT INTO ColumnNames SELECT ORDINAL_POSITION, COLUMN_NAME FROM information_schema.columns WHERE TABLE_NAME=''' + @TableName + ''' ORDER BY ORDINAL_POSITION;';

PREPARE STMT FROM @columnQuery;
EXECUTE STMT;

and neither does this:

DECLARE TableName VARCHAR(50);
SET @TableName = 'residents';

SET @columnQuery = CONCAT('INSERT INTO ColumnNames SELECT ORDINAL_POSITION, COLUMN_NAME FROM information_schema.columns WHERE TABLE_NAME=''', @TableName, ''' ORDER BY ORDINAL_POSITION;');

PREPARE STMT FROM @columnQuery;
EXECUTE STMT;

Neither does this:

SET @columnQuery = 'INSERT INTO ColumnNames SELECT ORDINAL_POSITION, COLUMN_NAME FROM information_schema.columns WHERE TABLE_NAME=? ORDER BY ORDINAL_POSITION;';

PREPARE STMT FROM @columnQuery;
EXECUTE STMT USING @TableName;

Here is the part of the sproc that is giving me issues:


CREATE PROCEDURE sp_ReturnSingleRowAsXml
(
  IN TableName VARCHAR(50)
)
BEGIN

DECLARE columnQuery VARCHAR(1000);

DROP TABLE IF EXISTS ColumnNames;

CREATE TEMPORARY TABLE ColumnNames (
  ID INT, ColumnName VARCHAR(50)
);

SET @columnQuery = 'INSERT INTO ColumnNames SELECT ORDINAL_POSITION, COLUMN_NAME FROM information_schema.columns WHERE TABLE_NAME=''' + @TableName + ''' ORDER BY ORDINAL_POSITION;';

PREPARE STMT FROM @columnQuery;
EXECUTE STMT;

select * from ColumnNames;

END;

CALL sp_ReturnSingleRowAsXml('residents');

The select returns nothing when it should return about 40 rows. BUT, if I hard code the word 'residents' IN the @columnQuery, I get results! And yes, there is definitely a table named 'residents'.

Sometimes I get this error, sometimes I don't, depending on whether I'm using CONCAT or just manually concatenating strings:

MySQL Database Error: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'NULL' at line 1

A: 

try this:

DECLARE TableName VARCHAR(50); 
SET @TableName = 'residents'; 

SET @columnQuery = 'INSERT INTO ColumnNames SELECT ORDINAL_POSITION, COLUMN_NAME FROM information_schema.columns WHERE TABLE_NAME=? ORDER BY ORDINAL_POSITION;'; 

PREPARE STMT FROM @columnQuery; 
EXECUTE STMT USING @TableName; 
Brian Driscoll
That threw an error because the USING needs to be after the EXECUTE.
Blackcoil
my bad - I will update.
Brian Driscoll
It still doesn't work though. :)
Blackcoil
hmm... that should work. Are you getting an error? If so, can you post it?
Brian Driscoll
you can try wrapping the ? in TABLE_NAME=? in quotes.
Brian Driscoll
No error...it just isn't doing what it should. I will post the code from the stored procedure in question.
Blackcoil
"you can try wrapping the ? in TABLE_NAME=? in quotes" tried that too.
Blackcoil
when I do that I get the error: MySQL Database Error: Incorrect arguments to EXECUTE
Blackcoil
take a look at the simple sproc I posted above, see if you can make it work on your mysql box. it's harmless. :)
Blackcoil
A: 

I was able to get it to work with the following:

CREATE PROCEDURE `sp_ReturnSingleRowAsXml`( 
  IN TableName VARCHAR(50) 
)
BEGIN 

DROP TABLE IF EXISTS ColumnNames; 

CREATE TEMPORARY TABLE ColumnNames ( 
  ID INT, ColumnName VARCHAR(50) 
); 
SET @tblName = TableName;
SET @columnQuery = 'INSERT INTO ColumnNames SELECT ORDINAL_POSITION, COLUMN_NAME FROM information_schema.columns WHERE TABLE_NAME=? ORDER BY ORDINAL_POSITION;'; 

PREPARE STMT FROM @columnQuery; 
EXECUTE STMT USING @tblName; 

select * from ColumnNames; 

END
Brian Driscoll
Awesome! Works like a charm. So what is the deal? Input parameters can't be used directly in statements? I have to put them into a local variable first? Weird.
Blackcoil