Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
Home
Discussion Groups
DB Engine
SQL ServerMSDESQL Server CE
Services
Analysis (Data Mining)Analysis (OLAP)DTSIntegration ServicesNotification ServicesReporting Services
Programming
CLRConnectivitySQLXML
Other Technologies
ClusteringEnglish QueryFull-Text SearchReplicationService Broker
General
Data WarehousingPerformanceSecuritySetupSQL Server ToolsOther SQL Server Topics
DirectoryUser Groups
Related Topics
MS AccessOther DB ProductsMS Server Products.NET DevelopmentVB DevelopmentJava DevelopmentMore Topics ...

SQL Server Forum / General / Other SQL Server Topics / January 2007

Tip: Looking for answers? Try searching our database.

Executing Stored Procedures

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Daniel.Peaper@gmail.com - 29 Jan 2007 10:20 GMT
Hi All,

I am running SQL2000. Can anyone tell me how I can use the contents of
a table as parameters for a stored procedure that resides on a
different SQL server?

I have a table:

Customers:
Cust_ID
Cust_Name
Cust_Contact
Cust_Phone

I need to execute a stored procedure and pass each of the above as
parameters:

@CustID, @CustName, @CustContact,@CustPhone

The stored procedure also returns an error code.

Thanks,
Danny
Dan Guzman - 29 Jan 2007 12:40 GMT
> I need to execute a stored procedure and pass each of the above as
> parameters:

One method is to select the values into local variables and then pass those
as parameters.  For example:

SELECT
   @CustID = CustID,
   @CustName = CustName,
   @CustContact = CustContact,
   @CustPhone = CustPhone
FROM dbo.Customers
WHERE CustID = 1

EXEC @ReturnCode = dbo.MyProcedure
   @CustID = @CustID,
   @CustName = @CustName,
   @CustContact = @CustContact,
   @CustPhone = @CustPhone

Signature

Hope this helps.

Dan Guzman
SQL Server MVP

> Hi All,
>
[quoted text clipped - 19 lines]
> Thanks,
> Danny
Daniel.Peaper@gmail.com - 30 Jan 2007 03:18 GMT
On Jan 29, 10:40 pm, "Dan Guzman" <guzma...@nospam-
online.sbcglobal.net> wrote:
> > I need to execute a stored procedure and pass each of the above as
> > parameters:
[quoted text clipped - 51 lines]
>
> - Show quoted text -

Thanks for that Dan,

Will this not just process the first record CustID=1? How would I go
about processing the whole table? Do I have to build a client
application to process a loop or can I proccess this on the SQL
server?
Erland Sommarskog - 30 Jan 2007 11:28 GMT
> Will this not just process the first record CustID=1? How would I go
> about processing the whole table? Do I have to build a client
> application to process a loop or can I proccess this on the SQL
> server?

You could set up a cursor. However, using loops is not a very efficient
use of SQL Server. It may be better to replace the stored procedure
with statements that operates on the entire table at once. Since the
stored procedure is another server, this is not exactly trivial, depending
a bit of what's in that remote procedure.

The way to write a cursor would be:

DECLARE custcur INSENSITIVE CURSOR FOR
   SELECT CustID, CustName, CustContact, CustPhone
   FROM   Customers

OPEN custcur

WHILE 1 = 1
BEGIN
  FETCH custcur INTO @CustID, @CustName, @CustContact, @CustPhone
  IF @@fetch_status <> 0
      BREAK

  EXEC SERVER.db.dbo.remote_sp @CustID, @CustName, @CustContact, @CustPhone
END

DEALLOCATE custcur

Signature

Erland Sommarskog, SQL Server MVP, esquel@sommarskog.se

Books Online for SQL Server 2005 at
http://www.microsoft.com/technet/prodtechnol/sql/2005/downloads/books.mspx
Books Online for SQL Server 2000 at
http://www.microsoft.com/sql/prodinfo/previousversions/books.mspx

 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2009 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.