8

Why the function will not call SQL Server

 3 years ago
source link: https://www.codesd.com/item/why-the-function-will-not-call-sql-server.html
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
neoserver,ios ssh client

Why the function will not call SQL Server

advertisements

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?


I cleaned up your code so it's valid (no size on ints, correct column name for Posts.Id):

CREATE FUNCTION typeOfPost
(@PostId int)
RETURNS varchar(30)
AS
BEGIN
declare @PostTypeId int
select @PostTypeId = (SELECT PostTypeId FROM posts WHERE Id = @PostId)
return (SELECT Name FROM PostTypes WHERE Id = @PostTypeId)
end
​

And then you get:

Error: CREATE FUNCTION permission denied in database 'StackOverflow.Exported'.\

You can't create objects in StackOverflow Data Explorer. The best object creation you can hope for is table variables.

In addition, the reason you get the error is that when the batch is syntax checked, the function doesn't exist, so the second statement won't work. In traditional SSMS environment, you would create the function in a batch and then execute using the function in another batch. This can be done in a single file using the GO batch separator. This is a feature of SSMS and some other tools (and can be overridden in the options).

In addition, the problem you are trying to solve is not normally one solved with scalar functions (and certainly not ones which individually make trips to tables to retrieve data). Normally you would handle this very simply with a JOIN, which is a lot more accessible to the optimizer than a scalar function, which tend to be treated as block boxes.


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK