question

rawilken avatar image
rawilken asked

Return Initials

I am trying to call a working function call FullName while feeding into it a new variable Alter Function Student ( ) Returns varchar(20) AS BEGIN Exec dbo.FullName('Rodd', 'A.', 'Wilken') END GO
t-sql
10 |1200

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.

Cyborg avatar image
Cyborg answered
To call a function you should use SELECT instead of EXEC,rewrite your code as SELECT dbo.FullName('Rodd', 'A.', 'Wilken'). If it is a table valued function then, SELECT * FROM dbo.FullName('Rodd', 'A.', 'Wilken') Also note that you cannot call Procedures inside Functions.
1 comment
10 |1200

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.

Kev Riley avatar image Kev Riley ♦♦ commented ·
@Cyborg, you can execute a udf if it is scalar-valued
1 Like 1 ·
Kev Riley avatar image
Kev Riley answered
If `fullname` is a scalar-valued function, then you can EXECUTE it declare @fn varchar(500) execute @fn = dbo.fullname 'Rodd', 'A.', 'Wilken' select @fn
5 comments
10 |1200

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.

Kev Riley avatar image Kev Riley ♦♦ commented ·
@cyborg - don't put the function parameters in ( )
3 Likes 3 ·
Cyborg avatar image Cyborg commented ·
Kev Note That he is used EXEC inside the scalar valued Function
0 Likes 0 ·
Kev Riley avatar image Kev Riley ♦♦ commented ·
@Cyborg - thats ok - it's allowed - try this alter function fullname ( @first varchar(50), @middle varchar(50), @last varchar(50) ) returns varchar(200) as begin declare @s varchar(200) select @s= @first + ' ' + @middle + ' ' + @last return(@s) end go alter function student() returns varchar(200) as begin declare @fn varchar(500) execute @fn = dbo.fullname 'Kev','P','Riley' return(@fn) end go select dbo.student()
0 Likes 0 ·
Cyborg avatar image Cyborg commented ·
Kev i used the following code


CREATE FUNCTION getName2(@Name VARCHAR(100))
RETURNS  VARCHAR(100)
AS
BEGIN
DECLARE @out VARCHAR(100)
 EXEC @Out = dbo.getName(@Name)
RETURN @OUT
END

but it fails and got the following message
Msg 102, Level 15, State 1, Procedure getName2, Line 8
Incorrect syntax near '@Name'.

why it is so?, m I doing any thing weird?
0 Likes 0 ·
Cyborg avatar image Cyborg commented ·
Ye!!!! Thanks Kev!
0 Likes 0 ·

Write an Answer

Hint: Notify or tag a user in this post by typing @username.

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.