Wrox Programmer Forums
Go Back   Wrox Programmer Forums > SQL Server > SQL Server 2005 > SQL Server 2005
|
SQL Server 2005 General discussion of SQL Server *2005* version only.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the SQL Server 2005 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 January 29th, 2009, 10:16 AM
Friend of Wrox
 
Join Date: Aug 2006
Posts: 231
Thanks: 0
Thanked 1 Time in 1 Post
Default best way of stored procedure

hi
in my Stored procedure, i need to call a table with where clause. from the result of the query i

will make in clause and will call another table like select * from <tablename> where memberid in

(@memberid). the @memberid is string which is result of first query.

what is the best approch here should i take temp tables or should i declare cursor to iterate or

should i used both for some time and then release.
please guide me to use Best way stored procedure for optimal results.
__________________
thanks......
 
Old January 29th, 2009, 05:30 PM
Friend of Wrox
 
Join Date: Jun 2008
Posts: 1,649
Thanks: 3
Thanked 141 Times in 140 Posts
Default

Best answer: DO NOT DO THIS!

Unless you really NEED to return *TWO* result sets (record sets, data tables, whatever) from the SP (which is unusual, but sometimes done) you should do it all in ONE query.

Join the tables. Or, at worst, use a sub-select.

Example:
Code:
Table: Departments
deptid  ::  deptname
113    ::  Administration
773    ::  Engineering
...

Table: Employees
empid :: deptid :: lastname ...
101  ::  773 :: Jones
102  ::  113 :: Adams
103  ::  981 :: Somebody

CREATE PROCEDURE GetEmployeesByDepartmentName( @deptname NVARCHAR(100) )
AS
SELECT E.* 
FROM Departments AS D, Employees AS E
WHERE D.deptid = E.deptid
AND D.deptname = @deptname
ORDER BY E.lastname
Give more details and we can surely find a way to avoid doing two SELECTs. Unless, again, you really *NEED* to get two resultsets. If so, maybe show the code in the "host language" that you will using.
 
Old January 30th, 2009, 01:13 AM
Friend of Wrox
 
Join Date: Aug 2006
Posts: 231
Thanks: 0
Thanked 1 Time in 1 Post
Default

do you think using the cursors will effect performance of DB Server or will increase size at run time.
__________________
thanks......
 
Old January 30th, 2009, 01:26 AM
Friend of Wrox
 
Join Date: Oct 2006
Posts: 475
Thanks: 0
Thanked 9 Times in 9 Posts
Default

What is it that you're actually trying to do? Forget, for a moment, what you think you want to do in code... I'm asking what the business requirements are so I can get a better idea of how to help you.

And, in SQL Server 2005, a cursor is NEVER necessary for anything.
__________________
--Jeff Moden





Similar Threads
Thread Thread Starter Forum Replies Last Post
stored procedure prashant_telkar SQL Server 2000 1 July 9th, 2007 07:57 AM
stored procedure keyvanjan Classic ASP Basics 6 August 1st, 2006 07:42 AM
stored procedure kvanchi ADO.NET 1 December 9th, 2004 07:27 AM
Stored Procedure... babloo81 SQL Server 2000 2 May 1st, 2004 11:25 PM
C# and stored procedure Msmsn C# 1 August 26th, 2003 11:03 PM





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