Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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 _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.

like image 951
Skychaser Avatar asked Aug 06 '10 23:08

Skychaser


People also ask

What is wrong with stored procedures?

Stored procedures introduce a cliff (or disconnect) between coherent functionality, because the domain logic gets split between the application- and the database layer. It's rarely clear where the line is drawn (e.g. which part of a query should go into the application layer and which part into the database layer?).

What are MySQL stored procedure disadvantages?

MySQL Stored Procedure DisadvantagesRestricted for complex business logic − Actually, stored procedure's constructs are not designed for developing complex and flexible business logic. Difficult to debug − It is difficult to debug stored procedures.

How do I return an error from a stored procedure in MySQL?

BEGIN select "error message '%s' and errorno '%d'"; -- use missed the semicolon ROLLBACK; END; Save this answer.

How can I tell if a stored procedure is working?

The quick and simple way to monitor the stored procedures execution is setting up a Trace with SQL Server Profiler. This is probably good for a quick check when a user runs a process in Test and you want to capture what is he/she running.


1 Answers

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;
like image 171
James Black Avatar answered Sep 21 '22 03:09

James Black