2012-12-26 64 views
1

可能重複:
recursive function.. won’t work again SQL轉換編號,以詞語的阿拉伯

我創建包含0和99之間的數字的表格; 對於這兩個數字的查詢工作, 和它的工作對這些數字太之間的數字: 100,200,1000,2000,1100,2100年,100萬一般來說,適用於數量> 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 
+1

[不要再發表您的問題(HTTP:/ /stackoverflow.com/questions/14040702/recursive-function-wont-work-again-sql)。如果您繼續這樣做,您可能會發現自己被禁止提出Stack Overflow問題。那是你要的嗎? – 2012-12-26 14:22:34

+0

爲什麼?即時通訊試圖知道我的錯誤在哪裏花了超過24小時,這是工作,我正在努力找到一個人,可以看到我沒有看到的錯誤。 – Nejthe

+0

因爲重複的問題是高度皺眉在堆棧交換網絡網站,這就是爲什麼。問題的重複必然意味着閱讀,評論和回答這些問題的努力重複。爲什麼你希望不必要地增加其他人花費的工作量? – 2012-12-26 14:29:35

回答

0

問題可能是您正在追加到result而未初始化它。空字符串不是空字符串(在大多數數據庫中)。

嘗試改變聲明:

DECLARE @result as nvarchar(1024) = '' 

或者,如果您使用的是舊版本的SQL Server:

DECLARE @result as nvarchar(1024); 
SET @result = = '' 
+1

它沒有工作..但我戈納檢查,進一步 我真的很感謝你的幫助。謝謝 – Nejthe