Note that there are some explanatory texts on larger screens.

plurals
  1. POCreating a Stored Procedure in MySQL with C#
    text
    copied!<p>I'm trying to create a new stored procedure programatically using the following code:</p> <pre><code>using (MySqlConnection conn = new MySqlConnection(connectionString)) { conn.Open(); using (MySqlTransaction trans = conn.BeginTransaction()) { using (MySqlCommand command = conn.CreateCommand()) { command.CommandText = query; command.ExecuteNonQuery(); } trans.Commit(); } } </code></pre> <p>And the following text as the create statement copied from Mysql workbench:</p> <pre><code>static string query = @" delimiter $$ CREATE PROCEDURE `GetParentIds`(IN `tempTableName` VARCHAR(255), IN `id` int) BEGIN DECLARE parId INT; DECLARE curId INT; DROP TEMPORARY TABLE IF EXISTS tempTableName; CREATE TEMPORARY TABLE tempTableName ( node_id INT NOT NULL PRIMARY KEY ); set curId := id; get_parents_loop: LOOP set parId := null; set parId = (select ParentID from {TableName} where ID = curId); IF parId is NULL THEN LEAVE get_parents_loop; END IF; INSERT INTO tempTableName(node_id) Values (parId); set curId := parId; END LOOP get_parents_loop; SELECT * FROM tempTableName; END$$"; </code></pre> <p>This procedure is passed an ID of an object which has a parentID and it gets all of the parentIDs of all the parents of the given object and returns them. The problem comes when I try to run it and I get the following message: "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 $$</p> <p>CREATE PROCEDURE <code>GetParentIds</code>(IN <code>tempTableName</code> VARCHAR(255),' at line 1"</p> <p>Any and all Ideas are welcome!</p> <p><strong>* EDIT **</strong> Thanks to all the answers below, this is what finally worked:</p> <pre><code>CREATE PROCEDURE GetParentIds(IN tempTableName VARCHAR(255), IN id int) BEGIN DECLARE parId INT; DECLARE curId INT; DROP TEMPORARY TABLE IF EXISTS tempTableName; CREATE TEMPORARY TABLE tempTableName (node_id INT NOT NULL PRIMARY KEY ); set curId := id; get_parents_loop: LOOP set parId := null; set parId = (select ParentID from TDOs where TDOID = curId); IF parId is NULL THEN LEAVE get_parents_loop; END IF; INSERT INTO tempTableName(node_id) Values (parId); set curId := parId; END LOOP get_parents_loop; SELECT * FROM tempTableName; END; </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload