用户定义函数中的控制流

时间:2022-02-25 13:00:10

I created this funcion:

我创造了这个功能:

CREATE FUNCTION F_Emp_NumEmp
(@NumDe NUMERIC(3))
RETURNS TABLE
AS
    RETURN (
        SELECT
            td.NumDe, NumEm, Nombre
        FROM
            TDepto td INNER JOIN Temple tem ON (td.NumDe = tem.NumDe)
        WHERE
            td.NumDe = @NumDe
    );

The problem I have is that I do not find any method to perform flow control within it, and I have not found any reference. I mean, what I'm trying to do is that when invoking this function, if the introduced parameter does not match any value, it shows a personalized message.

我遇到的问题是我没有找到任何方法在其中执行流量控制,我没有找到任何参考。我的意思是,我要做的是,在调用此函数时,如果引入的参数与任何值都不匹配,则会显示个性化消息。

1 个解决方案

#1


1  

Like most of the programming languages out there SQL Server functions also have a pre-defined signature (Number of Parameters, type of parameters , return type etc).

与大多数编程语言一样,SQL Server函数也具有预定义的签名(参数数量,参数类型,返回类型等)。

If you want an object which decides the control of flow, then you should be looking into Stored Procedures and not functions.

如果您想要一个决定流控制的对象,那么您应该查看存储过程而不是函数。

The function you have created is an Inline Table valued function it is not flexible at all in terms of what can you do inside an Inline Table valued function, it will always return a table, there can be only a single Select statement inside an Inline Table valued function etc etc.

你创建的函数是一个内联表值函数,根据你在内联表值函数中可以做什么,它根本不灵活,它总是返回一个表,在内联表中只能有一个Select语句有价值的功能等

If you are looking for a more flexible type of the function then maybe look into Multi Statement Table valued Functions, in those functions you can have more complex logic inside your function and you can do a lot more. However, it is still a function and the Function Signature rules apply, hence the function can only return a specific type of object which will be a table in case of a Multi Statement Table valued Function.

如果您正在寻找更灵活的函数类型,那么可以查看多语句表值函数,在这些函数中,您可以在函数内部使用更复杂的逻辑,并且可以执行更多操作。但是,它仍然是一个函数,并且函数签名规则适用,因此该函数只能返回特定类型的对象,该对象在多语句表值为Function的情况下将是一个表。

Like I mentioned before if you are looking for more flexibility in your object and trying to control execution flow inside that object Stored Procedures are the way to go. There depending on what parameter values are passed you can call different functions and do different things etc.

就像我之前提到的,如果您正在寻找对象的更多灵活性并尝试控制该对象内的执行流程,那么Stored Procedures就是您的选择。根据传递的参数值,您可以调用不同的函数并执行不同的操作等。

#1


1  

Like most of the programming languages out there SQL Server functions also have a pre-defined signature (Number of Parameters, type of parameters , return type etc).

与大多数编程语言一样,SQL Server函数也具有预定义的签名(参数数量,参数类型,返回类型等)。

If you want an object which decides the control of flow, then you should be looking into Stored Procedures and not functions.

如果您想要一个决定流控制的对象,那么您应该查看存储过程而不是函数。

The function you have created is an Inline Table valued function it is not flexible at all in terms of what can you do inside an Inline Table valued function, it will always return a table, there can be only a single Select statement inside an Inline Table valued function etc etc.

你创建的函数是一个内联表值函数,根据你在内联表值函数中可以做什么,它根本不灵活,它总是返回一个表,在内联表中只能有一个Select语句有价值的功能等

If you are looking for a more flexible type of the function then maybe look into Multi Statement Table valued Functions, in those functions you can have more complex logic inside your function and you can do a lot more. However, it is still a function and the Function Signature rules apply, hence the function can only return a specific type of object which will be a table in case of a Multi Statement Table valued Function.

如果您正在寻找更灵活的函数类型,那么可以查看多语句表值函数,在这些函数中,您可以在函数内部使用更复杂的逻辑,并且可以执行更多操作。但是,它仍然是一个函数,并且函数签名规则适用,因此该函数只能返回特定类型的对象,该对象在多语句表值为Function的情况下将是一个表。

Like I mentioned before if you are looking for more flexibility in your object and trying to control execution flow inside that object Stored Procedures are the way to go. There depending on what parameter values are passed you can call different functions and do different things etc.

就像我之前提到的,如果您正在寻找对象的更多灵活性并尝试控制该对象内的执行流程,那么Stored Procedures就是您的选择。根据传递的参数值,您可以调用不同的函数并执行不同的操作等。