views:

48

answers:

1

I'm trying to use the following stored procedure.

DELIMITER $$

CREATE DEFINER=`root`@`localhost` 
PROCEDURE `DeleteField`( IN _TABLENAME Text, IN _FIELDNAME text)
BEGIN
  if exists (select * from information_schema.Columns 
    where table_name = _TABLENAME and column_name = _FIELDNAME) 
  then 
    alter table _TABLENAME drop column _FIELDNAME;
  end if;
END

So I do Call('anytable','Anyfield') and I get the Error Error Code:1146Table'Database._tablename'doesn't exist This _tablename should be my parameter, not a string.

Plz some help before I hang myself, I love my life far too much.

+5  A: 

I expect you will need to create a dynamic SQL query to do this.

An example of how to do this is at:

http://www.java2s.com/Code/SQL/Procedure-Function/Createadynamicstatementinaprocedure.htm

This would be the alter table replacement, though I have tested this.

    DECLARE l_sql VARCHAR(4000);
    SET l_sql=CONCAT_ws(' ',
                'ALTER table ',_TABLENAME,' drop column ',_FIELDNAME);
    SET @sql=l_sql;
    PREPARE s1 FROM @sql;
    EXECUTE s1;
    DEALLOCATE PREPARE s1;
James Black
+1: Absolutely correct.
OMG Ponies
Thank you James, you saved my life and I feel I owe you half a cookie or something. Really, thank you a lot guy.I still don't know why I have to use dynamic procedures, but I can save this understanding for later, any link on that would be really appreciated though. :)
Skychaser