Sql server 无法在SQL Server上生成Module11函数
创建一个函数CheckModulo11,用于检查给定的accountNr是否为有效数字。在具有表示accountNr的列的表中的check子句中使用此函数。(您需要通过添加约束创建一个表来存储accountNr,并尝试将数据插入表中) 我的代码:Sql server 无法在SQL Server上生成Module11函数,sql-server,function,if-statement,while-loop,Sql Server,Function,If Statement,While Loop,创建一个函数CheckModulo11,用于检查给定的accountNr是否为有效数字。在具有表示accountNr的列的表中的check子句中使用此函数。(您需要通过添加约束创建一个表来存储accountNr,并尝试将数据插入表中) 我的代码: create function CheckModulo11(@accountNr int) returns @tableModulo11 table( modulo_nr int, number int, total int,
create function CheckModulo11(@accountNr int)
returns @tableModulo11 table(
modulo_nr int,
number int,
total int,
status char(5)
)
as
BEGIN
Declare @tmpTable table(
modulo_nr int,
number int,
total int,
status char(5)
)
Declare @length int, @counter int = 1, @value int, @total int, @changeNums char(10);
SET @changeNums = CONVERT(char(10), @accountNr);
Set @length = LEN(@changeNums);
while @length > 0
BEGIN
SET @value = CONVERT(int,SUBSTRING(@changeNums, @length, 1))
insert into @tmpTable
(modulo_nr, number, total, status) values
(@counter, @value, (@counter * @value), '-');
SET @total = @total + (@counter * @value);
SET @length = @length - 1;
SET @counter = @counter + 1;
END
IF @total % 11 = 0
insert into @tmpTable (modulo_nr, number, total, status) values ('', '', @total, 'valid');
ELSE
insert into @tmpTable (modulo_nr, number, total, status) values ('', '', @total, 'not valid');
insert into @tableModulo11
SELECT * FROM @tmpTable
return
END
GO
select * from dbo.CheckModulo11(972428577);
错误消息:
Msg 8152,第16级,第14状态,第185行字符串或二进制数据将被截断。
该语句已终止。
我发现了为什么它不起作用 您的函数很好,只是忘记了将@total变量初始化为0。因此,无论您增加它,它始终保持null;它在退出函数之前崩溃 尝试用这一行替换变量声明行,它将起作用(我尝试过):
我发现了为什么它不起作用 您的函数很好,只是忘记了将@total变量初始化为0。因此,无论您增加它,它始终保持null;它在退出函数之前崩溃 尝试用这一行替换变量声明行,它将起作用(我尝试过):
如果您有问题和错误消息,请将其作为文本包含在问题中。如果您要求用户访问超链接只是为了了解您遇到的问题,那么您的问题很可能会被忽略。非常感谢您的建议,我将在将来给予更多关注。如果您有问题和错误消息,请将其作为文本包含在您的问题中。如果您要求用户访问超链接只是为了了解您遇到的问题,那么您的问题很可能会被忽略。非常感谢您的建议,我以后会更加关注
Declare @length int, @counter int = 1, @value int, @total int = 0, @changeNums char(10);