The sum of all SQL Server matter that exist,
and the space in which all SQL Server events
occur or could occur.
Welcome to SQL Server Universe.com Sign in | Join | Help
in
Home SS SLUG Forums Articles Photos Downloads

Stored procedure within a stored procedure

Last post 05-17-2008, 8:01 by Dinesh Priyankara. 1 replies.
Sort Posts: Previous
  • Stored procedure within a stored procedure

     05-17-2008, 0:20

    • Joined on 05-16-2008
    • Posts 1
    • Points 0
    • Top 75 Contributor

    Hello,

    I am hoping there is a solution within SQL that work for this instead of making round trips from the front end. I have a stored procedure that is called from the front-end(USP_DistinctBalancePoolByCompanyCurrency) that accepts two parameters and returns one column of data possibly several rows. I have a second stored procedure(USP_BalanceDateByAccountPool) that accepts the previous stored procedures return values. What I would like to do is call the first stored procedure from the front end and return the results from the second stored procedure. Since it's likely I will have more than row of data, can I loop the second passing each value returned from the first?

    The Stored Procs are:

    CREATE PROCEDURE USP_DistinctBalancePoolByCompanyCurrency
    @CID int,
    @CY char(3)
    AS
    SELECT Distinct S.BalancePoolName
    FROM SiteRef S
     INNER JOIN Account A ON A.PoolId=S.ID      
          Inner JOIN AccountBalance AB ON A.Id = AB.AccountId
          Inner JOIN AccountPool AP On AP.Id=A.PoolId
    Where A.CompanyId=@CID And AB.Currency=@CY


    CREATE PROCEDURE USP_BalanceDateByAccountPool
    @PoolName varchar(50)
    AS
    Declare @DT datetime
    Select @DT=
    (Select MAX(AccountBalance.DateX) From Company Company
     INNER JOIN Account Account ON Company.Id = Account.CompanyId
          INNER JOIN SiteRef SiteRef ON Account.PoolId = SiteRef.ID
          Inner JOIN AccountBalance AccountBalance ON Account.Id = AccountBalance.AccountId
    WHERE SiteRef.BalancePoolName = @PoolName)
    SELECT SiteRef.BalancePoolName, AccountBalance.DateX, AccountBalance.Balance
    FROM Company Company
     INNER JOIN Account Account ON Company.Id = Account.CompanyId
          INNER JOIN SiteRef SiteRef ON Account.PoolId = SiteRef.ID
          Inner JOIN AccountBalance AccountBalance ON Account.Id = AccountBalance.AccountId
    WHERE SiteRef.BalancePoolName = @PoolName And AccountBalance.DateX = @DT
    Order By AccountBalance.DateX DESC

    Any assistance would be greatly appreciated.

    Thank you,
    Dave

  • Re: Stored procedure within a stored procedure

     05-17-2008, 8:01

    • Joined on 11-22-2006
    • Colombo, Sri Lanka
    • Posts 97
    • Points 0
    • Top 10 Contributor
      Male
    Hi Dave,

    There are many ways to send a result set directly to another stored procedure. Easiest method in your case is, insert the result set to a temporary table with first stored procedure and access it from the second procedure. You will be calling the second stored procedure inside the first one.

    Or else, you can set the result set of the first procedure to xml variable and pass it to the second procedure. However in this case, you need to change the second procedure's parameter type to xml.

    Hope this info is enough to solve your problem, if need sample code, please let us know.

View as RSS news feed in XML

(Best viewed with a resolution of more than 1024 * 768)

Powered by Community Server, by Telligent Systems