我想用存储过程写,因为有两个表,要获取对第一个表操作后的ID
存储过程我是这么写的:
CREATE PROCEDURE Add_Soft
(
  @Soft_ID int OUTPUT,
  @Soft_Big_CategoryID int=NULL)
AS  INSERT Soft_Soft
  (Soft_Big_CategoryID)
  SELECT @Soft_Big_CategoryID
  SELECT  @Soft_ID = @@IDENTITY---------------
GO
后台代码的编写获取是:
int softid;
SqlParameter outputID = cmd1.Parameters.Add("@Soft_ID",SqlDbType.Int);
outputID.Direction = ParameterDirection.Output;
softid = Convert.ToInt32(outputID.Value);
这样得到的好象不对啊,请教大虾~

解决方案 »

  1.   

    换一下顺序,让@@IDENTITY紧跟在Insert后面
      INSERT Soft_Soft
      (Soft_Big_CategoryID)
      SELECT  @Soft_ID = @@IDENTITY  SELECT @Soft_Big_CategoryID
      

  2.   

    我试了不行啊
    它说INSERT后不能跟赋值的SELECT
    顺序不能调换啊
      

  3.   

    不好意思,我把一个Insert看成两句了,试试这样写:  INSERT Soft_Soft(Soft_Big_CategoryID) VALUES(@Soft_Big_CategoryID)  SELECT  @Soft_ID = @@IDENTITY
      

  4.   

    did you run....
    SqlParameter outputID = cmd1.Parameters.Add("@Soft_ID",SqlDbType.Int);
    outputID.Direction = ParameterDirection.Output;cmd1.Parameters.Add("@Soft_Big_CategoryID",123);cmd1.ExecuteNonQuery(); ///??softid = Convert.ToInt32(cmd1.Parameters["@Soft_ID"].Value);
      

  5.   

    懂了,我是在之后RUN的
    如果要得到返回值,必须先执行存储过程,才能正确获取
    辛苦两位了
    谢谢