I'm trying to create / call a function on the Stack Exchange Data Explorer - I haven't done much SQL Server before but only MySQL.
Why won't it let me call this function I've just made?
-- Get Post with Best Comment on Site
-- Gets the Post with the Best Comment on the Site and Associated Data
CREATE FUNCTION typeOfPost
(@PostId int(11))
RETURNS varchar(30)
AS
BEGIN
declare @PostTypeId int(3)
select @PostTypeId = (SELECT PostTypeId FROM posts WHERE PostId = @PostId)
return (SELECT Name FROM PostTypes WHERE Id = @PostTypeId)
end
SELECT PostId, typeOfPost(PostId) AS [Post Type]
FROM comments
WHERE Score = (
SELECT max(Score)
FROM comments
);
It gives:
"SELECT"."typeOfPost" is not a recognized built in function name.
So I looked at examples of function calls in SQL Server and I saw a lot had ".dbo" on the front. If I put that on I get this:
Incorrect syntax near the keyword 'SELECT'.
Can anyone explain what's wrong with my function?