Results 1 to 8 of 8
  1. #1
    Join Date
    Dec 2005
    Posts
    14

    Unanswered: Calling a Stored Procedure on Multiple Records

    I have a stored procedure on an SQL Server database which displays statistical data for a single record. The example below selects a user ID as a parameter and displays the user's name and the total amount of transactions he has made:
    Code:
    CREATE PROCEDURE dbo.GetUserStats @UserID BIGINT AS
    	DECLARE @TempTable TABLE
    	(
    		UserID BIGINT,
    		UserName VARCHAR(60),
    		TotalAmt FLOAT
    	)
    
    	INSERT INTO @TempTable (UserID, UserName, TotalAmt)
    	SELECT u.RecID, u.LastName + ', ' + u.FirstName,
    	(SELECT SUM(t.Amount) FROM Transactions t WHERE t.UserID = @UserID)
    	FROM Users u
    	WHERE u.RecID = @UserID
    
    	SELECT * FROM @TempTable
    GO
    So if I execute this amount entering a single ID, it returns a single row for that user:
    Code:
    UserID UserName TotalAmt
    --------------------------
    1       Doe, John 100.00
    What I would like to do is create another stored procedure which calls this one for every user returned in a query, thus returning the same data for several users:
    Code:
    UserID UserName TotalAmt
    --------------------------
    1       Doe, John  100.00
    2       Smith, Bob 123.45
    3       Blow, Joe  150.55
    Is there a way to re-use a stored procedure within another, based on the results of a query?

  2. #2
    Join Date
    Dec 2002
    Posts
    1,245
    Some thoughts:

    1. This sounds more like a function than a stored procedure. Would make the execution concept much easier (ie, SELECT dbo.MyFunction(MyUserID) FROM MyTable).

    2. You could wrap this with another stored procedure; you could pass in the user list as a parameter (there was an excellent article recently over at SQLServerCentral.com about using an xml string as a parameter).


    But I really think that what you want is a function.

    Regards,

    hmscott
    Have you hugged your backup today?

  3. #3
    Join Date
    Dec 2005
    Posts
    14
    Thanks for your feedback.

    I'm not familiar with functions. Would I want to convert GetUserStats to a function?

  4. #4
    Join Date
    Nov 2004
    Location
    on the wrong server
    Posts
    8,835
    Provided Answers: 6
    may, maybe not. you do know you can do that whole thing without the temp table.

    BTW, bigint for userid? do you know there are not that many people in the world?
    “If one brings so much courage to this world the world has to kill them or break them, so of course it kills them. The world breaks every one and afterward many are strong at the broken places. But those that will not break it kills. It kills the very good and the very gentle and the very brave impartially. If you are none of these you can be sure it will kill you too but there will be no special hurry.” Earnest Hemingway, A Farewell To Arms.

  5. #5
    Join Date
    Dec 2002
    Posts
    1,245
    Quote Originally Posted by Pizzor2000

    I'm not familiar with functions. Would I want to convert GetUserStats to a function?
    I wouldn't "convert" per se; at least not the way that you have written it.
    Try this:
    • Open Query Analyzer
    • Click on File | New
    • Double click on Create Function
    • Double click on Create Scalar Function



    Read in SQL BOL about Scalar functions.

    I think you will get an idea of what a function is and how to go about writing your own. From there I think you will find it easier to understand how to proceed.

    Regards,

    hmscott
    Have you hugged your backup today?

  6. #6
    Join Date
    Dec 2005
    Posts
    14
    Quote Originally Posted by Thrasymachus
    may, maybe not. you do know you can do that whole thing without the temp table.

    BTW, bigint for userid? do you know there are not that many people in the world?
    The example I posted is very simplified. The actual query I'm trying to pull off is pretty complicated and pulls data from several tables, that's why I included the temp table. Can I use a temp table in a function?

    Yeah, I guess a BIGINT is more than I need for a user ID. I had the misconception of thinking INTs only go to 32,000 when I set up this database (as they do in many programming languages).

  7. #7
    Join Date
    Dec 2005
    Posts
    14
    I have tried building a few functions. I can get them to run using a direct statemtent such as:

    SELECT * FROM MyFunction(1)

    But If I try to use the line above in a stored procedure or another function, I get an error:

    Invalid object name 'MyFunction'.

  8. #8
    Join Date
    Feb 2004
    Location
    In front of the computer
    Posts
    15,579
    Provided Answers: 54
    They're fiesty. You need the user name as a prefix, as in:
    Code:
    SELECT *
       FROM dbo.MyFunction(1)
    -PatP

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •