Robin clave
Robin clave

Reputation: 638

Return varchar from SQL Server stored procedure

I have a stored procedure which returns an integer value.

Can I return a varchar instead of int ? Is there any way to return the varchar value to the calling procedures?

Upvotes: 1

Views: 5084

Answers (3)

percebus
percebus

Reputation: 847

It really escapes me why everywhere I turn nobody mentios the simple fact that you can just declare it like this

CREATE SP_foo
AS
  BEGIN
RETURN 'muajaja'
  END
GO

Than you can call something like this:

DECLARE @r varchar(255)
EXEC @r = SP_foo

I don't know if NOBODY mentions it like this because is probably not part of the T-SQL standard per-se? Even the MS docs says something weird:

RETURN [ integer_expression ]

integer_expression Is the integer value that is returned. Stored procedures can return an integer value to a calling procedure or an application

But hey! as long as it works, right?

Upvotes: 0

juergen d
juergen d

Reputation: 204746

select cast(123 as varchar(11))

Upvotes: 4

Jon Egerton
Jon Egerton

Reputation: 41539

You've got a couple of options:

Either use output parameters:

http://msdn.microsoft.com/en-us/library/ms378108.aspx

or just select the varchar value at the end to return the data in a one-line/one-field table.

Upvotes: 3

Related Questions