0

我想在 mySql 中使用存储过程,它必须返回identity_scope(). 我试试这个但不工作:


CREATE DEFINER=`root`@`localhost` PROCEDURE `InsertVideo`(  
  OUT out_scope_id        INT,
    IN  in_youtubeidvideo       VARCHAR(15),
    IN  in_title                  VARCHAR(200),
    IN  in_rating           DOUBLE,
    IN  in_viewcount        INT
)
BEGIN 

     INSERT INTO Video 
     (                
       YoutubeIdVideo,      
       Title,               
       Rating,              
       ViewCount,                     
       DataAdded,
       ConvertedFlag
     )

     VALUES 
     (
       in_youtubeidvideo,                
       in_title,             
       in_rating,        
       in_viewcount,
       CURDATE(),
       false
     );
END

using (MySqlConnection conn = new MySqlConnection(ConfigurationManager.ConnectionStrings["AxWaveConnection"].ToString()))
            {
                try
                {
                    conn.Open();
                }
                catch (Exception ex)
                {
                    Console.WriteLine(ex.Message);
                }

                MySqlCommand cmd = new MySqlCommand("InsertVideo", conn);
                cmd.CommandType = System.Data.CommandType.StoredProcedure;

                cmd.Parameters.Add("out_scope_id", scopeID);
                cmd.Parameters.Add(new MySqlParameter("in_youtubeidvideo", VideoId));
                cmd.Parameters.Add(new MySqlParameter("in_title", Title));
                cmd.Parameters.Add(new MySqlParameter("in_viewcount", ViewCount));
                cmd.Parameters.Add(new MySqlParameter("in_rating", Rating));

                cmd.Parameters["out_scope_id"].Direction = ParameterDirection.Output;


                try
                {
                    cmd.ExecuteNonQuery();

                    scopeID = Convert.ToInt32(cmd.Parameters["out_scope_id"].Value);
                }
                catch (Exception)
                {
                    scopeID = -1;        //Duplicate Record
                }



                conn.Close();
}
4

2 回答 2

2

您忘记在 proc 中设置参数的值;存储过程中的最后一行应如下所示:

select out_scope_id = LAST_INSERT_ID();

顺便说一句:SCOPE_IDENTITY() 是一个 MS SQL Server 函数;不是 MySQL 函数。

于 2013-01-28T18:31:21.257 回答
0
SELECT @@IDENTITY AS 'Identity';
GO
于 2013-01-28T18:32:15.950 回答