Sunday, February 19, 2012

Creating functions

Well, I just downloaded SQL 2005 Express to have a play. Good old Bill
says "up and running in 20 minutes"

Well 5 hrs, 5 reboots and 4 full un-install and re-install later I have
finally got it working. About what I expected really.

Anyway, my question relates to creating user defined functions. I've 10
yrs exp in MS Access and VB, and user ent Manager to create views on
the work server, but havnt done much with functions.

I'm using the Management Studio Express.

I've opened a new view and copied the following sample code but it wont
let me save it.

--------
IF OBJECT_ID(N'dbo.GetWeekDay', N'FN') IS NOT NULL
DROP FUNCTION dbo.GetWeekDay;
GO
CREATE FUNCTION dbo.GetWeekDay -- function name
(@.Date datetime) -- input parameter name and data
type
RETURNS int -- return parameter data type
AS
BEGIN -- begin body definition
RETURN DATEPART (weekday, @.Date) -- action performed
END;
GO
-------------

When I try to save it it says
"incorrect syntax near the key work 'IF'
incorrect syntax near 'GO'
CREATE FUNCTION must be the first statement in a query batch
must declair the scalar variable "@.Date"

Whats wrong with the syntax?

Thanks in advance

GrantHi frnd
there is no need for ' ; ' inside sql its on c++...
remove ' ; ' and try it might work...|||ok I'm confused about functiosn. In the past I have used MS Access ADP
to create function in SQL Server and they have shown up as a separate
object under views. But now I have executed a Create Function and it
says its execute ok, and I can run a select statement and call the
function and return valurs, so its obvoiusly there (somewhere), but its
nto showing up under views, so where to i view and edit functions??

Thanks

Grant|||Functions are under Functions (not Views).

g...@.technologyworks.co.nz wrote:

Quote:

Originally Posted by

ok I'm confused about functiosn. In the past I have used MS Access ADP
to create function in SQL Server and they have shown up as a separate
object under views. But now I have executed a Create Function and it
says its execute ok, and I can run a select statement and call the
function and return valurs, so its obvoiusly there (somewhere), but its
nto showing up under views, so where to i view and edit functions??
>
Thanks
>
Grant

|||(grant@.technologyworks.co.nz) writes:

Quote:

Originally Posted by

ok I'm confused about functiosn. In the past I have used MS Access ADP
to create function in SQL Server and they have shown up as a separate
object under views. But now I have executed a Create Function and it
says its execute ok, and I can run a select statement and call the
function and return valurs, so its obvoiusly there (somewhere), but its
nto showing up under views, so where to i view and edit functions??


There is a Functions node under Programmability.

--
Erland Sommarskog, SQL Server MVP, esquel@.sommarskog.se
Books Online for SQL Server 2005 at
http://www.microsoft.com/technet/pr...oads/books.mspx
Books Online for SQL Server 2000 at
http://www.microsoft.com/sql/prodin...ions/books.mspx

No comments:

Post a Comment