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)
 UDF - User Defined Functions

Author  Topic 

dfarrer
Starting Member

4 Posts

Posted - 2004-06-11 : 05:45:19
Hello All,

Im hoping someone can help me get off the ground with UDF's. I try and create the following in Query Analyser ...

USE PUBS

-- =============================================
-- Create scalar function (FN)
-- =============================================
CREATE FUNCTION test_function
(@p1 int,
@p2 int)
RETURNS int
AS
BEGIN
RETURN @p1 + @p2
-- eg.
-- DECLARE @sum AS int
-- SELECT @sum = @p1 + @P2
-- RETURN @sum
END
GO

About the simplest UDF you can make .. it fails with folloing error messages.

Server: Msg 170, Level 15, State 1, Line 6
Line 6: Incorrect syntax near 'FUNCTION'.
Server: Msg 137, Level 15, State 1, Line 12
Must declare the variable '@p1'.

Any help would be really appreciated.

Many Thanks

Dax Farrer

ditch
Master Smack Fu Yak Hacker

1466 Posts

Posted - 2004-06-11 : 05:50:04
add a GO statement after use pubs.

--****************************************
USE PUBS
go
-- =============================================
-- Create scalar function (FN)
-- =============================================
CREATE FUNCTION test_function
(@p1 int,
@p2 int)
RETURNS int
AS
BEGIN
RETURN @p1 + @p2
-- eg.
-- DECLARE @sum AS int
-- SELECT @sum = @p1 + @P2
-- RETURN @sum
END
GO

create function must always be the first statement in a batch.


Duane.
Go to Top of Page

JasonGoff
Posting Yak Master

158 Posts

Posted - 2004-06-11 : 05:51:48
Which version of SQL Server are you using? The code above worked fine on my implementation of SQL2000.
Go to Top of Page

dfarrer
Starting Member

4 Posts

Posted - 2004-06-11 : 05:51:55
Nixce idea, but it didnt work.. still returning same error messages when I try and parse it
Go to Top of Page

dfarrer
Starting Member

4 Posts

Posted - 2004-06-11 : 05:53:29
In the Query Analyser About box its Version 8.00.194
Go to Top of Page

JasonGoff
Posting Yak Master

158 Posts

Posted - 2004-06-11 : 05:59:39
ok, in query analyser, run..

select @@version

What server version does that report. If it's 2000, great. Anything else doesn't support functions !
Go to Top of Page

dfarrer
Starting Member

4 Posts

Posted - 2004-06-11 : 06:03:13
Many thanks, though my Enterprise Manager seems to be 2000 the analyser returns 7. This computer did have 7 installed on it
Go to Top of Page
   

- Advertisement -