Microsoft SQL Server
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
 
User Name:
Password:
Remember me
 
Go Back   Dev Articles Community ForumsDatabasesMicrosoft SQL Server

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Display Modes
 
Unread Dev Articles Community Forums Sponsor:
  #1  
Old September 26th, 2004, 01:54 PM
Frunobulax Frunobulax is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Sep 2004
Posts: 1 Frunobulax User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
Return variable from a Stored Procedure?

I bought a component to do calculations based on Zip code distances, and I'm trying to create a stored procedure out of their sample code to return the distance value. Here's what I have:

CREATE PROCEDURE zipcode_distance
@ZipStart nchar(5),
@ZipEnd nchar(5)

as

DECLARE @Lat1 REAL
DECLARE @Long1 REAL
DECLARE @Lat2 REAL
DECLARE @Long2 REAL
DECLARE @Result varchar(5)
SET @Result = 0
DECLARE @FirstExists BIT
SET @FirstExists = 1

SELECT @Lat1 = Latitude, @Long1 = Longitude FROM ZIPCodes WHERE ZIPCode = @ZIPStart AND CityType = 'D'

IF 0 = @@ROWCOUNT BEGIN
SET @FirstExists = 0
SET @Result = -1 /* ZIPStart not found */
END

SELECT @Lat2 = Latitude, @Long2 = Longitude FROM ZIPCodes WHERE ZIPCode = @ZIPEnd AND CityType = 'D'

IF 0 = @@ROWCOUNT BEGIN
IF 0 = @FirstExists SET @Result = -3 /* ZIPStart & ZIPEnd not found */
ELSE SET @Result = -2 /* ZIPEnd not found */
END

IF 0 = @Result SELECT dbo.DistanceAssistant(@Lat1,@Long1,@Lat2,@Long2) As Distance
ELSE SELECT @Result AS Distance

RETURN @Result

GO

I'm passing ZipStart and ZipEnd from form fields. "@Return" returns the values stated above (0, -2 or -3) (and I can return that to the ASP page with no problem), but what I really want to do is get the "Distance" value into something that I can pull back to the ASP page. My SQL experience to date is basically just insert and update, so I'm just kind of fumbling around and would appreciate the help.

Reply With Quote
  #2  
Old October 8th, 2004, 11:13 AM
vikramp vikramp is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Oct 2004
Posts: 7 vikramp User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
Hi Frunobulax,

To get the value of distance back in your ASP page in a variable, you need to add OUTPUT parameter to your stored procedure. For example:

CREATE PROCEDURE zipcode_distance
@ZipStart nchar(5),
@ZipEnd nchar(5),
@Distance varchar(5) OUTPUT

Then, assign whatever is the result to this @Distance variable as:
Set @Distance = @Result

Now, in asp page, pass this new parameter to the stored procedure, but without a value (since this is OUTPUT parameter). Then, once stored procedure is executed, you can access value of this parameter as

cmd.Parameters("@Distance")


Regards,

Vikram

Reply With Quote
Reply

Viewing: Dev Articles Community ForumsDatabasesMicrosoft SQL Server > Return variable from a Stored Procedure?


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump


Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 





© 2003-2008 by Developer Shed. All rights reserved. DS Cluster 5 hosted by Hostway
Stay green...Green IT