Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 225
  • Last Modified:

CAn I return a varchar value from a msswl stored procedure

Hey Guys,

Is there a way to get a stored procedure return a varchar value. I noticed that by default they return @rc which is int and cant be converted from inside the stored procedure. I am going crazy over this. Here's my code



create proc  qwerty
     
as
BEGIN


declare @return_this varchar(255)  

  SET @sreturn_this = N'return a string'  
  return @return_this
 

END
go

I know I could do this in a round about way by putting it in a table or something but that seems like an uneccessary overhead....

Any help is deeply appreciated....
Thanks


Any help is deeply appreciated
0
chetan1981
Asked:
chetan1981
1 Solution
 
Guy Hengel [angelIII / a3]Billing EngineerCommented:
you cannot return varchar as procedure return value, but you can use a output parameter instead, or make it a stored function:

create function dbo.qwerty
returns varchar(255)    
as
BEGIN
  declare @return_this varchar(255)  

  SET @sreturn_this = 'return a string'  
  return (@return_this)
END

0
 
Aneesh RetnakaranDatabase AdministratorCommented:
One more option


create proc  qwerty
     
as
BEGIN
   declare @return_this varchar(255)  

  SET @sreturn_this = N'return a string'  
  SELECT @return_this
 END

in this case the  sp will  return a recordset, so u should hanlde thid at the frontend. In Angels method the sp will return a 'Value'
0
 
Anthony PerkinsCommented:
First of all you have a typo in your variable name, it should be either @sreturn_this or @return_this

The following is what angelIII was referring to using an OUTPUT parameter (no points please):

create proc  qwerty
                   @return_this varchar(255)  OUTPUT
As

BEGIN

SET @return_this = 'return a string'      -- No point in using N', as it is a varchar (not nvarchar)

END

You can test this in SQL Query Analyzer as follows:

Declare @X varchar(255)
exec qwerty @X OUTPUT
Select @X
0
 
chetan1981Author Commented:
Thanks you guys....decided to go with a UDF instead. BUt this was new knowledge nevertheless.

Thanks once again!!!
0

Featured Post

Fill in the form and get your FREE NFR key NOW!

Veeam is happy to provide a FREE NFR server license to certified engineers, trainers, and bloggers.  It allows for the non‑production use of Veeam Agent for Microsoft Windows. This license is valid for five workstations and two servers.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now