可能重复:
递归函数..不会再次工作 SQL
我创建了一个包含 0 到 99 之间数字的表格;对于这两个数字之间的数字,查询正在工作,并且它也适用于这些数字:100、200、1000、2000、1100、2100、1000000。通常它适用于数字> 100并且不包含数字从我已经创建的表中......对于所有其他数字,我得到“空”。我认为问题出在变量 @Units 中,但我不能再专注于此了。
CREATE FUNCTION dbo.fnIntegerToWords(@Nb as BIGINT)
RETURNS NVARCHAR(1024)
AS
BEGIN
DECLARE @Millions as int
DECLARE @Remainder as int
DECLARE @Thousands as bigint
DECLARE @Hundreds as int
DECLARE @Units as int
DECLARE @word as nvarchar (1024)
DECLARE @result as nvarchar(1024)
set @Millions = @Nb/1000000
set @Units = @Nb %1000000
IF @Millions > 0
BEGIN
if @Millions = 1
set @result = N' مليون'
if @Units > 0
begin
set @Thousands = @Units / 1000
set @Units = @Units % 1000
if @Thousands > 0
begin
if @Thousands = 1
set @result = @result + N' و' + N'ألف'
else
if @Thousands = 2
set @result = @result + N' و' + N'ألفان'
else
if @Thousands between 100 and 199
BEGIN
set @Hundreds = @Thousands / 100
set @Remainder = @Thousands % 100
if @Remainder = 0
begin
set @result = @result + N' مائة ألف'
set @Thousands = 0
end
else
if @Remainder > 0
begin
set @result = @result + N' و' + N'مائة'
if @Remainder > 0
set @result = @result + N' و' + dbo.fnIntegerToWords(@Remainder) + N' ألف'
end
END
else
if @Thousands between 200 and 299
BEGIN
set @Hundreds = @Thousands / 100
set @Remainder = @Thousands % 100
if @Remainder = 0
begin
set @result = @result + N' و' + N' مئتان ألف'
set @Thousands = 0
end
else
if @Remainder > 0
begin
set @result = @result + N' و' + N' مئتان'
if @Remainder > 0
set @result = @result + N' و' + dbo.fnIntegerToWords(@Remainder) + N' ألف'
end
END
else
if @Thousands > 299
BEGIN
set @Hundreds = @Thousands / 100
set @Remainder = @Thousands % 100
if @Remainder = 0
begin
set @result = @result + N' و' + dbo.fnIntegerToWords(@Hundreds) + N' مائة ألف'
end
else
begin
set @result = @result + N' و' + dbo.fnIntegerToWords(@Hundreds) + N' مائة'
if @Remainder > 0
set @result = @result + N' و' + dbo.fnIntegerToWords(@Remainder)+ N' ألف'
end
END
else
if @Thousands between 3 and 99
set @result = @result + N' و' + dbo.fnIntegerToWords(@Thousands) + N' ألف'
END
if @Units > 0
Begin
set @Hundreds = @Units / 100
set @Units = @Units % 100
if @Hundreds > 0
BEGIN
if @Hundreds = 1
set @result = @result + N' و' + N' مائة'
else
if @Hundreds = 2
set @result = @result + N' و' + N' مئتان'
else
set @result = @result + N' و' + dbo.fnIntegerToWords(@Hundreds) + N' مائة'
END
if @Units > 0
set @result = @result + N' و' + dbo.fnIntegerToWords(@Units)
End
end
END
ELSE
IF @Millions = 0
BEGIN
set @Thousands = @Units /1000
set @Units = @Units %1000
IF @Thousands > 0
BEGIN
if @Thousands = 1
set @result = N' ألف'
else
if @Thousands = 2
set @result = N' ألفان'
else
if @Thousands between 100 and 199
BEGIN
set @Hundreds = @Thousands / 100
set @Remainder = @Thousands % 100
if @Remainder = 0
set @result = N' مائة ألف'
else
if @Remainder > 0
begin
set @result = N' مائة'
if @Remainder > 0
set @result = @result + N' و' + dbo.fnIntegerToWords(@Remainder) + N' ألف'
end
END
else
if @Thousands between 200 and 299
BEGIN
set @Hundreds = @Thousands / 100
set @Remainder = @Thousands % 100
if @Remainder = 0
begin
set @result = N' مئتان ألف'
end
else
if @Remainder > 0
begin
set @result = N' مئتان'
if @Remainder > 0
set @result = @result + N' و' + dbo.fnIntegerToWords(@Remainder) + N' ألف'
end
END
else
if @Thousands > 299
BEGIN
set @Hundreds = @Thousands / 100
set @Remainder = @Thousands % 100
if @Remainder = 0
begin
set @result = dbo.fnIntegerToWords(@Hundreds) + N' مائة ألف'
end
else
begin
set @result = dbo.fnIntegerToWords(@Hundreds) + N' مائة'
if @Remainder > 0
set @result = @result + N' و' + dbo.fnIntegerToWords(@Remainder) + N' ألف'
end
END
else
set @result = dbo.fnIntegerToWords(@Thousands) + N' ألف'
if @Units <> 0
BEGIN
set @Hundreds = @Units / 100
set @Units = @Units % 100
if @Hundreds > 0
BEGIN
if @Hundreds = 1
set @result = @result + N' و' + N' مائة'
else
if @Hundreds = 2
set @result = @result + N' و' + N' مئتان'
else
set @result = @result + N' و' + dbo.fnIntegerToWords(@Hundreds) + N' مائة'
END
if @Units > 0
set @result = @result + N' و' + dbo.fnIntegerToWords(@Units)
END
END
END
ELSE
IF @Thousands = 0
BEGIN
set @Hundreds = @Units / 100
set @Units = @Units % 100
if @Hundreds > 0
BEGIN
if @Hundreds = 1
set @result = N' مائة'
else
if @Hundreds = 2
set @result = N' مئتان'
else
set @result = dbo.fnIntegerToWords(@Hundreds) + N' مائة'
if @Units > 0
set @result = @result + N' و' + dbo.fnIntegerToWords(@Units)
END
else
set @result = (select COALESCE(word,'') from number where @Units=number)
END
RETURN @result
END