开发者

MySql adding column name with last_insert_id()

开发者 https://www.devze.com 2023-03-20 10:21 出处:网络
I\'m trying to add a new mysql column in a table, using an insert_id from an insert of another table. This is the sentence that i use...

I'm trying to add a new mysql column in a table, using an insert_id from an insert of another table. This is the sentence that i use...

string sqlInsert = "INSERT INTO test (IdPico, Nombre, TextoBienvenida, FechaCreacion) VALUES (1, 'nombretest', 'aslkñdfa lsñdk asjd asldkf añlsj f', '2011-07-13 10:22:53'); ";
sqlInsert += "SET @IDTESTCREATED := CONCAT('Test', LAST_INSERT_ID(); ";
sqlInsert += "ALTER TABLE Usuarios ADD COLUMN @IDTESTCREATED BIT DEFAULT 0; ";

I using ASP.NET 4.0 and MySql connection, and server responds with 'Fatal error encountered du开发者_开发技巧ring command execution. '

Could anybody help me?


Well ... I answer myself.

After making a deep search, I have not found how to add a column dynamically by a variable in mysql.

At end I had to make two querys, first to insert the test and get the id, and second to update the users table.

Since the insertion and retrieval of id are in the same query, no problems of persistent connections and concurrent updates.

 string sqlInsert = "INSERT INTO Test (<fields>) VALUES (<values>);";
    sqlInsert += "SELECT LAST_INSERT_ID() AS IdTestInserted; ";
 string idnewtest = <result of insert query>;
 string sqlAlter = "ALTER TABLE Users ADD COLUMN Test" + idnewtest + " BIT DEFAULT 0; ";

I regret not having found the answer, but at least I achieved my goal. Thank you all for your help!

0

精彩评论

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