Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2000 Forums
 Transact-SQL (2000)
 FUNCTION vs OUTPUT parameter

Author  Topic 

ilimax
Posting Yak Master

164 Posts

Posted - 2008-06-13 : 16:10:41
I have never used FUNCTION in Transact SQL.
I see RETURN statement in this example below and for me looks same as OUTPUT parameter in STORED PROCEDURE. I found in BOOKS ONLINE, I can execute FUNCTION same as STORED PROCEDURE. I have never tried to execute from my VB applications, but I will try.
Can anybody tell me where is the best place to use CREATE FUNCTION?



CREATE FUNCTION fnFirstName (@FullName VarChar(100) , @FirstOrLast VarChar(5))
RETURNS VarChar(100)
AS
BEGIN
DECLARE @CommaPosition Int
DECLARE @TheName VarChar(100)
IF @FirstOrLast = 'First'
BEGIN
SET @CommaPosition = CHARINDEX(',', @FullName)
SET @TheName = SUBSTRING(@FullName, @CommaPosition + 2, LEN(@FullName))
END
ELSE IF @FirstOrLast = 'Last'
BEGIN
SET @CommaPosition = CHARINDEX(',', @FullName)
SET @TheName = SUBSTRING(@FullName, 1, @CommaPosition - 1)
END
RETURN @TheName
END
GO


tkizer
Almighty SQL Goddess

38200 Posts

Posted - 2008-06-13 : 16:14:48
You can't execute a function in the same way as a stored procedure. Functions get used in queries, like this:

--scalar-valued
SELECT dbo.SomeFunction(Column1)
FROM Table1

--table-valued
SELECT * FROM dbo.SomeOtherFunction(@var1)

Tara Kizer
Microsoft MVP for Windows Server System - SQL Server
http://weblogs.sqlteam.com/tarad/

Subscribe to my blog
Go to Top of Page
   

- Advertisement -