开发者

What am I doing wrong in this MySQL stored procedure?

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 _TAB开发者_如何学编程LENAME 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.


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;
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜