3

I am using MySQL database with C# to develop an application.Using MySQL Server 5.0 and odbc connector. In some cases I am required to execute ddl commands such as ALTER TABLE or CREATE TABLE to manipulate the database. In these cases I need to use the IF EXISTS command to check the database before I execute commands. I write below commands that execute without any problem in Navicat or Workbench, but do not work when send this commands with application by ExecuteNoneQury methods.

what is wrong?

use db;
drop procedure if exists  sp_update ;
delimiter //

create procedure sp_update()
begin

     if not exists( SELECT column_name FROM INFORMATION_SCHEMA.COLUMNS WHERE TABLE_NAME = 'tab' AND COLUMN_NAME = 'col' and table_schema = 'db') then
       ALTER TABLE `tab` ADD COLUMN `col`  int(11) NULL DEFAULT NULL ;
    end if;

end//

delimiter ;
call sp_update();
drop procedure if exists  sp_update ;

C# Command :

public override int ExecuteNoneQuery(string commandText)
    {
        int obTemp = 0;
        Conn = new MySqlConnection(Connection.ConnectionString);
        try
        {
            MySqlCommand MySqlCommand = new MySqlCommand(commandText, Conn);
            if (Conn.State == ConnectionState.Closed)
            {
                Conn.Open();
            }
            obTemp = MySqlCommand.ExecuteNonQuery();
        }
        finally
        {
            if (Conn.State == ConnectionState.Open)
            {
                Conn.Close();
            }
        }
        return obTemp;
    }
2
  • 5
    Please post your ADO.NET code. Commented Nov 20, 2011 at 20:55
  • Error Is: 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 'delimiter // create procedure sp_update() begin if not exists( SELECT' at line 1 Commented Nov 21, 2011 at 16:20

2 Answers 2

3

"delimiter" is not MySQL syntax.. It is a convinience function for the mysql command line client and is only understood by it (well, some GUI clients mimic the behavior too, to be able to run scripts that are originally thought for command line client).

But, you do not need "delimiter" in any code executed by connectors. Using it will result in syntax error like the one you got.

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

1 Comment

Thank you very much. Saved me a lot of time and nerves.
1

I solved my own problem. I needed to split up my sql command into two parts.

Part 1 create procedure:

drop procedure if exists  sp_update ;
create procedure sp_update()
begin

     if not exists( SELECT column_name FROM INFORMATION_SCHEMA.COLUMNS WHERE TABLE_NAME = 'tab' AND COLUMN_NAME = 'col' and table_schema = 'db') then
       ALTER TABLE `tab` ADD COLUMN `col`  int(11) NULL DEFAULT NULL ;
    end if;
end

Part 2:

call sp_update();
drop procedure if exists  sp_update ;

Send each command to MySQL separately.

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.