SQL SERVER函数出现问题,求救
以下函数在编辑的时候总是提示在while那边出现问题,求救
set ANSI_NULLS ON
set QUOTED_IDENTIFIER ON
go
-- =============================================
-- Author: <Author,,Name>
-- Create date: <Create Date,,>
-- Description: <Description,,>
-- =============================================
ALTER FUNCTION [dbo].[DeleteHZ]
(
-- Add the parameters for the function here
@cargoname varchar(512)
)
RETURNS varchar(150)
AS
begin
DECLARE @Result varchar(512)
declare @sno smallint
select @Result=''
select @sno=1
while(@sno<=datalength(@cargoname))
begin
if datalength(SUBSTRING(@cargoname,@sno, 1))=1
set @Result=@Result+SUBSTRING(@cargoname,@sno, 1)
set @sno=@sno+1
select @Result
end
RETURN('')
end
GO
------解决方案--------------------ALTER FUNCTION [dbo].[DeleteHZ]
(
@cargoname varchar(512)
)
RETURNS varchar(150)
AS
begin
DECLARE @Result varchar(512)
declare @sno smallint
select @Result=''
select @sno=1
while(@sno<=datalength(@cargoname))
begin
if DATALENGTH(SUBSTRING(@cargoname,@sno, 1))=1
set @Result=@Result+SUBSTRING(@cargoname,@sno, 1)
set @sno=@sno+1
--select @Result --这一行函数里不允许包含select语句
end
RETURN @Result --RETURN你的结果
end
GO
SELECT DBO.[DeleteHZ]('我们a')
--DATALENGTH: 返回字符串的字节长度
--LEN: 返回字符长度