Returning a varchar from a stored procedure

M

Maziar Aflatoun

Hi,

How do I return a varchar value from a stored procedure?
Ex.
SELECT GotoURL FROM MarketingCampaigns WHERE CampaignID=@CampaignID

I like to return the GotoURL as a string? I don't want to put the whole
result set in a dataset and then grab the GotoURL.



Thank you
Maz.
 
R

Rik Moed

Maziar Aflatoun said:
Hi,

How do I return a varchar value from a stored procedure?
Ex.
SELECT GotoURL FROM MarketingCampaigns WHERE CampaignID=@CampaignID

I like to return the GotoURL as a string? I don't want to put the whole
result set in a dataset and then grab the GotoURL.



Thank you
Maz.

Hi Maz,

I would use an output parameter for this.

CREATE PROCEDURE dbo.sp_name
(
@CampaignID int
, @GotoURL varchar(50) output
)
AS
SELECT @GotoURL = GotoURL FROM MarketingCampaigns WHERE
CampaignID=@CampaignID
GO
 
G

Guest

In addition to this, use the SqlCommand.ExecuteScalar method to return just
the first column of the first row, avoiding messing with the dataset. Here
is a description from the help docs of ExecuteScalar:

Return Value
The first column of the first row in the result set, or a null reference if
the result set is empty.

Ian Suttle
http://www.IanSuttle.com
 
B

Brad

SqlCommand.ExecuteScalar will do exactly want you need.
SqlCommand.CommandText = "myStoredProcedure"
......
Dim gotoUrl as string = SqlCommand.ExecuteScalar
Alternative is to return as an output parameter
 
M

Maziar Aflatoun

Thanks guys. Got it.
Maz.


Brad said:
SqlCommand.ExecuteScalar will do exactly want you need.
SqlCommand.CommandText = "myStoredProcedure"
......
Dim gotoUrl as string = SqlCommand.ExecuteScalar
Alternative is to return as an output parameter
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top