微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

sqlserver中编写自定义函数中的返回值问题

写了一个获取符号间隔字符串的函数,原先代码如下

CREATE FUNCTION funGetSubStr
(
 -- Add the parameters for the function here

 @StrParams nvarchar(4000),
 @char char(1)=N'1'
)
RETURNS nvarchar(4000)
AS
BEGIN
 declare  --@str  varchar(300),
 @id  varchar(3000),
 @m  int,
 @n  int 
 --set  @str='a,b,c,d,e' 
 --set  @m=CHARINDEX(',',@str) 
 --set  @n=1 
 --WHILE  @m>0 
 --BEGIN 
 --    set  @id=substring(@str,@n,@m-@n) 
 --    print  @id 
 --    set  @n=@m+1 
 --    set  @m=CHARINDEX(',@str,@n) 
 --END
 set @m=charindex(@char,@strparams)
 set @n=1
 if @m>0
 begin
  set @id=substring(@strparams,@m,@m-@n)

   return @id

 end
 else
 begin
return null

 end
END
GO

sqlserver提示函数最后一句必须是return语句,后改为如下

 

-- ================================================
-- Template generated from Template Explorer using:
-- Create Scalar Function (New Menu).sql
--
-- Use the Specify Values for Template Parameters
-- command (Ctrl-Shift-M) to fill in the parameter
-- values below.
--
-- This block of comments will not be included in
-- the deFinition of the function.
-- ================================================
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
-- =============================================
-- Author:  <Author,Name>
-- Create date: <Create Date,>
-- Description: <Description,>
-- =============================================
CREATE FUNCTION funGetSubStr
(
 -- Add the parameters for the function here

 @StrParams nvarchar(4000),@m-@n)

 end
 else
 begin
  set @id=null
 end
   return @id

END
GO

 

通过。

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。

相关推荐