如何创建返回 int 的 SQL Server 函数?
我正在尝试创建一个 SQL 函数来测试参数是否以某个术语开头或包含该术语,但不以其开头。
基本上,如果参数以术语开头,则函数返回 0。否则返回 1。
这是我所拥有的函数的骨架,我正在尝试从我发现的另一个函数中进行改编:
CREATE FUNCTION [dbo].[fnGetRelevance]
(
@fieldName nvarchar(50),
@searchTerm nvarchar(50)
)
RETURNS @value int -- this is showing an error, it seems to expect table but all I want is an int
(
-- does this need to be here? If so, what should it be?
)
AS
BEGIN
declare @field TABLE(Data nvarchar(50))
insert into @field
select Data from @fieldName
if (Data like @searchTerm + '%') -- starts with
begin
return 0
end
else if (Data like '%' + @searchTerm + '%' and Data not like @searchTerm + '%') -- contains, but doesn't start with
begin
return 1
end
END
GO
I'm trying to create a SQL Function that tests whether a parameter starts with a certain term or contains the term, but doesn't start with it.
Basically, if the parameter starts with the term, the function returns a 0. Otherwise it returns a 1.
This is the bones of the function that I have, which I'm trying to adapt from another function I found:
CREATE FUNCTION [dbo].[fnGetRelevance]
(
@fieldName nvarchar(50),
@searchTerm nvarchar(50)
)
RETURNS @value int -- this is showing an error, it seems to expect table but all I want is an int
(
-- does this need to be here? If so, what should it be?
)
AS
BEGIN
declare @field TABLE(Data nvarchar(50))
insert into @field
select Data from @fieldName
if (Data like @searchTerm + '%') -- starts with
begin
return 0
end
else if (Data like '%' + @searchTerm + '%' and Data not like @searchTerm + '%') -- contains, but doesn't start with
begin
return 1
end
END
GO
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
您无需为返回值提供变量名称,只需提供其类型,并且不需要括号;
还;
不起作用,您需要 动态 SQL 从名称位于多变的。
You don't provide a variable name for the return value, just its type, and the parens are not needed;
Also;
Will not work, you would need dynamic SQL to select from an object the name of which is in a variable.
作为参考,这是根据 Alex K 的建议实现的完整功能
for reference, this is the complete function as implemented with the suggestions from Alex K
这里有一些问题。我已在下面的代码中添加了注释:
这应该会让您更接近您想要实现的目标。
There are a few problems here. I've added comments to the code below:
This should get you a bit closer to what you're trying to acheive.
我假设以下内容:
@fieldName
实际上是一个表名称(根据您尝试的使用情况判断)。@searchterm
是您要查找的术语Data
是表中的一列@fieldName
如果以上任何一项不正确,则答案为嘶嘶无用。
您将需要使用动态 sql,因为选择查询中的表无法参数化。您将需要 2 个不同版本的动态 sql,因为您想要检查“开头为”和更一般的“包含”。您将需要动态 sql 的输出变量来确定调用的结果。
顺便说一句,
INT
就大小而言完全是多余的。如果你只有 2 个状态(我怀疑),你需要BIT
,如果你有 3 个状态(正如我怀疑),你需要TINYINT
。我现在将坚持使用 int 以接近您原来的示例,但请考虑更改它。Im assuming the following:
@fieldName
is in fact a table name (judging by your attempted usage).@searchterm
is the term you're looking forData
is a column in table@fieldName
If any of the above are incorrect, this answer is neigh-on useless.
You will need to use dynamic sql as the table in a select query cannot be parameterised. You will need 2 different versions of the dynamic sql as you want to check "starts with" and more general "contains". You will need an output variable from the dynamic sql in order to determine the result of the call.
As an asside,
INT
is total overkill in terms of size. If you have just 2 states (which I doubt) you wantBIT
, if you have 3 states (as I suspect) you wantTINYINT
. I'll stick with int for now to stay close to your original example, but consider changing it.