存储过程结果集到另一个SP中的子查询中

时间:2013-12-20 06:31:25

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

我想将存储过程结果用于另一个存储过程的内部,如子查询。有可能..?

以下示例代码:密码生成逻辑在一个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

2 个答案:

答案 0 :(得分:2)

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

然后您可以直接在select语句中使用该函数。

答案 1 :(得分:1)

这样做的最佳做法是创建一个函数,然后在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