2014-02-20 265 views
0

我希望你們中的一些人能幫助我。我的SQL技能相當薄弱,而且我的工作中還有一個重要的SSIS工作現在無法運行。自從我開始沒有問題以來,它運行良好3年以上,但在過去的48小時內它失敗了兩次。錯誤如下:SQL子查詢返回多個值,困惑於查詢設計?

子查詢返回的值超過1。當 子查詢遵循=,!=,<,< =,>,> =或當子查詢用作 表達式時,這是不允許的。

我已經確認(盡我所能)沒有對數據庫結構/設計或任何內容進行更改,但遺憾的是,這就是我所能做的。否則這個可愛的問題屬於我。

我知道錯誤意味着我得到多個記錄時,我應該得到一個,但這是關於盡我所知。

該作業的流程如下:ProvUserDefinedDataRotated表每晚都被丟棄,然後在下面的查詢立即執行。

SELECT objectinternalid, 
     FaxComments = Isnull ((SELECT value 
           FROM provuserdefineddata 
           WHERE userdefinedfieldinternalid = 75 
            AND objectinternalid = Q.objectinternalid), 
        ''), 
     FaxCommentsDate = Isnull ((SELECT value 
            FROM provuserdefineddata 
            WHERE userdefinedfieldinternalid = 76 
             AND objectinternalid = 
              Q.objectinternalid), '' 
         ), 
     Specialty = Isnull ((SELECT value 
          FROM provuserdefineddata 
          WHERE userdefinedfieldinternalid = 77 
            AND objectinternalid = Q.objectinternalid), 
        ''), 
     DepartmentFullDescript = Isnull ((SELECT value 
             FROM provuserdefineddata 
             WHERE userdefinedfieldinternalid = 78 
               AND objectinternalid = 
                Q.objectinternalid), ''), 
     Alerts1 = Isnull ((SELECT value 
          FROM provuserdefineddata 
          WHERE userdefinedfieldinternalid = 79 
           AND objectinternalid = Q.objectinternalid), '') 
     , 
     ProviderStatus = Isnull ((SELECT value 
           FROM provuserdefineddata 
           WHERE userdefinedfieldinternalid = 80 
             AND objectinternalid = 
              Q.objectinternalid), ''), 
     ProviderRole = Isnull ((SELECT value 
           FROM provuserdefineddata 
           WHERE userdefinedfieldinternalid = 81 
             AND objectinternalid = 
         Q.objectinternalid), ''), 
     ProfessionalTitle = Isnull ((SELECT value 
            FROM provuserdefineddata 
            WHERE userdefinedfieldinternalid = 82 
              AND objectinternalid = 
               Q.objectinternalid), 
          ''), 
     SignatureBlockTitle = Isnull ((SELECT value 
             FROM provuserdefineddata 
             WHERE userdefinedfieldinternalid = 96 
              AND objectinternalid = 
          Q.objectinternalid), ''), 
     AlertsUsedWithProfTitles = Isnull ((SELECT value 
              FROM provuserdefineddata 
              WHERE userdefinedfieldinternalid = 
                98 
                AND objectinternalid = 
                 Q.objectinternalid), ''), 
     ProfTitleAlertDescrpt = Isnull ((SELECT value 
             FROM provuserdefineddata 
             WHERE userdefinedfieldinternalid = 99 
               AND objectinternalid = 
                Q.objectinternalid), ''), 
     ProviderFullFaxNumber = Isnull ((SELECT value 
             FROM provuserdefineddata 
             WHERE userdefinedfieldinternalid = 102 
               AND objectinternalid = 
                Q.objectinternalid), ''), 
     NonEsig = Isnull ((SELECT value 
          FROM provuserdefineddata 
          WHERE userdefinedfieldinternalid = 112 
           AND objectinternalid = Q.objectinternalid), '') 
     , 
     EsigEWSTrainNotes = Isnull ((SELECT value 
            FROM provuserdefineddata 
            WHERE userdefinedfieldinternalid = 146 
              AND objectinternalid = 
               Q.objectinternalid), 
          ''), 
     ESigEWSTrainedDate = Isnull ((SELECT value 
            FROM provuserdefineddata 
            WHERE userdefinedfieldinternalid = 147 
              AND objectinternalid = 
               Q.objectinternalid), 
          ''), 
     EPICTrained = Isnull ((SELECT value 
           FROM provuserdefineddata 
           WHERE userdefinedfieldinternalid = 163 
            AND objectinternalid = Q.objectinternalid), 
        ''), 
     EPICTrainedDate = Isnull ((SELECT value 
            FROM provuserdefineddata 
            WHERE userdefinedfieldinternalid = 164 
             AND objectinternalid = 
              Q.objectinternalid), '' 
         ) 
INTO provuserdefineddatarotated 
FROM provuserdefineddata Q 
GROUP BY objectinternalid 
ORDER BY objectinternalid 

從我有限的瞭解,好像這個查詢只是將數據複製到新創建的ProvUserDefinedDataRotated後表示,表被刪除表?

我在困惑什麼這樣

FaxComments = Isnull ((SELECT value 
          FROM provuserdefineddata 
          WHERE userdefinedfieldinternalid = 75 
           AND objectinternalid = Q.objectinternalid), 
       ''), 

個人陳述實際上意味着雖然。我完全不明白該代碼中發生了什麼。 :(

此查詢和/或指導如何解決設計任何解釋這在所有會如此感激。我一直在谷歌上搜索瘋狂不得要領。

謝謝!

+0

請放心,無論誰寫這個SQL都做得不好,並留下了一個定時炸彈,最終會炸燬。 –

回答

1

它的核心是這個查詢從provuserdefineddata表中撤回記錄。該表中的驅動程序是objectinternalid這個值然後用於根據各種類型的userdefinedfieldinternalid來在同一個表中找回匹配。

在該片斷

FaxComments = ISNULL((SELECT值 FROM provuserdefineddata WHERE userdefinedfieldinternalid = 75 AND objectinternalid = Q.objectinternalid) ''),

讀取,作爲獲得「值「,它具有userdefinedfieldinternalid爲75並且與主表中的objectinternalid匹配的表provuserdefineddata。哦,如果我們沒有找到任何東西(isnull),那麼把它做成一個空字符串。

該機制假定將找到零到一個項目。找到不止一個結果,你有可愛的錯誤。

所以,你是正確的,代碼沒有改變,但有些地方已經引入了數據不應該在那裏。

你如何解決它?如果沒有代碼更改,您需要確定不止一次顯示的userdefinedfieldinternalid和objectinternalid組合(針對上面的ID列表)。

除了完全重構此數據庫設計外,解決此問題的更安全方法是將top 1或合計max/min應用於子查詢。

+0

謝謝,我現在完全明白這一點!我能夠追蹤重複記錄並聯系創建它們的運營用戶來解決! – Oryx

1

這裏是的isNull的解釋:http://technet.microsoft.com/en-us/library/ms184325.aspx

總之爲isNull(select value ..., '')

if (the inner select value is null) 
then return '' 
else return value of inner select 

與您的查詢問題是,這些子查詢必須返回一個或沒有值。 (這只是一個規則)。

在您的子查詢中,您總是請求相同的表。因此,對於一個特定的userdefinedfieldinternalidobjectinternalid必須有多個值。不限制userdefinedfieldinternalid給你使用,你可以有選擇喜歡選擇「多」值的那些值:

SELECT userdefinedfieldinternalid, objectinternalid FROM provuserdefineddata group by userdefinedfieldinternalid, objectinternalid having count(*)>1 

我希望我得到了你的數據庫管理系統的語法正確。