【问题标题】:Stored Procedure resultset into sub query in another SP存储过程结果集到另一个 SP 中的子查询
【发布时间】:2013-12-20 06:31:24
【问题描述】:

我想在另一个存储过程中使用存储过程结果,例如子查询。有可能吗..?

下面的示例代码:密码生成逻辑在一个 sp 中创建:

Create Procedure GetDynamicPassword
@Password out
As

Begin

    -- Password generate Logic createhere
        -- More than 100 line to generate password logic


End

我想将上述结果用于另一个 SP。

无法使用临时表,因为我需要这个结果一次性处理这么多记录。

我想在下面的sp里面使用上面的sp。

Create Procedure GetDynamicUsers


As

Begin


    Select a.col1, b.col2 , c.col3...
    ( I want to uses password here from above SP )
        ( like exec GetDynamicPassword) As Password 
        from Table1 As a
    inner join Table2 As b on a.Code = b.Code
        inner join Table3 As c on a.Code = c.Code 
    inner join Table4 As d on a.Code = d.Code


End

【问题讨论】:

  • 您可以在查询中使用函数,但不能在 proc 中使用。在 GetDynamicUsers 中执行 GetDynamicPassword 并将其存储在临时表中。为什么不能这样做。

标签: sql sql-server sql-server-2008 sql-server-2005 stored-procedures


【解决方案1】:

将您的存储过程GetDynamicPassword 重写为Scalar valued User-Defined Function

那么你就可以直接在你的select语句中使用这个函数了。

【讨论】:

    【解决方案2】:

    最好的做法是创建一个函数,然后在你的 SP 中调用它

    Create function GetDynamicPassword
    returns varchar(100)
    As
    
    Begin
    
    -- Password generate Logic createhere
        -- More than 100 line to generate password logic
    
    
    End
    

    然后在下面的 SP 中使用它

    Create Procedure GetDynamicUsers
    
    As
    
    Begin
    
    Select a.col1, b.col2 , c.col3...,
    dbo.GetDynamicPassword() As Password 
        from Table1 As a
    inner join Table2 As b on a.Code = b.Code
        inner join Table3 As c on a.Code = c.Code 
    inner join Table4 As d on a.Code = d.Code
    
    
    End
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2013-05-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-08-27
      相关资源
      最近更新 更多