Wrox Programmer Forums
Go Back   Wrox Programmer Forums > SQL Server > SQL Server 2000 > SQL Server 2000
|
SQL Server 2000 General discussion of Microsoft SQL Server -- for topics that don't fit in one of the more specific SQL Server forums. version 2000 only. There's a new forum for SQL Server 2005.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the SQL Server 2000 section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old October 16th, 2007, 09:06 AM
Authorized User
 
Join Date: Oct 2007
Posts: 46
Thanks: 1
Thanked 0 Times in 0 Posts
Send a message via MSN to debbiecoates
Default calling a stored proceedure from another one

say I have a stored procedure called sp_FindUser which has an input parameter of projectID, and an output parameterr of userID

and then I write another stored proceedures
this time it creates a temp table full of project id's

ie

select ProjectID
into #Temp
from projects

now I want to look at all the records in #temp and find the userID using my first stored procedure

In vb I could open up all the records in #temp and loop round executing a function for each projectid.

eg
rs.open (select * from #Temp)
    do until rs.eof
    debug.print rs.fields(Projectid)
    debug.print sp_FindUser(rs.fields(projectid))


etc etc

have you any idea how i go about this?



 
Old October 16th, 2007, 09:22 AM
joefawcett's Avatar
Wrox Author
 
Join Date: Jun 2003
Posts: 3,074
Thanks: 1
Thanked 38 Times in 37 Posts
Default

I think this is a case where you will need to use a cursor which allows similar access to the rows as your VB ADO example. Look in Books On Line for cursor examples.

--

Joe (Microsoft MVP - XML)
 
Old October 16th, 2007, 11:25 PM
Friend of Wrox
 
Join Date: Oct 2006
Posts: 475
Thanks: 0
Thanked 9 Times in 9 Posts
Default

Oh no... post the code for your sp_FindUser proc and let's see what we can do... using a cursor here will absolutely kill any notion of performance.

--Jeff Moden
 
Old October 17th, 2007, 03:32 AM
Authorized User
 
Join Date: Oct 2007
Posts: 46
Thanks: 1
Thanked 0 Times in 0 Posts
Send a message via MSN to debbiecoates
Default

Quote:
quote:Originally posted by Jeff Moden
 Oh no... post the code for your sp_FindUser proc and let's see what we can do... using a cursor here will absolutely kill any notion of performance.

--Jeff Moden
 
Old October 17th, 2007, 03:36 AM
Authorized User
 
Join Date: Oct 2007
Posts: 46
Thanks: 1
Thanked 0 Times in 0 Posts
Send a message via MSN to debbiecoates
Default

Cheers jeff

here is the stored proceedure I have written

create PROCEDURE dbo.sp_FindUser

(
    @iProjectid INT,
    @iuserID INT OUTPUT
)


AS

SELECT TOP 1 P.projectallocationlogid, P.projectid, P.newuserid, P.createdate
into #temp
FROM ProjectAllocationLog P
WHERE P.projectid=@iProjectid
ORDER BY createdate DESC


SELECT @iuserID = newuserid
FROM #TEMP

this assigns the UserID that I want into the output parameter of the stored Procedure.

now what I wanted to do was call this stored procedure from another
eg

say I made another stored proceedure, which ran its course and gave me a list of projectID's in a table.

how do I then loop round the table and call the FindUser for every project in the new table?

 
Old October 17th, 2007, 09:05 AM
Friend of Wrox
 
Join Date: Oct 2006
Posts: 475
Thanks: 0
Thanked 9 Times in 9 Posts
Default

You don't "loop round the table" and expect any kind of performance... you must think in "sets" of data.

Correct me if I'm wrong, but it appears that all you want to find is a set of the latest users that were added to all projects. In order to do that, your sp_FindUser proc finds the latest entry for a given project... instead, you need to find the latest entry for ALL projects at once...

I don't have your table structure nor your data to test with, but something like the following will work and you don't even need a temp table...

Code:
 SELECT p.ProjectAllocationLogID, p.ProjectID, p.NewUserID, d.LatestCreateDate
   FROM ProjectAllocationLog p,
        (--==== Derived table "d" finds latest entry for eash ProjectID
         SELECT ProjectID,MAX(CreateDate) AS LatestCreateDate
           FROM ProjectAllocationLog
        )d
  WHERE p.ProjectID  = d.ProjectID
    AND p.CreateDate = d.LatestCreateDate


--Jeff Moden
 
Old October 17th, 2007, 09:14 AM
Authorized User
 
Join Date: Oct 2007
Posts: 46
Thanks: 1
Thanked 0 Times in 0 Posts
Send a message via MSN to debbiecoates
Default

Yes, I see what you mean, about looking at the data in sets, I've always looked row by row, if you know what I mean. Anyway, i shall look into it, and try the code you wrote and adapt it to how I need it.

Thank You very much x

:)

 
Old October 17th, 2007, 11:35 AM
Registered User
 
Join Date: Oct 2007
Posts: 4
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Calling a sp from another sp can cost performance. I suggest you use the inner join to perform the task in sp instead of calling another sp.







Similar Threads
Thread Thread Starter Forum Replies Last Post
calling stored procedure jomet JSP Basics 0 November 23rd, 2007 08:06 AM
Calling an insert stored proc from a select stored dzitam SQL Language 10 April 2nd, 2007 12:39 PM
Calling Stored Procedure with parameters zarina_24 Classic ASP Professional 4 March 2nd, 2006 11:57 AM
Calling an Oracle Stored Procedure booksnore2 BOOK: Professional SQL Server Reporting Services ISBN: 0-7645-6878-7 2 October 1st, 2004 09:35 AM
Stored Proceedure Returning A Recordset rodmcleay C# 3 July 5th, 2004 09:53 PM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.