SQL标量函数,如何不使用select语句

发布于 2024-12-21 16:47:19 字数 1161 浏览 0 评论 0原文

我正在构建第一个标量函数 (MS SQL Server),但收到此错误:

“函数中包含的 Select 语句无法将数据返回到客户端。”

我知道我不能使用 select 语句,但是我如何获取信息呢?

该函数显示学生是否已经学习并通过了基础科目,其工作方式如下:

参数 id 是一个通用 id,它为我提供了来自学生表的学生 ID。 然后,从“主题”表中我得到主题类型。给定 kindOfSubject 参数的函数 getKindOfSubjectStudied 将显示学生传递的主题是否是初级的。如果是这样,结果将为 1,否则将为 0。

所有单独测试的查询都有效,没有错误。

下面是功能代码的解释。

多谢

set ANSI_NULLS ON
set QUOTED_IDENTIFIER ON
USE MYDBASE
GO

CREATE FUNCTION [dbo].[getStudentHasElementary] 
( 
    -- paramenter
    @id int
)
RETURNS INTEGER
AS
BEGIN
    DECLARE @Result AS INTEGER
    DECLARE @return_value varchar (50)

SELECT studentsid 
FROM dbo.Students
WHERE (id = id)  

SELECT  kindOfSubject 
FROM dbo.Subjects
WHERE (studentsid = studentsid) 


--- I use this function to know if the subject is elementary
EXEC    @return_value = [dbo].[getKindOfSubjectStudied]
        @id = id,
        @kindOfSubject = kindOfSubject;

SELECT    'Return Value' = @return_value



IF (@return_value = 'elementary')
        BEGIN
            SET @Result = 1
        END
        ELSE
        BEGIN
            SET @Result = 0
        END

    RETURN @Result
END

I'm building my first scalar function (MS SQL Server) and I get this error:

"Select statements included within a function cannot return data to a client."

I understand that I cannot use select statements, but how do I get the info then?

The function shows if a student has studied and passed elementary subjects, and works this way:

the parameter id is a general id which gives me the studentid from table Students.
Then, from table Subjects I get the kind of subject. The function getKindOfSubjectStudied given kindOfSubject parameter will show if the subject passed by the student is elementary. If so the result will be 1, otherwise it'll be 0.

All the queries tested separately work, no errors.

Below is the function code explained.

Thanks a lot

set ANSI_NULLS ON
set QUOTED_IDENTIFIER ON
USE MYDBASE
GO

CREATE FUNCTION [dbo].[getStudentHasElementary] 
( 
    -- paramenter
    @id int
)
RETURNS INTEGER
AS
BEGIN
    DECLARE @Result AS INTEGER
    DECLARE @return_value varchar (50)

SELECT studentsid 
FROM dbo.Students
WHERE (id = id)  

SELECT  kindOfSubject 
FROM dbo.Subjects
WHERE (studentsid = studentsid) 


--- I use this function to know if the subject is elementary
EXEC    @return_value = [dbo].[getKindOfSubjectStudied]
        @id = id,
        @kindOfSubject = kindOfSubject;

SELECT    'Return Value' = @return_value



IF (@return_value = 'elementary')
        BEGIN
            SET @Result = 1
        END
        ELSE
        BEGIN
            SET @Result = 0
        END

    RETURN @Result
END

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

蒗幽 2024-12-28 16:47:19

只需更改此行:

SELECT    'Return Value' = @return_value

RETURN @return_value;

您还必须将该变量的定义修改为 int,如下所示:

DECLARE @return_value int;

或将函数的返回值更改为 varchar(50) 像这样:

CREATE FUNCTION [dbo].[getStudentHasElementary] 
( 
    -- paramenter
    @id int
)
RETURNS varchar(50)

你必须清理你的两个查询。尝试声明 @kindofsubject 变量并以这种方式编写查询:

DECLARE @kindofsubject varchar(50); -- I'm guessing here...?
-- make sure your constraints ensure only one row will be returned
-- otherwise use TOP 1 (not a great idea, really)
SELECT @kindofsubject = kindOfSubject 
FROM dbo.Subjects
WHERE (studentsid = studentsid);

Just change this line:

SELECT    'Return Value' = @return_value

to

RETURN @return_value;

You do also have to modify the definition of that variable to either be int like so:

DECLARE @return_value int;

Or change the return value of your function to varchar(50) like this:

CREATE FUNCTION [dbo].[getStudentHasElementary] 
( 
    -- paramenter
    @id int
)
RETURNS varchar(50)

You do have to clean up your two queries. Try declaring the @kindofsubject variable and writing the query this way:

DECLARE @kindofsubject varchar(50); -- I'm guessing here...?
-- make sure your constraints ensure only one row will be returned
-- otherwise use TOP 1 (not a great idea, really)
SELECT @kindofsubject = kindOfSubject 
FROM dbo.Subjects
WHERE (studentsid = studentsid);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文