Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to return varchar value from a function

Tags:

I written the following function.

SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO Create FUNCTION NameFunction (     @eid int ) RETURNS varchar AS BEGIN     Declare @logid varchar(50);     SELECT @logid = E.LoginId from HumanResources.Employee As E     where E.BusinessEntityID = @eid      RETURN  @logid  END GO 

When I am executing it is showing result as a. But expected result is adventure-works\terri0 Where I did the mistake here. Only first character coming. Need to change any thing?

like image 435
Searcher Avatar asked Sep 25 '12 10:09

Searcher


People also ask

How do I return a varchar function in SQL?

RETURNS varchar should be RETURNS varchar(50) . varchar without a length specified is interpreted as varchar(1) in this context (and as varchar(30) in the context of a CAST ).

How can we return varchar value in stored procedure?

You can use out parameter or the resulset to return any data type. Show activity on this post. Show activity on this post. A stored procedure's return code is always integer, but you can have OUTPUT parameters that are any desired type -- see http://msdn.microsoft.com/en-us/library/aa174792.aspx .

How can we return varchar from stored procedure in Oracle?

CREATE PROC [myproc] @output VARCHAR(3) OUTPUT AS SET @output = 'SSS'; RETURN 0; Which you could call like this, DECLARE @output VARCHAR(3); EXEC [myproc] @output OUTPUT; SELECT @output; Or maybe you'd prefer to return a scalar result set?

Can we return table from function?

To return a table from the function, you use RETURNS TABLE syntax and specify the columns of the table. Each column is separated by a comma (, ). In the function, we return a query that is a result of a SELECT statement.


Video Answer


1 Answers

Change your RETURN type to include a length, at this point it is just returning 1 character:

RETURNS varchar(100) 

Full code:

SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO Create FUNCTION NameFunction (     @eid int ) RETURNS varchar(100) -- or whatever length you need AS BEGIN     Declare @logid varchar(50);     SELECT @logid = E.LoginId from HumanResources.Employee As E     where E.BusinessEntityID = @eid      RETURN  @logid  END GO 
like image 179
Taryn Avatar answered Sep 28 '22 09:09

Taryn