一个存储过程  GetMaxUserID, 返回 最大ID号在另一个存储过程 InsertUser 中怎么调用 GetMaxUserID 取得返回值内部定义的变量?谢复!

解决方案 »

  1.   

    被调用的存储过程GetMaxUserID返回最大ID号,可采用输出参数
    Create GetMaxUserID
         @ID varchar(10),
         @MaxID varchar(10)
    AS
    ....
    在InserUser中调用
    declare @MaxID varchar(10)
    exec GetMaxUserID '001',@MaxID output
    select @MaxID
      

  2.   

    select distinct id, dbo.GetMaxUserID(id) from tablename
      

  3.   


    create table T(ID int)
    insert T select 1
    union all select 2--返回 最大ID号
    create proc GetMaxUserID @ID int output
    as
    select @ID=isnull(max(ID), 0) from T
    go--插入一行記錄
    create proc InsertUser
    as
    declare @ID int
    exec GetMaxUserID @ID output
    insert T select @ID+1
    goexec InsertUser
    goselect * from T
    go--result
    ID          
    ----------- 
    1
    2
    3(3 row(s) affected)
      

  4.   

    我的第一个 过程 GetMaxUserID 是用 return 返回 maxIDcreate proc GetMaxUserID 
    as
    declare @id
    select @ID=isnull(max(ID), 0) from Treturn @idgo问: 这时 在 InsertUser 中怎么调用 GetMaxUserID 来取得 返回的 @id ?谢
      

  5.   

    我知道用输出参数可以, 但这种只有一个返回值, 用 return 应该也是可以的, 再次 请问这个返回值在另一个 存储过程中怎么取得?
      

  6.   

    用return也可以:
    use pubs
    go 
    create proc GetMaxUserID 
    as
    declare @id as varchar(15)
    select @ID=isnull(max(pub_id), 0) from Titlesreturn @idgocreate proc InserUser
    AS
    declare @id varchar(15)
    exec @id=GetMaxUserID
    select id=@idexec InserUsergo
    ------结果
    id              
    --------------- 
    1389(所影响的行数为 1 行)
      

  7.   

    wzh1215(懒猫) 的才是我想的结果,
    谢谢各位