开发者

Can I pass the result of a select statement as a value for a parameter into a stored procedure?

I was just trying this knowing that my select would return one row. Is something like this possible or will I need a temporary variable?

lets say my stored procedure took one parameter:

exec dbo.GetUserData @UserName = UserName from @MyTempTable where UserId= @UserId

Or what if the parameter expected was XML? Is there a way I can do something like

exec dbo.GetUserData @UserXml =
    select 
        case 
            when @val = 1 then '1' 
            when @val = 0 then '0' 
            else NULL 
        end as '@MyId',
        @ThisId as '@ThisId',
        @ThatId as 开发者_JAVA技巧'@ThatId'
    FOR XML PATH('Info')


XML or otherwise, assign the value to the variable before using the variable in the EXEC call.

DECLARE @UserName [data type]

BEGIN

  SELECT @UserName = UserName 
    FROM @MyTempTable 
   WHERE UserId = @UserId

    EXEC dbo.GetUserData @UserName

END


You'll need a temporary variable.

DECLARE @username nvarchar(20) 
SELECT @username = Username FROM @MyTempTable WHERE UserId=@UserId

exec dbo.GetUserData @Username.
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜